@@ -15,32 +15,16 @@ import type {
1515 StreamRecord ,
1616 SubsequentResultRecord ,
1717} from './types.js' ;
18- import { isDeferredGroupedFieldSetRecord } from './types.js' ;
19-
20- interface DeferredFragmentNode {
21- deferredFragmentRecord : DeferredFragmentRecord ;
22- deferredGroupedFieldSetRecords : Set < DeferredGroupedFieldSetRecord > ;
23- reconcilableResults : Set < ReconcilableDeferredGroupedFieldSetResult > ;
24- children : Set < SubsequentResultNode > ;
25- }
26-
27- function isDeferredFragmentNode (
28- node : SubsequentResultNode | undefined ,
29- ) : node is DeferredFragmentNode {
30- return node !== undefined && 'deferredFragmentRecord' in node ;
31- }
32-
33- type SubsequentResultNode = DeferredFragmentNode | StreamRecord ;
18+ import {
19+ isDeferredFragmentRecord ,
20+ isDeferredGroupedFieldSetRecord ,
21+ } from './types.js' ;
3422
3523/**
3624 * @internal
3725 */
3826export class IncrementalGraph {
39- private _rootNodes : Set < SubsequentResultNode > ;
40- private _deferredFragmentNodes : Map <
41- DeferredFragmentRecord ,
42- DeferredFragmentNode
43- > ;
27+ private _rootNodes : Set < SubsequentResultRecord > ;
4428
4529 private _completedQueue : Array < IncrementalDataRecordResult > ;
4630 private _nextQueue : Array <
@@ -49,15 +33,14 @@ export class IncrementalGraph {
4933
5034 constructor ( ) {
5135 this . _rootNodes = new Set ( ) ;
52- this . _deferredFragmentNodes = new Map ( ) ;
5336 this . _completedQueue = [ ] ;
5437 this . _nextQueue = [ ] ;
5538 }
5639
5740 getNewRootNodes (
5841 incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
5942 ) : ReadonlyArray < SubsequentResultRecord > {
60- const initialResultChildren = new Set < SubsequentResultNode > ( ) ;
43+ const initialResultChildren = new Set < SubsequentResultRecord > ( ) ;
6144 this . _addIncrementalDataRecords (
6245 incrementalDataRecords ,
6346 undefined ,
@@ -69,13 +52,12 @@ export class IncrementalGraph {
6952 addCompletedReconcilableDeferredGroupedFieldSet (
7053 reconcilableResult : ReconcilableDeferredGroupedFieldSetResult ,
7154 ) : void {
72- for ( const deferredFragmentNode of this . _fragmentsToNodes (
73- reconcilableResult . deferredGroupedFieldSetRecord . deferredFragmentRecords ,
74- ) ) {
75- deferredFragmentNode . deferredGroupedFieldSetRecords . delete (
55+ for ( const deferredFragmentRecord of reconcilableResult
56+ . deferredGroupedFieldSetRecord . deferredFragmentRecords ) {
57+ deferredFragmentRecord . deferredGroupedFieldSetRecords . delete (
7658 reconcilableResult . deferredGroupedFieldSetRecord ,
7759 ) ;
78- deferredFragmentNode . reconcilableResults . add ( reconcilableResult ) ;
60+ deferredFragmentRecord . reconcilableResults . add ( reconcilableResult ) ;
7961 }
8062
8163 const incrementalDataRecords = reconcilableResult . incrementalDataRecords ;
@@ -131,64 +113,50 @@ export class IncrementalGraph {
131113 reconcilableResults : ReadonlyArray < ReconcilableDeferredGroupedFieldSetResult > ;
132114 }
133115 | undefined {
134- const deferredFragmentNode = this . _deferredFragmentNodes . get (
135- deferredFragmentRecord ,
136- ) ;
137116 // TODO: add test case?
138117 /* c8 ignore next 3 */
139- if ( deferredFragmentNode === undefined ) {
118+ if ( ! this . _rootNodes . has ( deferredFragmentRecord ) ) {
140119 return ;
141120 }
142- if ( deferredFragmentNode . deferredGroupedFieldSetRecords . size > 0 ) {
121+ if ( deferredFragmentRecord . deferredGroupedFieldSetRecords . size > 0 ) {
143122 return ;
144123 }
145124 const reconcilableResults = Array . from (
146- deferredFragmentNode . reconcilableResults ,
125+ deferredFragmentRecord . reconcilableResults ,
147126 ) ;
148- this . _removeRootNode ( deferredFragmentNode ) ;
127+ this . _removeRootNode ( deferredFragmentRecord ) ;
149128 for ( const reconcilableResult of reconcilableResults ) {
150- for ( const otherDeferredFragmentNode of this . _fragmentsToNodes (
151- reconcilableResult . deferredGroupedFieldSetRecord
152- . deferredFragmentRecords ,
153- ) ) {
154- otherDeferredFragmentNode . reconcilableResults . delete (
129+ for ( const otherDeferredFragmentRecord of reconcilableResult
130+ . deferredGroupedFieldSetRecord . deferredFragmentRecords ) {
131+ otherDeferredFragmentRecord . reconcilableResults . delete (
155132 reconcilableResult ,
156133 ) ;
157134 }
158135 }
159136 const newRootNodes = this . _promoteNonEmptyToRoot (
160- deferredFragmentNode . children ,
137+ deferredFragmentRecord . children ,
161138 ) ;
162139 return { newRootNodes, reconcilableResults } ;
163140 }
164141
165142 removeDeferredFragment (
166143 deferredFragmentRecord : DeferredFragmentRecord ,
167144 ) : boolean {
168- const deferredFragmentNode = this . _deferredFragmentNodes . get (
169- deferredFragmentRecord ,
170- ) ;
171- if ( deferredFragmentNode === undefined ) {
145+ if ( ! this . _rootNodes . has ( deferredFragmentRecord ) ) {
172146 return false ;
173147 }
174- this . _removeRootNode ( deferredFragmentNode ) ;
175- this . _deferredFragmentNodes . delete ( deferredFragmentRecord ) ;
176- // TODO: add test case for an erroring deferred fragment with child defers
177- /* c8 ignore next 5 */
178- for ( const child of deferredFragmentNode . children ) {
179- if ( isDeferredFragmentNode ( child ) ) {
180- this . removeDeferredFragment ( child . deferredFragmentRecord ) ;
181- }
182- }
148+ this . _removeRootNode ( deferredFragmentRecord ) ;
183149 return true ;
184150 }
185151
186152 removeStream ( streamRecord : StreamRecord ) : void {
187153 this . _removeRootNode ( streamRecord ) ;
188154 }
189155
190- private _removeRootNode ( subsequentResultNode : SubsequentResultNode ) : void {
191- this . _rootNodes . delete ( subsequentResultNode ) ;
156+ private _removeRootNode (
157+ subsequentResultRecord : SubsequentResultRecord ,
158+ ) : void {
159+ this . _rootNodes . delete ( subsequentResultRecord ) ;
192160 if ( this . _rootNodes . size === 0 ) {
193161 for ( const resolve of this . _nextQueue ) {
194162 resolve ( { value : undefined , done : true } ) ;
@@ -199,16 +167,16 @@ export class IncrementalGraph {
199167 private _addIncrementalDataRecords (
200168 incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
201169 parents : ReadonlyArray < DeferredFragmentRecord > | undefined ,
202- initialResultChildren ?: Set < SubsequentResultNode > | undefined ,
170+ initialResultChildren ?: Set < SubsequentResultRecord > | undefined ,
203171 ) : void {
204172 for ( const incrementalDataRecord of incrementalDataRecords ) {
205173 if ( isDeferredGroupedFieldSetRecord ( incrementalDataRecord ) ) {
206174 for ( const deferredFragmentRecord of incrementalDataRecord . deferredFragmentRecords ) {
207- const deferredFragmentNode = this . _addDeferredFragmentNode (
175+ this . _addDeferredFragment (
208176 deferredFragmentRecord ,
209177 initialResultChildren ,
210178 ) ;
211- deferredFragmentNode . deferredGroupedFieldSetRecords . add (
179+ deferredFragmentRecord . deferredGroupedFieldSetRecords . add (
212180 incrementalDataRecord ,
213181 ) ;
214182 }
@@ -220,33 +188,29 @@ export class IncrementalGraph {
220188 initialResultChildren . add ( incrementalDataRecord ) ;
221189 } else {
222190 for ( const parent of parents ) {
223- const deferredFragmentNode = this . _addDeferredFragmentNode (
224- parent ,
225- initialResultChildren ,
226- ) ;
227- deferredFragmentNode . children . add ( incrementalDataRecord ) ;
191+ this . _addDeferredFragment ( parent , initialResultChildren ) ;
192+ parent . children . add ( incrementalDataRecord ) ;
228193 }
229194 }
230195 }
231196 }
232197
233198 private _promoteNonEmptyToRoot (
234- maybeEmptyNewRootNodes : Set < SubsequentResultNode > ,
199+ maybeEmptyNewRootNodes : Set < SubsequentResultRecord > ,
235200 ) : ReadonlyArray < SubsequentResultRecord > {
236201 const newRootNodes : Array < SubsequentResultRecord > = [ ] ;
237202 for ( const node of maybeEmptyNewRootNodes ) {
238- if ( isDeferredFragmentNode ( node ) ) {
203+ if ( isDeferredFragmentRecord ( node ) ) {
239204 if ( node . deferredGroupedFieldSetRecords . size > 0 ) {
240205 for ( const deferredGroupedFieldSetRecord of node . deferredGroupedFieldSetRecords ) {
241206 if ( ! this . _completesRootNode ( deferredGroupedFieldSetRecord ) ) {
242207 this . _onDeferredGroupedFieldSet ( deferredGroupedFieldSetRecord ) ;
243208 }
244209 }
245210 this . _rootNodes . add ( node ) ;
246- newRootNodes . push ( node . deferredFragmentRecord ) ;
211+ newRootNodes . push ( node ) ;
247212 continue ;
248213 }
249- this . _deferredFragmentNodes . delete ( node . deferredFragmentRecord ) ;
250214 for ( const child of node . children ) {
251215 maybeEmptyNewRootNodes . add ( child ) ;
252216 }
@@ -264,53 +228,26 @@ export class IncrementalGraph {
264228 private _completesRootNode (
265229 deferredGroupedFieldSetRecord : DeferredGroupedFieldSetRecord ,
266230 ) : boolean {
267- return this . _fragmentsToNodes (
268- deferredGroupedFieldSetRecord . deferredFragmentRecords ,
269- ) . some ( ( node ) => this . _rootNodes . has ( node ) ) ;
270- }
271-
272- private _fragmentsToNodes (
273- deferredFragmentRecords : ReadonlyArray < DeferredFragmentRecord > ,
274- ) : Array < DeferredFragmentNode > {
275- return deferredFragmentRecords
276- . map ( ( deferredFragmentRecord ) =>
277- this . _deferredFragmentNodes . get ( deferredFragmentRecord ) ,
278- )
279- . filter < DeferredFragmentNode > ( isDeferredFragmentNode ) ;
231+ return deferredGroupedFieldSetRecord . deferredFragmentRecords . some (
232+ ( deferredFragmentRecord ) => this . _rootNodes . has ( deferredFragmentRecord ) ,
233+ ) ;
280234 }
281235
282- private _addDeferredFragmentNode (
236+ private _addDeferredFragment (
283237 deferredFragmentRecord : DeferredFragmentRecord ,
284- initialResultChildren : Set < SubsequentResultNode > | undefined ,
285- ) : DeferredFragmentNode {
286- let deferredFragmentNode = this . _deferredFragmentNodes . get (
287- deferredFragmentRecord ,
288- ) ;
289- if ( deferredFragmentNode !== undefined ) {
290- return deferredFragmentNode ;
238+ initialResultChildren : Set < SubsequentResultRecord > | undefined ,
239+ ) : void {
240+ if ( this . _rootNodes . has ( deferredFragmentRecord ) ) {
241+ return ;
291242 }
292- deferredFragmentNode = {
293- deferredFragmentRecord,
294- deferredGroupedFieldSetRecords : new Set ( ) ,
295- reconcilableResults : new Set ( ) ,
296- children : new Set ( ) ,
297- } ;
298- this . _deferredFragmentNodes . set (
299- deferredFragmentRecord ,
300- deferredFragmentNode ,
301- ) ;
302243 const parent = deferredFragmentRecord . parent ;
303244 if ( parent === undefined ) {
304245 invariant ( initialResultChildren !== undefined ) ;
305- initialResultChildren . add ( deferredFragmentNode ) ;
306- return deferredFragmentNode ;
246+ initialResultChildren . add ( deferredFragmentRecord ) ;
247+ return ;
307248 }
308- const parentNode = this . _addDeferredFragmentNode (
309- parent ,
310- initialResultChildren ,
311- ) ;
312- parentNode . children . add ( deferredFragmentNode ) ;
313- return deferredFragmentNode ;
249+ parent . children . add ( deferredFragmentRecord ) ;
250+ this . _addDeferredFragment ( parent , initialResultChildren ) ;
314251 }
315252
316253 private _onDeferredGroupedFieldSet (
0 commit comments