@@ -28,6 +28,7 @@ import {
2828 isCompletedExecutionGroup ,
2929 isFailedExecutionGroup ,
3030} from './types.js' ;
31+ import { withCleanup } from './withCleanup.js' ;
3132
3233export function buildIncrementalResponse (
3334 context : IncrementalPublisherContext ,
@@ -63,11 +64,13 @@ interface SubsequentIncrementalExecutionResultContext {
6364 * @internal
6465 */
6566class IncrementalPublisher {
67+ private _isDone : boolean ;
6668 private _context : IncrementalPublisherContext ;
6769 private _nextId : number ;
6870 private _incrementalGraph : IncrementalGraph ;
6971
7072 constructor ( context : IncrementalPublisherContext ) {
73+ this . _isDone = false ;
7174 this . _context = context ;
7275 this . _nextId = 0 ;
7376 this . _incrementalGraph = new IncrementalGraph ( ) ;
@@ -92,10 +95,14 @@ class IncrementalPublisher {
9295 ? { errors, data, pending, hasNext : true }
9396 : { data, pending, hasNext : true } ;
9497
95- return {
96- initialResult,
97- subsequentResults : this . _subscribe ( ) ,
98- } ;
98+ const subsequentResults = withCleanup ( this . _subscribe ( ) , async ( ) => {
99+ this . _isDone = true ;
100+ this . _context . abortSignalListener ?. disconnect ( ) ;
101+ this . _incrementalGraph . abort ( ) ;
102+ await this . _returnAsyncIteratorsIgnoringErrors ( ) ;
103+ } ) ;
104+
105+ return { initialResult, subsequentResults } ;
99106 }
100107
101108 private _toPendingResults (
@@ -121,22 +128,12 @@ class IncrementalPublisher {
121128 return String ( this . _nextId ++ ) ;
122129 }
123130
124- private _subscribe ( ) : AsyncGenerator <
131+ private async * _subscribe ( ) : AsyncGenerator <
125132 SubsequentIncrementalExecutionResult ,
126133 void ,
127134 void
128135 > {
129- let isDone = false ;
130-
131- const _next = async ( ) : Promise <
132- IteratorResult < SubsequentIncrementalExecutionResult , void >
133- > => {
134- if ( isDone ) {
135- this . _context . abortSignalListener ?. disconnect ( ) ;
136- await this . _returnAsyncIteratorsIgnoringErrors ( ) ;
137- return { value : undefined , done : true } ;
138- }
139-
136+ while ( ! this . _isDone ) {
140137 const context : SubsequentIncrementalExecutionResultContext = {
141138 pending : [ ] ,
142139 incremental : [ ] ,
@@ -155,7 +152,7 @@ class IncrementalPublisher {
155152 const hasNext = this . _incrementalGraph . hasNext ( ) ;
156153
157154 if ( ! hasNext ) {
158- isDone = true ;
155+ this . _isDone = true ;
159156 }
160157
161158 const subsequentIncrementalExecutionResult : SubsequentIncrementalExecutionResult =
@@ -172,50 +169,14 @@ class IncrementalPublisher {
172169 subsequentIncrementalExecutionResult . completed = completed ;
173170 }
174171
175- return { value : subsequentIncrementalExecutionResult , done : false } ;
172+ yield subsequentIncrementalExecutionResult ;
173+ break ;
176174 }
177175
178176 // eslint-disable-next-line no-await-in-loop
179177 batch = await this . _incrementalGraph . nextCompletedBatch ( ) ;
180178 } while ( batch !== undefined ) ;
181-
182- // TODO: add test for this case
183- /* c8 ignore next */
184- this . _context . abortSignalListener ?. disconnect ( ) ;
185- await this . _returnAsyncIteratorsIgnoringErrors ( ) ;
186- return { value : undefined , done : true } ;
187- } ;
188-
189- const _return = async ( ) : Promise <
190- IteratorResult < SubsequentIncrementalExecutionResult , void >
191- > => {
192- isDone = true ;
193- this . _incrementalGraph . abort ( ) ;
194- await this . _returnAsyncIterators ( ) ;
195- return { value : undefined , done : true } ;
196- } ;
197-
198- const _throw = async (
199- error ?: unknown ,
200- ) : Promise < IteratorResult < SubsequentIncrementalExecutionResult , void > > => {
201- isDone = true ;
202- this . _incrementalGraph . abort ( ) ;
203- await this . _returnAsyncIterators ( ) ;
204- // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
205- return Promise . reject ( error ) ;
206- } ;
207-
208- return {
209- [ Symbol . asyncIterator ] ( ) {
210- return this ;
211- } ,
212- next : _next ,
213- return : _return ,
214- throw : _throw ,
215- async [ Symbol . asyncDispose ] ( ) {
216- await _return ( ) ;
217- } ,
218- } ;
179+ }
219180 }
220181
221182 private _handleCompletedIncrementalData (
0 commit comments