Skip to content

Commit 1bc5318

Browse files
committed
use map instead of mutating
1 parent ed321bb commit 1bc5318

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/execution/IncrementalPublisher.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ interface SubsequentIncrementalExecutionResultContext {
6060
*/
6161
class IncrementalPublisher {
6262
private _context: IncrementalPublisherContext;
63+
private _ids: Map<SubsequentResultRecord, string>;
6364
private _nextId: number;
6465
private _incrementalGraph: IncrementalGraph;
6566

6667
constructor(context: IncrementalPublisherContext) {
6768
this._context = context;
69+
this._ids = new Map();
6870
this._nextId = 0;
6971
this._incrementalGraph = new IncrementalGraph();
7072
}
@@ -96,7 +98,7 @@ class IncrementalPublisher {
9698
const pendingResults: Array<PendingResult> = [];
9799
for (const pendingSource of newPending) {
98100
const id = String(this._getNextId());
99-
pendingSource.id = id;
101+
this._ids.set(pendingSource, id);
100102
const pendingResult: PendingResult = {
101103
id,
102104
path: pathToArray(pendingSource.path),
@@ -232,14 +234,15 @@ class IncrementalPublisher {
232234
) {
233235
for (const deferredFragmentRecord of deferredGroupedFieldSetResult
234236
.deferredGroupedFieldSetRecord.deferredFragmentRecords) {
235-
const id = deferredFragmentRecord.id;
236237
if (
237238
!this._incrementalGraph.removeDeferredFragment(deferredFragmentRecord)
238239
) {
239240
// This can occur if multiple deferred grouped field sets error for a fragment.
240241
continue;
241242
}
243+
const id = this._ids.get(deferredFragmentRecord);
242244
invariant(id !== undefined);
245+
this._ids.delete(deferredFragmentRecord);
243246
context.completed.push({
244247
id,
245248
errors: deferredGroupedFieldSetResult.errors,
@@ -266,7 +269,7 @@ class IncrementalPublisher {
266269
if (reconcilableResults === undefined) {
267270
continue;
268271
}
269-
const id = deferredFragmentRecord.id;
272+
const id = this._ids.get(deferredFragmentRecord);
270273
invariant(id !== undefined);
271274
const incremental = context.incremental;
272275
for (const reconcilableResult of reconcilableResults) {
@@ -284,6 +287,7 @@ class IncrementalPublisher {
284287
}
285288
incremental.push(incrementalEntry);
286289
}
290+
this._ids.delete(deferredFragmentRecord);
287291
context.completed.push({ id });
288292
}
289293
}
@@ -293,9 +297,10 @@ class IncrementalPublisher {
293297
context: SubsequentIncrementalExecutionResultContext,
294298
): void {
295299
const streamRecord = streamItemsResult.streamRecord;
296-
const id = streamRecord.id;
300+
const id = this._ids.get(streamRecord);
297301
invariant(id !== undefined);
298302
if (streamItemsResult.errors !== undefined) {
303+
this._ids.delete(streamRecord);
299304
context.completed.push({
300305
id,
301306
errors: streamItemsResult.errors,
@@ -310,6 +315,7 @@ class IncrementalPublisher {
310315
});
311316
}
312317
} else if (streamItemsResult.result === undefined) {
318+
this._ids.delete(streamRecord);
313319
context.completed.push({ id });
314320
this._incrementalGraph.removeStream(streamRecord);
315321
if (isCancellableStreamRecord(streamRecord)) {
@@ -345,7 +351,7 @@ class IncrementalPublisher {
345351
if (deferredFragmentRecord === initialDeferredFragmentRecord) {
346352
continue;
347353
}
348-
const id = deferredFragmentRecord.id;
354+
const id = this._ids.get(deferredFragmentRecord);
349355
// TODO: add test case for when an fragment has not been released, but might be processed for the shortest path.
350356
/* c8 ignore next 3 */
351357
if (id === undefined) {

src/execution/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ export type SubsequentResultRecord = DeferredFragmentRecord | StreamRecord;
217217
export interface DeferredFragmentRecord {
218218
path: Path | undefined;
219219
label: string | undefined;
220-
id?: string | undefined;
221220
parent: DeferredFragmentRecord | undefined;
222221
}
223222

@@ -232,7 +231,6 @@ export type StreamItemRecord = ThunkIncrementalResult<StreamItemResult>;
232231
export interface StreamRecord {
233232
path: Path;
234233
label: string | undefined;
235-
id?: string | undefined;
236234
streamItemQueue: Array<StreamItemRecord>;
237235
}
238236

0 commit comments

Comments
 (0)