Created
June 22, 2025 11:45
-
-
Save farnoy/a518fd96bfe42dadc0342a53bce1c6b1 to your computer and use it in GitHub Desktop.
seroval streaming deserialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { serializeAsync, deserialize, crossSerializeStream } from "seroval"; | |
const $R = []; | |
const ASYNC_RECURSIVE = {} as Record<string, Promise<unknown>>; | |
ASYNC_RECURSIVE.a = Promise.resolve(ASYNC_RECURSIVE); | |
ASYNC_RECURSIVE.b = new Promise(r => setTimeout(() => r(2137), 2000)); | |
console.log('simple', await serializeAsync(ASYNC_RECURSIVE)) | |
console.log('simple parsed', deserialize(await serializeAsync(ASYNC_RECURSIVE))) | |
console.log('serializing') | |
const result = crossSerializeStream(ASYNC_RECURSIVE, { | |
onSerialize(data: string) { | |
console.log('chunk', data) | |
eval(data) | |
console.log('current view', $R[0]) | |
}, | |
async onDone() { | |
console.log('parsed', $R[0]) | |
console.log('await parsed.a', await $R[0].a) | |
console.log('await parsed.b', await $R[0].b) | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
simple (h=>h={a:Promise.resolve().then(()=>h),b:Promise.resolve(2137)})() | |
simple parsed { a: Promise { <pending> }, b: Promise { 2137 } } | |
serializing | |
chunk ($R[0]={a:$R[1]=($R[3]=r=>(r.p=new Promise((s,f)=>{r.s=s,r.f=f})))($R[2]={p:0,s:0,f:0}),b:$R[4]=$R[3]($R[5]={p:0,s:0,f:0})}) | |
current view { a: Promise { <pending> }, b: Promise { <pending> } } | |
f [Function: bound destroy] | |
chunk ($R[6]=(r,d)=>{r.s(d),r.p.s=1,r.p.v=d})($R[2],$R[0]) | |
current view <ref *1> { | |
a: Promise { [Circular *1], s: 1, v: [Circular *1] }, | |
b: Promise { <pending> } | |
} | |
chunk $R[6]($R[5],2137) | |
current view <ref *1> { | |
a: Promise { [Circular *1], s: 1, v: [Circular *1] }, | |
b: Promise { 2137, s: 1, v: 2137 } | |
} | |
parsed <ref *1> { | |
a: Promise { [Circular *1], s: 1, v: [Circular *1] }, | |
b: Promise { 2137, s: 1, v: 2137 } | |
} | |
await parsed.a <ref *1> { | |
a: Promise { [Circular *1], s: 1, v: [Circular *1] }, | |
b: Promise { 2137, s: 1, v: 2137 } | |
} | |
await parsed.b 2137 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment