Skip to content

Commit

Permalink
Fix closures
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Jan 26, 2025
1 parent 28eece6 commit 825dc77
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 44 deletions.
28 changes: 17 additions & 11 deletions packages/seroval/src/core/abort-signal.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
export function abortSignalToPromise(signal: AbortSignal): Promise<any> {
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<any> {
return new Promise(resolveAbortSignal.bind(signal));
}
56 changes: 29 additions & 27 deletions packages/seroval/src/core/context/parser/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 },
);

Expand Down
8 changes: 2 additions & 6 deletions packages/seroval/src/core/cross/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ export function crossSerializeStream<T>(

ctx.start(source);

return () => {
ctx.destroy();
};
return ctx.destroy.bind(ctx);
}

export type ToCrossJSONStreamOptions = CrossStreamParserContextOptions;
Expand All @@ -154,9 +152,7 @@ export function toCrossJSONStream<T>(

ctx.start(source);

return () => {
ctx.destroy();
};
return ctx.destroy.bind(ctx);
}

export type FromCrossJSONOptions = CrossDeserializerContextOptions;
Expand Down

0 comments on commit 825dc77

Please sign in to comment.