Skip to content

Commit c8fd614

Browse files
authored
refactor(executor): replace internal abort signal with lightweight version (#4650)
1 parent 9190a9f commit c8fd614

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/execution/Executor.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import {
4343
} from '../type/definition.js';
4444
import type { GraphQLSchema } from '../type/schema.js';
4545

46-
import { cancellablePromise } from './cancellablePromise.js';
46+
import { withCancellation } from './cancellablePromise.js';
4747
import type {
4848
DeferUsage,
4949
FieldDetailsList,
@@ -223,8 +223,9 @@ export class Executor<
223223
> {
224224
validatedExecutionArgs: ValidatedExecutionArgs;
225225
aborted: boolean;
226+
abortReason: unknown;
226227
collectedErrors: CollectedErrors;
227-
internalAbortController: AbortController;
228+
abortResultPromise: ((reason?: unknown) => void) | undefined;
228229
resolverAbortController: AbortController | undefined;
229230
sharedResolverAbortSignal: AbortSignal;
230231

@@ -234,8 +235,8 @@ export class Executor<
234235
) {
235236
this.validatedExecutionArgs = validatedExecutionArgs;
236237
this.aborted = false;
238+
this.abortReason = new Error('This operation was aborted');
237239
this.collectedErrors = new CollectedErrors();
238-
this.internalAbortController = new AbortController();
239240

240241
if (sharedResolverAbortSignal === undefined) {
241242
this.resolverAbortController = new AbortController();
@@ -318,7 +319,13 @@ export class Executor<
318319
return this.buildResponse(null);
319320
},
320321
);
321-
return cancellablePromise(promise, this.internalAbortController.signal);
322+
const { promise: cancellablePromise, abort: abortResultPromise } =
323+
withCancellation(promise);
324+
this.abortResultPromise = abortResultPromise;
325+
if (this.aborted) {
326+
abortResultPromise(this.abortReason);
327+
}
328+
return cancellablePromise;
322329
}
323330
maybeRemoveExternalAbortListener();
324331
return this.buildResponse(result);
@@ -330,21 +337,26 @@ export class Executor<
330337
}
331338

332339
throwIfAborted(): void {
333-
this.internalAbortController.signal.throwIfAborted();
340+
if (this.aborted) {
341+
throw this.abortReason;
342+
}
334343
}
335344

336345
abort(reason?: unknown): PromiseOrValue<void> {
337346
if (this.aborted) {
338347
return;
339348
}
340349
this.aborted = true;
341-
this.internalAbortController.abort(reason);
342-
this.resolverAbortController?.abort(reason);
350+
if (reason !== undefined) {
351+
this.abortReason = reason;
352+
}
353+
this.abortResultPromise?.(this.abortReason);
354+
this.resolverAbortController?.abort(this.abortReason);
343355
}
344356

345357
finish(): void {
346-
this.aborted = true;
347358
this.throwIfAborted();
359+
this.aborted = true;
348360
}
349361

350362
/**

0 commit comments

Comments
 (0)