1- import { invariant } from '../jsutils/invariant.js' ;
21import type { ObjMap } from '../jsutils/ObjMap.js' ;
32import { pathToArray } from '../jsutils/Path.js' ;
43
@@ -7,7 +6,6 @@ import type { GraphQLError } from '../error/GraphQLError.js';
76import type { AbortSignalListener } from './AbortSignalListener.js' ;
87import { IncrementalGraph } from './IncrementalGraph.js' ;
98import type {
10- CancellableStreamRecord ,
119 CompletedExecutionGroup ,
1210 CompletedResult ,
1311 DeferredFragmentRecord ,
@@ -21,23 +19,25 @@ import type {
2119 InitialIncrementalExecutionResult ,
2220 PendingResult ,
2321 StreamItemsResult ,
22+ StreamRecord ,
2423 SubsequentIncrementalExecutionResult ,
2524} from './types.js' ;
26- import {
27- isCancellableStreamRecord ,
28- isCompletedExecutionGroup ,
29- isFailedExecutionGroup ,
30- } from './types.js' ;
25+ import { isCompletedExecutionGroup , isFailedExecutionGroup } from './types.js' ;
3126import { withCleanup } from './withCleanup.js' ;
3227
28+ // eslint-disable-next-line max-params
3329export function buildIncrementalResponse (
34- context : IncrementalPublisherContext ,
3530 result : ObjMap < unknown > ,
3631 errors : ReadonlyArray < GraphQLError > ,
3732 newDeferredFragmentRecords : ReadonlyArray < DeferredFragmentRecord > | undefined ,
3833 incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
34+ earlyReturns : Map < StreamRecord , ( ) => Promise < unknown > > ,
35+ abortSignalListener : AbortSignalListener | undefined ,
3936) : ExperimentalIncrementalExecutionResults {
40- const incrementalPublisher = new IncrementalPublisher ( context ) ;
37+ const incrementalPublisher = new IncrementalPublisher (
38+ earlyReturns ,
39+ abortSignalListener ,
40+ ) ;
4141 return incrementalPublisher . buildResponse (
4242 result ,
4343 errors ,
@@ -46,11 +46,6 @@ export function buildIncrementalResponse(
4646 ) ;
4747}
4848
49- interface IncrementalPublisherContext {
50- abortSignalListener : AbortSignalListener | undefined ;
51- cancellableStreams : Set < CancellableStreamRecord > | undefined ;
52- }
53-
5449interface SubsequentIncrementalExecutionResultContext {
5550 pending : Array < PendingResult > ;
5651 incremental : Array < IncrementalResult > ;
@@ -64,12 +59,17 @@ interface SubsequentIncrementalExecutionResultContext {
6459 * @internal
6560 */
6661class IncrementalPublisher {
67- private _context : IncrementalPublisherContext ;
62+ private _earlyReturns : Map < StreamRecord , ( ) => Promise < unknown > > ;
63+ private _abortSignalListener : AbortSignalListener | undefined ;
6864 private _nextId : number ;
6965 private _incrementalGraph : IncrementalGraph ;
7066
71- constructor ( context : IncrementalPublisherContext ) {
72- this . _context = context ;
67+ constructor (
68+ earlyReturns : Map < StreamRecord , ( ) => Promise < unknown > > ,
69+ abortSignalListener : AbortSignalListener | undefined ,
70+ ) {
71+ this . _earlyReturns = earlyReturns ;
72+ this . _abortSignalListener = abortSignalListener ;
7373 this . _nextId = 0 ;
7474 this . _incrementalGraph = new IncrementalGraph ( ) ;
7575 }
@@ -100,7 +100,7 @@ class IncrementalPublisher {
100100 return {
101101 initialResult,
102102 subsequentResults : withCleanup ( subsequentResults , async ( ) => {
103- this . _context . abortSignalListener ?. disconnect ( ) ;
103+ this . _abortSignalListener ?. disconnect ( ) ;
104104 await this . _returnAsyncIteratorsIgnoringErrors ( ) ;
105105 } ) ,
106106 } ;
@@ -241,21 +241,18 @@ class IncrementalPublisher {
241241 errors : streamItemsResult . errors ,
242242 } ) ;
243243 this . _incrementalGraph . removeStream ( streamRecord ) ;
244- if ( isCancellableStreamRecord ( streamRecord ) ) {
245- invariant ( this . _context . cancellableStreams !== undefined ) ;
246- this . _context . cancellableStreams . delete ( streamRecord ) ;
247- streamRecord . earlyReturn ( ) . catch ( ( ) => {
244+ const earlyReturn = this . _earlyReturns . get ( streamRecord ) ;
245+ if ( earlyReturn !== undefined ) {
246+ earlyReturn ( ) . catch ( ( ) => {
248247 /* c8 ignore next 1 */
249248 // ignore error
250249 } ) ;
250+ this . _earlyReturns . delete ( streamRecord ) ;
251251 }
252252 } else if ( streamItemsResult . result === undefined ) {
253253 context . completed . push ( { id } ) ;
254254 this . _incrementalGraph . removeStream ( streamRecord ) ;
255- if ( isCancellableStreamRecord ( streamRecord ) ) {
256- invariant ( this . _context . cancellableStreams !== undefined ) ;
257- this . _context . cancellableStreams . delete ( streamRecord ) ;
258- }
255+ this . _earlyReturns . delete ( streamRecord ) ;
259256 } else {
260257 const incrementalEntry : IncrementalStreamResult = {
261258 id,
@@ -310,14 +307,10 @@ class IncrementalPublisher {
310307 }
311308
312309 private async _returnAsyncIterators ( ) : Promise < void > {
313- const cancellableStreams = this . _context . cancellableStreams ;
314- if ( cancellableStreams === undefined ) {
315- return ;
316- }
317310 const promises : Array < Promise < unknown > > = [ ] ;
318- for ( const streamRecord of cancellableStreams ) {
319- if ( streamRecord . earlyReturn !== undefined ) {
320- promises . push ( streamRecord . earlyReturn ( ) ) ;
311+ for ( const earlyReturn of this . _earlyReturns . values ( ) ) {
312+ if ( earlyReturn !== undefined ) {
313+ promises . push ( earlyReturn ( ) ) ;
321314 }
322315 }
323316 await Promise . all ( promises ) ;
0 commit comments