Skip to content

Commit 8ca80dd

Browse files
author
igor.luckenkov
committed
create IAbortSignal, IAbortController types to use instead of globals
1 parent 11a5d32 commit 8ca80dd

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

src/execution/__tests__/executor-test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { describe, it } from 'mocha';
44
import { expectJSON } from '../../__testUtils__/expectJSON.js';
55
import { resolveOnNextTick } from '../../__testUtils__/resolveOnNextTick.js';
66

7+
import type { IAbortSignal, IEvent } from '../../jsutils/AbortController.js';
78
import { inspect } from '../../jsutils/inspect.js';
89

910
import { Kind } from '../../language/kinds.js';
@@ -1317,7 +1318,7 @@ describe('Execute: Handles basic execution tasks', () => {
13171318

13181319
it('stops execution and throws an error when signal is aborted', async () => {
13191320
// TODO: use real Event once we can finally drop node14 support
1320-
class MockAbortEvent implements Event {
1321+
class MockAbortEvent implements IEvent {
13211322
cancelable = false;
13221323
bubbles = false;
13231324
composed = false;
@@ -1335,9 +1336,9 @@ describe('Execute: Handles basic execution tasks', () => {
13351336
CAPTURING_PHASE = 0;
13361337
NONE = 0;
13371338

1338-
target: AbortSignal;
1339+
target: IAbortSignal;
13391340

1340-
constructor(abortSignal: AbortSignal) {
1341+
constructor(abortSignal: IAbortSignal) {
13411342
this.target = abortSignal;
13421343
}
13431344

@@ -1362,9 +1363,9 @@ describe('Execute: Handles basic execution tasks', () => {
13621363
};
13631364
}
13641365

1365-
class MockAbortSignal implements AbortSignal {
1366+
class MockAbortSignal implements IAbortSignal {
13661367
aborted: boolean = false;
1367-
onabort: ((ev: Event) => any) | null = null;
1368+
onabort: ((ev: IEvent) => any) | null = null;
13681369
reason: unknown;
13691370

13701371
throwIfAborted() {
@@ -1386,7 +1387,7 @@ describe('Execute: Handles basic execution tasks', () => {
13861387
this.onabort = null;
13871388
}
13881389

1389-
dispatchEvent(event: Event): boolean {
1390+
dispatchEvent(event: IEvent): boolean {
13901391
expect(this.onabort).to.be.a('function');
13911392
this.onabort?.(event);
13921393
return true;

src/execution/execute.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import type {
2+
IAbortController,
3+
IAbortSignal,
4+
IEvent,
5+
} from '../jsutils/AbortController.js';
16
import { inspect } from '../jsutils/inspect.js';
27
import { invariant } from '../jsutils/invariant.js';
38
import { isAsyncIterable } from '../jsutils/isAsyncIterable.js';
@@ -262,7 +267,7 @@ export interface ExecutionArgs {
262267
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
263268
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
264269
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
265-
signal?: AbortSignal | undefined;
270+
signal?: IAbortSignal | undefined;
266271
}
267272

268273
const UNEXPECTED_EXPERIMENTAL_DIRECTIVES =
@@ -523,16 +528,18 @@ class ExecutionController {
523528
/** For performance reason we can't use `signal.isAborted` so we cache it here */
524529
isAborted: boolean = false;
525530

526-
private readonly _passedInAbortSignal: AbortSignal | undefined;
527-
private readonly _abortController: AbortController | undefined =
528-
typeof AbortController !== 'undefined' ? new AbortController() : undefined;
531+
private readonly _passedInAbortSignal: IAbortSignal | undefined;
532+
private readonly _abortController: IAbortController | undefined =
533+
typeof AbortController !== 'undefined'
534+
? (new AbortController() as IAbortController)
535+
: undefined;
529536

530-
constructor(signal?: AbortSignal) {
537+
constructor(signal?: IAbortSignal) {
531538
this._passedInAbortSignal = signal;
532539
this._passedInAbortSignal?.addEventListener('abort', this._abortCB);
533540
}
534541

535-
get signal(): AbortSignal | undefined {
542+
get signal(): IAbortSignal | undefined {
536543
return this._abortController?.signal;
537544
}
538545

@@ -542,7 +549,7 @@ class ExecutionController {
542549
this.isAborted = true;
543550
}
544551

545-
private readonly _abortCB = (event: Event) =>
552+
private readonly _abortCB = (event: IEvent) =>
546553
this.abort(
547554
event.target && 'reason' in event.target
548555
? event.target.reason

src/graphql.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { IAbortSignal } from './jsutils/AbortController.js';
12
import { isPromise } from './jsutils/isPromise.js';
23
import type { Maybe } from './jsutils/Maybe.js';
34
import type { PromiseOrValue } from './jsutils/PromiseOrValue.js';
@@ -70,7 +71,7 @@ export interface GraphQLArgs {
7071
operationName?: Maybe<string>;
7172
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
7273
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
73-
signal?: AbortSignal;
74+
signal?: IAbortSignal;
7475
}
7576

7677
export function graphql(args: GraphQLArgs): Promise<ExecutionResult> {

src/jsutils/AbortController.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface IAbortController {
2+
abort: (reason?: unknown) => void;
3+
signal: IAbortSignal;
4+
}
5+
6+
export interface IEvent {
7+
target: any;
8+
}
9+
10+
type EventListener = (event: IEvent) => void;
11+
12+
export interface IAbortSignal {
13+
readonly aborted: boolean;
14+
onabort: ((this: IAbortSignal, ev: IEvent) => any) | null;
15+
readonly reason: any;
16+
throwIfAborted: () => void;
17+
addEventListener: (type: string, listener: EventListener) => void;
18+
removeEventListener: (type: string, listener: EventListener) => void;
19+
}

src/type/definition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { IAbortSignal } from '../jsutils/AbortController.js';
12
import { devAssert } from '../jsutils/devAssert.js';
23
import { didYouMean } from '../jsutils/didYouMean.js';
34
import { identityFunc } from '../jsutils/identityFunc.js';
@@ -904,7 +905,7 @@ export interface GraphQLResolveInfo {
904905
* Note: signal is undefined only if execution environment doesn't support
905906
* AbortController (e.g. node14 without polyfill).
906907
*/
907-
readonly signal: AbortSignal | undefined;
908+
readonly signal: IAbortSignal | undefined;
908909
}
909910

910911
/**

0 commit comments

Comments
 (0)