diff --git a/packages/seroval/src/core/abort-signal.ts b/packages/seroval/src/core/abort-signal.ts index a95ebb2..fdca374 100644 --- a/packages/seroval/src/core/abort-signal.ts +++ b/packages/seroval/src/core/abort-signal.ts @@ -1,13 +1,19 @@ -export function abortSignalToPromise(signal: AbortSignal): Promise { - return new Promise(resolve => { - signal.addEventListener( - 'abort', - () => { - resolve(signal.reason); - }, - { - once: true, - }, - ); +function resolveAbortSignalResult( + this: AbortSignal, + resolve: (value: any) => void, +): void { + resolve(this.reason); +} + +function resolveAbortSignal( + this: AbortSignal, + resolve: (value: any) => void, +): void { + this.addEventListener('abort', resolveAbortSignalResult.bind(this, resolve), { + once: true, }); } + +export function abortSignalToPromise(signal: AbortSignal): Promise { + return new Promise(resolveAbortSignal.bind(signal)); +} diff --git a/packages/seroval/src/core/context/parser/stream.ts b/packages/seroval/src/core/context/parser/stream.ts index 17450e0..306b01d 100644 --- a/packages/seroval/src/core/context/parser/stream.ts +++ b/packages/seroval/src/core/context/parser/stream.ts @@ -281,6 +281,34 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont return result; } + protected handleAbortSignal(id: number, current: AbortSignal): void { + if (this.alive) { + const parsed = this.parseWithError(current.reason); + if (parsed) { + this.onParse( + createSerovalNode( + SerovalNodeType.AbortSignalAbort, + id, + NIL, + NIL, + NIL, + NIL, + NIL, + NIL, + [ + this.parseSpecialReference(SpecialReference.AbortSignalAbort), + parsed, + ], + NIL, + NIL, + NIL, + ), + ); + } + } + this.popPendingState(); + } + protected parseAbortSignal( id: number, current: AbortSignal, @@ -293,33 +321,7 @@ export default abstract class BaseStreamParserContext extends BaseSyncParserCont current.addEventListener( 'abort', - () => { - if (this.alive) { - const parsed = this.parseWithError(current.reason); - if (parsed) { - this.onParse( - createSerovalNode( - SerovalNodeType.AbortSignalAbort, - id, - NIL, - NIL, - NIL, - NIL, - NIL, - NIL, - [ - this.parseSpecialReference(SpecialReference.AbortSignalAbort), - parsed, - ], - NIL, - NIL, - NIL, - ), - ); - } - } - this.popPendingState(); - }, + this.handleAbortSignal.bind(this, id, current), { once: true }, ); diff --git a/packages/seroval/src/core/cross/index.ts b/packages/seroval/src/core/cross/index.ts index 2edb1ae..b8d9175 100644 --- a/packages/seroval/src/core/cross/index.ts +++ b/packages/seroval/src/core/cross/index.ts @@ -131,9 +131,7 @@ export function crossSerializeStream( ctx.start(source); - return () => { - ctx.destroy(); - }; + return ctx.destroy.bind(ctx); } export type ToCrossJSONStreamOptions = CrossStreamParserContextOptions; @@ -154,9 +152,7 @@ export function toCrossJSONStream( ctx.start(source); - return () => { - ctx.destroy(); - }; + return ctx.destroy.bind(ctx); } export type FromCrossJSONOptions = CrossDeserializerContextOptions;