File tree Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -28,8 +28,14 @@ export class AbortSignalListener {
28
28
this . abortSignal . removeEventListener ( 'abort' , this . abort ) ;
29
29
}
30
30
31
- cancellablePromise < T > ( originalPromise : Promise < T > ) : Promise < T > {
31
+ cancellablePromise < T > (
32
+ originalPromise : Promise < T > ,
33
+ onCancel ?: ( ( ) => Promise < unknown > ) | undefined ,
34
+ ) : Promise < T > {
32
35
if ( this . abortSignal . aborted ) {
36
+ onCancel ?.( ) . catch ( ( ) => {
37
+ // ignore
38
+ } ) ;
33
39
// eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
34
40
return Promise . reject ( this . abortSignal . reason ) ;
35
41
}
@@ -50,4 +56,26 @@ export class AbortSignalListener {
50
56
51
57
return promise ;
52
58
}
59
+
60
+ cancellableIterable < T > ( iterable : AsyncIterable < T > ) : AsyncIterable < T > {
61
+ const iterator = iterable [ Symbol . asyncIterator ] ( ) ;
62
+
63
+ const earlyReturn =
64
+ typeof iterator . return === 'function'
65
+ ? iterator . return . bind ( iterator )
66
+ : undefined ;
67
+
68
+ const cancellableAsyncIterator : AsyncIterator < T > = {
69
+ next : ( ) => this . cancellablePromise ( iterator . next ( ) , earlyReturn ) ,
70
+ } ;
71
+
72
+ if ( earlyReturn ) {
73
+ cancellableAsyncIterator . return = ( ) =>
74
+ this . cancellablePromise ( earlyReturn ( ) ) ;
75
+ }
76
+
77
+ return {
78
+ [ Symbol . asyncIterator ] : ( ) => cancellableAsyncIterator ,
79
+ } ;
80
+ }
53
81
}
Original file line number Diff line number Diff line change @@ -1387,7 +1387,9 @@ function completeListValue(
1387
1387
const itemType = returnType . ofType ;
1388
1388
1389
1389
if ( isAsyncIterable ( result ) ) {
1390
- const asyncIterator = result [ Symbol . asyncIterator ] ( ) ;
1390
+ const maybeCancellableIterable =
1391
+ exeContext . abortSignalListener ?. cancellableIterable ( result ) ?? result ;
1392
+ const asyncIterator = maybeCancellableIterable [ Symbol . asyncIterator ] ( ) ;
1391
1393
1392
1394
return completeAsyncIteratorValue (
1393
1395
exeContext ,
@@ -2229,7 +2231,7 @@ function executeSubscription(
2229
2231
// TODO: add test case
2230
2232
/* c8 ignore next */
2231
2233
abortSignalListener ?. disconnect ( ) ;
2232
- return resolved ;
2234
+ return abortSignalListener ?. cancellableIterable ( resolved ) ?? resolved ;
2233
2235
} ,
2234
2236
( error : unknown ) => {
2235
2237
abortSignalListener ?. disconnect ( ) ;
You can’t perform that action at this time.
0 commit comments