Skip to content

Commit 31ef71a

Browse files
authored
refactor(executor): separate finish/abort orchestration (#4648)
1 parent 3021de5 commit 31ef71a

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/execution/Executor.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ export class Executor<
263263
externalAbortSignal.addEventListener('abort', onExternalAbort);
264264
}
265265

266-
const onFinish = () => {
267-
this.finish();
266+
const maybeRemoveExternalAbortListener = () => {
268267
removeExternalAbortListener?.();
269268
};
270269

@@ -308,39 +307,44 @@ export class Executor<
308307
if (isPromise(result)) {
309308
const promise = result.then(
310309
(data) => {
311-
onFinish();
310+
maybeRemoveExternalAbortListener();
311+
this.throwIfAborted();
312312
return this.buildResponse(data);
313313
},
314314
(error: unknown) => {
315-
onFinish();
315+
maybeRemoveExternalAbortListener();
316+
this.throwIfAborted();
316317
this.collectedErrors.add(ensureGraphQLError(error), undefined);
317318
return this.buildResponse(null);
318319
},
319320
);
320321
return cancellablePromise(promise, this.internalAbortController.signal);
321322
}
322-
onFinish();
323+
maybeRemoveExternalAbortListener();
323324
return this.buildResponse(result);
324325
} catch (error) {
325-
onFinish();
326+
maybeRemoveExternalAbortListener();
326327
this.collectedErrors.add(ensureGraphQLError(error), undefined);
327328
return this.buildResponse(null);
328329
}
329330
}
330331

332+
throwIfAborted(): void {
333+
this.internalAbortController.signal.throwIfAborted();
334+
}
335+
331336
abort(reason?: unknown): PromiseOrValue<void> {
332-
if (!this.aborted) {
333-
this.finish();
334-
this.internalAbortController.abort(reason);
335-
this.resolverAbortController?.abort(reason);
337+
if (this.aborted) {
338+
return;
336339
}
340+
this.aborted = true;
341+
this.internalAbortController.abort(reason);
342+
this.resolverAbortController?.abort(reason);
337343
}
338344

339345
finish(): void {
340-
if (!this.aborted) {
341-
this.aborted = true;
342-
}
343-
this.internalAbortController.signal.throwIfAborted();
346+
this.aborted = true;
347+
this.throwIfAborted();
344348
}
345349

346350
/**
@@ -350,9 +354,11 @@ export class Executor<
350354
buildResponse(
351355
data: ObjMap<unknown> | null,
352356
): ExecutionResult | TAlternativeInitialResponse {
353-
this.resolverAbortController?.abort();
354357
const errors = this.collectedErrors.errors;
355-
return errors.length ? { errors, data } : { data };
358+
const result = errors.length ? { errors, data } : { data };
359+
this.finish();
360+
this.resolverAbortController?.abort();
361+
return result;
356362
}
357363

358364
executeCollectedRootFields(

0 commit comments

Comments
 (0)