Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jun 19, 2024
1 parent 049eae4 commit 0c05f32
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/execution/IncrementalGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type SubsequentResultNode = DeferredFragmentNode | StreamRecord;
* @internal
*/
export class IncrementalGraph {
private _pending: Set<SubsequentResultNode>;
private _rootNodes: Set<SubsequentResultNode>;
private _deferredFragmentNodes: Map<
DeferredFragmentRecord,
DeferredFragmentNode
Expand All @@ -48,7 +48,7 @@ export class IncrementalGraph {
>;

constructor() {
this._pending = new Set();
this._rootNodes = new Set();
this._deferredFragmentNodes = new Map();
this._completedQueue = [];
this._nextQueue = [];
Expand All @@ -57,13 +57,13 @@ export class IncrementalGraph {
getNewPending(
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>,
): ReadonlyArray<SubsequentResultRecord> {
const newPending = new Set<SubsequentResultNode>();
const initialResultChildren = new Set<SubsequentResultNode>();
this._addIncrementalDataRecords(
incrementalDataRecords,
undefined,
newPending,
initialResultChildren,
);
return this._pendingNodesToResults(newPending);
return this._promoteNonEmptyToRoot(initialResultChildren);
}

addCompletedReconcilableDeferredGroupedFieldSet(
Expand Down Expand Up @@ -122,7 +122,7 @@ export class IncrementalGraph {
}

hasNext(): boolean {
return this._pending.size > 0;
return this._rootNodes.size > 0;
}

completeDeferredFragment(deferredFragmentRecord: DeferredFragmentRecord):
Expand All @@ -137,7 +137,7 @@ export class IncrementalGraph {
// TODO: add test case?
/* c8 ignore next 3 */
if (deferredFragmentNode === undefined) {
return undefined;
return;
}
if (deferredFragmentNode.deferredGroupedFieldSetRecords.size > 0) {
return;
Expand All @@ -156,7 +156,7 @@ export class IncrementalGraph {
);
}
}
const newPending = this._pendingNodesToResults(
const newPending = this._promoteNonEmptyToRoot(
deferredFragmentNode.children,
);
return { newPending, reconcilableResults };
Expand Down Expand Up @@ -188,8 +188,8 @@ export class IncrementalGraph {
}

private _removePending(subsequentResultNode: SubsequentResultNode): void {
this._pending.delete(subsequentResultNode);
if (this._pending.size === 0) {
this._rootNodes.delete(subsequentResultNode);
if (this._rootNodes.size === 0) {
for (const resolve of this._nextQueue) {
resolve({ value: undefined, done: true });
}
Expand All @@ -199,14 +199,14 @@ export class IncrementalGraph {
private _addIncrementalDataRecords(
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>,
parents: ReadonlyArray<DeferredFragmentRecord> | undefined,
newPending?: Set<SubsequentResultNode> | undefined,
initialResultChildren?: Set<SubsequentResultNode> | undefined,
): void {
for (const incrementalDataRecord of incrementalDataRecords) {
if (isDeferredGroupedFieldSetRecord(incrementalDataRecord)) {
for (const deferredFragmentRecord of incrementalDataRecord.deferredFragmentRecords) {
const deferredFragmentNode = this._addDeferredFragmentNode(
deferredFragmentRecord,
newPending,
initialResultChildren,
);
deferredFragmentNode.deferredGroupedFieldSetRecords.add(
incrementalDataRecord,
Expand All @@ -216,21 +216,21 @@ export class IncrementalGraph {
this._onDeferredGroupedFieldSet(incrementalDataRecord);
}
} else if (parents === undefined) {
invariant(newPending !== undefined);
newPending.add(incrementalDataRecord);
invariant(initialResultChildren !== undefined);
initialResultChildren.add(incrementalDataRecord);
} else {
for (const parent of parents) {
const deferredFragmentNode = this._addDeferredFragmentNode(
parent,
newPending,
initialResultChildren,
);
deferredFragmentNode.children.add(incrementalDataRecord);
}
}
}
}

private _pendingNodesToResults(
private _promoteNonEmptyToRoot(
newPendingNodes: Set<SubsequentResultNode>,
): ReadonlyArray<SubsequentResultRecord> {
const newPendingResults: Array<SubsequentResultRecord> = [];
Expand All @@ -242,7 +242,7 @@ export class IncrementalGraph {
this._onDeferredGroupedFieldSet(deferredGroupedFieldSetRecord);
}
}
this._pending.add(node);
this._rootNodes.add(node);
newPendingResults.push(node.deferredFragmentRecord);
continue;
}
Expand All @@ -251,7 +251,7 @@ export class IncrementalGraph {
newPendingNodes.add(child);
}
} else {
this._pending.add(node);
this._rootNodes.add(node);
newPendingResults.push(node);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand All @@ -266,7 +266,7 @@ export class IncrementalGraph {
): boolean {
return this._fragmentsToNodes(
deferredGroupedFieldSetRecord.deferredFragmentRecords,
).some((node) => this._pending.has(node));
).some((node) => this._rootNodes.has(node));
}

private _fragmentsToNodes(
Expand All @@ -281,7 +281,7 @@ export class IncrementalGraph {

private _addDeferredFragmentNode(
deferredFragmentRecord: DeferredFragmentRecord,
newPending: Set<SubsequentResultNode> | undefined,
initialResultChildren: Set<SubsequentResultNode> | undefined,
): DeferredFragmentNode {
let deferredFragmentNode = this._deferredFragmentNodes.get(
deferredFragmentRecord,
Expand All @@ -301,11 +301,14 @@ export class IncrementalGraph {
);
const parent = deferredFragmentRecord.parent;
if (parent === undefined) {
invariant(newPending !== undefined);
newPending.add(deferredFragmentNode);
invariant(initialResultChildren !== undefined);
initialResultChildren.add(deferredFragmentNode);
return deferredFragmentNode;
}
const parentNode = this._addDeferredFragmentNode(parent, newPending);
const parentNode = this._addDeferredFragmentNode(
parent,
initialResultChildren,
);
parentNode.children.add(deferredFragmentNode);
return deferredFragmentNode;
}
Expand Down

0 comments on commit 0c05f32

Please sign in to comment.