Skip to content

Commit

Permalink
rename PromiseCanceller to AbortSignalListener
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Oct 31, 2024
1 parent 46643cd commit 3036d41
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { promiseWithResolvers } from '../jsutils/promiseWithResolvers.js';

/**
* A PromiseCanceller object can be used to cancel multiple promises
* using a single AbortSignal.
* A AbortSignalListener object can be used to trigger multiple responses
* in response to an abort signal.
*
* @internal
*/
export class PromiseCanceller {
export class AbortSignalListener {
abortSignal: AbortSignal;
abort: () => void;

Expand All @@ -28,7 +28,7 @@ export class PromiseCanceller {
this.abortSignal.removeEventListener('abort', this.abort);
}

withCancellation<T>(originalPromise: Promise<T>): Promise<T> {
cancellablePromise<T>(originalPromise: Promise<T>): Promise<T> {
if (this.abortSignal.aborted) {
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
return Promise.reject(this.abortSignal.reason);
Expand Down
8 changes: 4 additions & 4 deletions src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { pathToArray } from '../jsutils/Path.js';

import type { GraphQLError } from '../error/GraphQLError.js';

import type { AbortSignalListener } from './AbortSignalListener.js';
import { IncrementalGraph } from './IncrementalGraph.js';
import type { PromiseCanceller } from './PromiseCanceller.js';
import type {
CancellableStreamRecord,
CompletedExecutionGroup,
Expand Down Expand Up @@ -44,7 +44,7 @@ export function buildIncrementalResponse(
}

interface IncrementalPublisherContext {
promiseCanceller: PromiseCanceller | undefined;
abortSignalListener: AbortSignalListener | undefined;
cancellableStreams: Set<CancellableStreamRecord> | undefined;
}

Expand Down Expand Up @@ -127,7 +127,7 @@ class IncrementalPublisher {
IteratorResult<SubsequentIncrementalExecutionResult, void>
> => {
if (isDone) {
this._context.promiseCanceller?.disconnect();
this._context.abortSignalListener?.disconnect();
await this._returnAsyncIteratorsIgnoringErrors();
return { value: undefined, done: true };
}
Expand Down Expand Up @@ -176,7 +176,7 @@ class IncrementalPublisher {

// TODO: add test for this case
/* c8 ignore next */
this._context.promiseCanceller?.disconnect();
this._context.abortSignalListener?.disconnect();
await this._returnAsyncIteratorsIgnoringErrors();
return { value: undefined, done: true };
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ import { describe, it } from 'mocha';

import { expectPromise } from '../../__testUtils__/expectPromise.js';

import { PromiseCanceller } from '../PromiseCanceller.js';
import { AbortSignalListener } from '../AbortSignalListener.js';

describe('PromiseCanceller', () => {
describe('AbortSignalListener', () => {
it('works to cancel an already resolved promise', async () => {
const abortController = new AbortController();
const abortSignal = abortController.signal;

const promiseCanceller = new PromiseCanceller(abortSignal);
const abortSignalListener = new AbortSignalListener(abortSignal);

const promise = Promise.resolve(1);

const withCancellation = promiseCanceller.withCancellation(promise);
const cancellablePromise = abortSignalListener.cancellablePromise(promise);

abortController.abort(new Error('Cancelled!'));

await expectPromise(withCancellation).toRejectWith('Cancelled!');
await expectPromise(cancellablePromise).toRejectWith('Cancelled!');
});

it('works to cancel a hanging promise', async () => {
const abortController = new AbortController();
const abortSignal = abortController.signal;

const promiseCanceller = new PromiseCanceller(abortSignal);
const abortSignalListener = new AbortSignalListener(abortSignal);

const promise = new Promise(() => {
/* never resolves */
});

const withCancellation = promiseCanceller.withCancellation(promise);
const cancellablePromise = abortSignalListener.cancellablePromise(promise);

abortController.abort(new Error('Cancelled!'));

await expectPromise(withCancellation).toRejectWith('Cancelled!');
await expectPromise(cancellablePromise).toRejectWith('Cancelled!');
});

it('works to cancel a hanging promise created after abort signal triggered', async () => {
Expand All @@ -43,14 +43,14 @@ describe('PromiseCanceller', () => {

abortController.abort(new Error('Cancelled!'));

const promiseCanceller = new PromiseCanceller(abortSignal);
const abortSignalListener = new AbortSignalListener(abortSignal);

const promise = new Promise(() => {
/* never resolves */
});

const withCancellation = promiseCanceller.withCancellation(promise);
const cancellablePromise = abortSignalListener.cancellablePromise(promise);

await expectPromise(withCancellation).toRejectWith('Cancelled!');
await expectPromise(cancellablePromise).toRejectWith('Cancelled!');
});
});
32 changes: 16 additions & 16 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { GraphQLStreamDirective } from '../type/directives.js';
import type { GraphQLSchema } from '../type/schema.js';
import { assertValidSchema } from '../type/validate.js';

import { AbortSignalListener } from './AbortSignalListener.js';
import type { DeferUsageSet, ExecutionPlan } from './buildExecutionPlan.js';
import { buildExecutionPlan } from './buildExecutionPlan.js';
import type {
Expand All @@ -63,7 +64,6 @@ import {
import { getVariableSignature } from './getVariableSignature.js';
import { buildIncrementalResponse } from './IncrementalPublisher.js';
import { mapAsyncIterable } from './mapAsyncIterable.js';
import { PromiseCanceller } from './PromiseCanceller.js';
import type {
CancellableStreamRecord,
CompletedExecutionGroup,
Expand Down Expand Up @@ -164,7 +164,7 @@ export interface ValidatedExecutionArgs {
export interface ExecutionContext {
validatedExecutionArgs: ValidatedExecutionArgs;
errors: Array<GraphQLError> | undefined;
promiseCanceller: PromiseCanceller | undefined;
abortSignalListener: AbortSignalListener | undefined;
completed: boolean;
cancellableStreams: Set<CancellableStreamRecord> | undefined;
}
Expand Down Expand Up @@ -318,8 +318,8 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
const exeContext: ExecutionContext = {
validatedExecutionArgs,
errors: undefined,
promiseCanceller: abortSignal
? new PromiseCanceller(abortSignal)
abortSignalListener: abortSignal
? new AbortSignalListener(abortSignal)
: undefined,
completed: false,
cancellableStreams: undefined,
Expand Down Expand Up @@ -378,7 +378,7 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
},
(error: unknown) => {
exeContext.completed = true;
exeContext.promiseCanceller?.disconnect();
exeContext.abortSignalListener?.disconnect();
return {
data: null,
errors: withError(exeContext.errors, error as GraphQLError),
Expand All @@ -392,7 +392,7 @@ export function experimentalExecuteQueryOrMutationOrSubscriptionEvent(
exeContext.completed = true;
// TODO: add test case for synchronous null bubbling to root with cancellation
/* c8 ignore next */
exeContext.promiseCanceller?.disconnect();
exeContext.abortSignalListener?.disconnect();
return { data: null, errors: withError(exeContext.errors, error) };
}
}
Expand Down Expand Up @@ -483,7 +483,7 @@ function buildDataResponse(
const { rawResult: data, incrementalDataRecords } = graphqlWrappedResult;
const errors = exeContext.errors;
if (incrementalDataRecords === undefined) {
exeContext.promiseCanceller?.disconnect();
exeContext.abortSignalListener?.disconnect();
return errors !== undefined ? { errors, data } : { data };
}

Expand Down Expand Up @@ -834,7 +834,7 @@ function executeField(
incrementalContext: IncrementalContext | undefined,
deferMap: ReadonlyMap<DeferUsage, DeferredFragmentRecord> | undefined,
): PromiseOrValue<GraphQLWrappedResult<unknown>> | undefined {
const { validatedExecutionArgs, promiseCanceller } = exeContext;
const { validatedExecutionArgs, abortSignalListener } = exeContext;
const { schema, contextValue, variableValues, hideSuggestions, abortSignal } =
validatedExecutionArgs;
const fieldName = fieldDetailsList[0].node.name.value;
Expand Down Expand Up @@ -879,7 +879,7 @@ function executeField(
fieldDetailsList,
info,
path,
promiseCanceller?.withCancellation(result) ?? result,
abortSignalListener?.cancellablePromise(result) ?? result,
incrementalContext,
deferMap,
);
Expand Down Expand Up @@ -1598,7 +1598,7 @@ async function completePromisedListItemValue(
deferMap: ReadonlyMap<DeferUsage, DeferredFragmentRecord> | undefined,
): Promise<unknown> {
try {
const resolved = await (exeContext.promiseCanceller?.withCancellation(
const resolved = await (exeContext.abortSignalListener?.cancellablePromise(
item,
) ?? item);
let completed = completeValue(
Expand Down Expand Up @@ -2220,19 +2220,19 @@ function executeSubscription(
const result = resolveFn(rootValue, args, contextValue, info, abortSignal);

if (isPromise(result)) {
const promiseCanceller = abortSignal
? new PromiseCanceller(abortSignal)
const abortSignalListener = abortSignal
? new AbortSignalListener(abortSignal)
: undefined;
const promise = promiseCanceller?.withCancellation(result) ?? result;
const promise = abortSignalListener?.cancellablePromise(result) ?? result;
return promise.then(assertEventStream).then(
(resolved) => {
// TODO: add test case
/* c8 ignore next */
promiseCanceller?.disconnect();
abortSignalListener?.disconnect();
return resolved;
},
(error: unknown) => {
promiseCanceller?.disconnect();
abortSignalListener?.disconnect();
throw locatedError(error, fieldNodes, pathToArray(path));
},
);
Expand Down Expand Up @@ -2604,7 +2604,7 @@ function completeStreamItem(
fieldDetailsList,
info,
itemPath,
exeContext.promiseCanceller?.withCancellation(item) ?? item,
exeContext.abortSignalListener?.cancellablePromise(item) ?? item,
incrementalContext,
new Map(),
).then(
Expand Down

0 comments on commit 3036d41

Please sign in to comment.