@@ -205,6 +205,50 @@ function serializeArray(ctx: SerializerContext, value: unknown[]) {
205205 return id ;
206206}
207207
208+ function serializeStreamNext (
209+ ctx : SerializerContext ,
210+ depth : number ,
211+ id : Uint8Array ,
212+ value : unknown ,
213+ ) {
214+ if ( ctx . alive ) {
215+ const serialized = serializeWithError ( ctx , depth , value ) ;
216+ if ( serialized ) {
217+ onSerialize ( ctx , [ SerovalNodeType . StreamNext , id , serialized ] ) ;
218+ }
219+ }
220+ }
221+
222+ function serializeStreamThrow (
223+ ctx : SerializerContext ,
224+ depth : number ,
225+ id : Uint8Array ,
226+ value : unknown ,
227+ ) {
228+ if ( ctx . alive ) {
229+ const serialized = serializeWithError ( ctx , depth , value ) ;
230+ if ( serialized ) {
231+ onSerialize ( ctx , [ SerovalNodeType . StreamThrow , id , serialized ] ) ;
232+ }
233+ }
234+ popPendingState ( ctx ) ;
235+ }
236+
237+ function serializeStreamReturn (
238+ ctx : SerializerContext ,
239+ depth : number ,
240+ id : Uint8Array ,
241+ value : unknown ,
242+ ) {
243+ if ( ctx . alive ) {
244+ const serialized = serializeWithError ( ctx , depth , value ) ;
245+ if ( serialized ) {
246+ onSerialize ( ctx , [ SerovalNodeType . StreamReturn , id , serialized ] ) ;
247+ }
248+ }
249+ popPendingState ( ctx ) ;
250+ }
251+
208252function serializeStream ( ctx : SerializerContext , current : Stream < unknown > ) {
209253 const id = createID ( ctx , current ) ;
210254 pushPendingState ( ctx ) ;
@@ -213,32 +257,9 @@ function serializeStream(ctx: SerializerContext, current: Stream<unknown>) {
213257 const prevDepth = CURRENT_DEPTH ;
214258
215259 current . on ( {
216- next : value => {
217- if ( ctx . alive ) {
218- const serialized = serializeWithError ( ctx , prevDepth , value ) ;
219- if ( serialized ) {
220- onSerialize ( ctx , [ SerovalNodeType . StreamNext , id , serialized ] ) ;
221- }
222- }
223- } ,
224- throw : value => {
225- if ( ctx . alive ) {
226- const serialized = serializeWithError ( ctx , prevDepth , value ) ;
227- if ( serialized ) {
228- onSerialize ( ctx , [ SerovalNodeType . StreamThrow , id , serialized ] ) ;
229- }
230- }
231- popPendingState ( ctx ) ;
232- } ,
233- return : value => {
234- if ( ctx . alive ) {
235- const serialized = serializeWithError ( ctx , prevDepth , value ) ;
236- if ( serialized ) {
237- onSerialize ( ctx , [ SerovalNodeType . StreamReturn , id , serialized ] ) ;
238- }
239- }
240- popPendingState ( ctx ) ;
241- } ,
260+ next : serializeStreamNext . bind ( null , ctx , prevDepth , id ) ,
261+ throw : serializeStreamThrow . bind ( null , ctx , prevDepth , id ) ,
262+ return : serializeStreamReturn . bind ( null , ctx , prevDepth , id ) ,
242263 } ) ;
243264 return id ;
244265}
@@ -368,7 +389,12 @@ function serializeBoxed(ctx: SerializerContext, value: object) {
368389function serializeArrayBuffer ( ctx : SerializerContext , value : ArrayBuffer ) {
369390 const id = createID ( ctx , value ) ;
370391 const arr = new Uint8Array ( value ) ;
371- onSerialize ( ctx , [ SerovalNodeType . ArrayBuffer , id , encodeInteger ( arr . length ) , arr ] ) ;
392+ onSerialize ( ctx , [
393+ SerovalNodeType . ArrayBuffer ,
394+ id ,
395+ encodeInteger ( arr . length ) ,
396+ arr ,
397+ ] ) ;
372398 return id ;
373399}
374400
@@ -436,30 +462,44 @@ function serializeSet(ctx: SerializerContext, value: Set<unknown>) {
436462 return id ;
437463}
438464
465+ function serializePromiseSuccess (
466+ ctx : SerializerContext ,
467+ depth : number ,
468+ id : Uint8Array ,
469+ value : unknown ,
470+ ) {
471+ if ( ctx . alive ) {
472+ const serialized = serializeWithError ( ctx , depth , value ) ;
473+ if ( serialized ) {
474+ onSerialize ( ctx , [ SerovalNodeType . PromiseSuccess , id , serialized ] ) ;
475+ }
476+ }
477+ popPendingState ( ctx ) ;
478+ }
479+
480+ function serializePromiseFailure (
481+ ctx : SerializerContext ,
482+ depth : number ,
483+ id : Uint8Array ,
484+ value : unknown ,
485+ ) {
486+ if ( ctx . alive ) {
487+ const serialized = serializeWithError ( ctx , depth , value ) ;
488+ if ( serialized ) {
489+ onSerialize ( ctx , [ SerovalNodeType . PromiseFailure , id , serialized ] ) ;
490+ }
491+ }
492+ popPendingState ( ctx ) ;
493+ }
494+
439495function serializePromise ( ctx : SerializerContext , value : Promise < unknown > ) {
440496 const id = createID ( ctx , value ) ;
441497 onSerialize ( ctx , [ SerovalNodeType . Promise , id ] ) ;
442498 const prevDepth = CURRENT_DEPTH ;
443499 pushPendingState ( ctx ) ;
444500 value . then (
445- val => {
446- if ( ctx . alive ) {
447- const serialized = serializeWithError ( ctx , prevDepth , val ) ;
448- if ( serialized ) {
449- onSerialize ( ctx , [ SerovalNodeType . PromiseSuccess , id , serialized ] ) ;
450- }
451- }
452- popPendingState ( ctx ) ;
453- } ,
454- val => {
455- if ( ctx . alive ) {
456- const serialized = serializeWithError ( ctx , prevDepth , val ) ;
457- if ( serialized ) {
458- onSerialize ( ctx , [ SerovalNodeType . PromiseFailure , id , serialized ] ) ;
459- }
460- }
461- popPendingState ( ctx ) ;
462- } ,
501+ serializePromiseSuccess . bind ( null , ctx , prevDepth , id ) ,
502+ serializePromiseFailure . bind ( null , ctx , prevDepth , id ) ,
463503 ) ;
464504 return id ;
465505}
0 commit comments