Skip to content

Commit

Permalink
use map instead of mutating
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jun 19, 2024
1 parent e160b6f commit 68fa5ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ interface SubsequentIncrementalExecutionResultContext {
*/
class IncrementalPublisher {
private _context: IncrementalPublisherContext;
private _ids: Map<SubsequentResultRecord, string>;
private _nextId: number;
private _incrementalGraph: IncrementalGraph;

constructor(context: IncrementalPublisherContext) {
this._context = context;
this._ids = new Map();
this._nextId = 0;
this._incrementalGraph = new IncrementalGraph();
}
Expand Down Expand Up @@ -96,7 +98,7 @@ class IncrementalPublisher {
const pendingResults: Array<PendingResult> = [];
for (const pendingSource of newPending) {
const id = String(this._getNextId());
pendingSource.id = id;
this._ids.set(pendingSource, id);
const pendingResult: PendingResult = {
id,
path: pathToArray(pendingSource.path),
Expand Down Expand Up @@ -232,14 +234,15 @@ class IncrementalPublisher {
) {
for (const deferredFragmentRecord of deferredGroupedFieldSetResult
.deferredGroupedFieldSetRecord.deferredFragmentRecords) {
const id = deferredFragmentRecord.id;
if (
!this._incrementalGraph.removeDeferredFragment(deferredFragmentRecord)
) {
// This can occur if multiple deferred grouped field sets error for a fragment.
continue;
}
const id = this._ids.get(deferredFragmentRecord);
invariant(id !== undefined);
this._ids.delete(deferredFragmentRecord);
context.completed.push({
id,
errors: deferredGroupedFieldSetResult.errors,
Expand All @@ -265,7 +268,7 @@ class IncrementalPublisher {
if (reconcilableResults === undefined) {
continue;
}
const id = deferredFragmentRecord.id;
const id = this._ids.get(deferredFragmentRecord);
invariant(id !== undefined);
const incremental = context.incremental;
for (const reconcilableResult of reconcilableResults) {
Expand All @@ -283,6 +286,7 @@ class IncrementalPublisher {
}
incremental.push(incrementalEntry);
}
this._ids.delete(deferredFragmentRecord);
context.completed.push({ id });
}
}
Expand All @@ -292,9 +296,10 @@ class IncrementalPublisher {
context: SubsequentIncrementalExecutionResultContext,
): void {
const streamRecord = streamItemsResult.streamRecord;
const id = streamRecord.id;
const id = this._ids.get(streamRecord);
invariant(id !== undefined);
if (streamItemsResult.errors !== undefined) {
this._ids.delete(streamRecord);
context.completed.push({
id,
errors: streamItemsResult.errors,
Expand All @@ -309,6 +314,7 @@ class IncrementalPublisher {
});
}
} else if (streamItemsResult.result === undefined) {
this._ids.delete(streamRecord);
context.completed.push({ id });
this._incrementalGraph.removeStream(streamRecord);
if (isCancellableStreamRecord(streamRecord)) {
Expand Down Expand Up @@ -344,7 +350,7 @@ class IncrementalPublisher {
if (deferredFragmentRecord === initialDeferredFragmentRecord) {
continue;
}
const id = deferredFragmentRecord.id;
const id = this._ids.get(deferredFragmentRecord);
// TODO: add test case for when an fragment has not been released, but might be processed for the shortest path.
/* c8 ignore next 3 */
if (id === undefined) {
Expand Down
2 changes: 0 additions & 2 deletions src/execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ export type SubsequentResultRecord = DeferredFragmentRecord | StreamRecord;
export interface DeferredFragmentRecord {
path: Path | undefined;
label: string | undefined;
id?: string | undefined;
parent: DeferredFragmentRecord | undefined;
}

Expand All @@ -232,7 +231,6 @@ export type StreamItemRecord = ThunkIncrementalResult<StreamItemResult>;
export interface StreamRecord {
path: Path;
label: string | undefined;
id?: string | undefined;
streamItemQueue: Array<StreamItemRecord>;
}

Expand Down

0 comments on commit 68fa5ab

Please sign in to comment.