Skip to content

Commit 7b6319f

Browse files
committed
use map instead of mutating
1 parent 32b97d9 commit 7b6319f

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,
@@ -265,7 +268,7 @@ class IncrementalPublisher {
265268
if (reconcilableResults === undefined) {
266269
continue;
267270
}
268-
const id = deferredFragmentRecord.id;
271+
const id = this._ids.get(deferredFragmentRecord);
269272
invariant(id !== undefined);
270273
const incremental = context.incremental;
271274
for (const reconcilableResult of reconcilableResults) {
@@ -283,6 +286,7 @@ class IncrementalPublisher {
283286
}
284287
incremental.push(incrementalEntry);
285288
}
289+
this._ids.delete(deferredFragmentRecord);
286290
context.completed.push({ id });
287291
}
288292
}
@@ -292,9 +296,10 @@ class IncrementalPublisher {
292296
context: SubsequentIncrementalExecutionResultContext,
293297
): void {
294298
const streamRecord = streamItemsResult.streamRecord;
295-
const id = streamRecord.id;
299+
const id = this._ids.get(streamRecord);
296300
invariant(id !== undefined);
297301
if (streamItemsResult.errors !== undefined) {
302+
this._ids.delete(streamRecord);
298303
context.completed.push({
299304
id,
300305
errors: streamItemsResult.errors,
@@ -309,6 +314,7 @@ class IncrementalPublisher {
309314
});
310315
}
311316
} else if (streamItemsResult.result === undefined) {
317+
this._ids.delete(streamRecord);
312318
context.completed.push({ id });
313319
this._incrementalGraph.removeStream(streamRecord);
314320
if (isCancellableStreamRecord(streamRecord)) {
@@ -344,7 +350,7 @@ class IncrementalPublisher {
344350
if (deferredFragmentRecord === initialDeferredFragmentRecord) {
345351
continue;
346352
}
347-
const id = deferredFragmentRecord.id;
353+
const id = this._ids.get(deferredFragmentRecord);
348354
// TODO: add test case for when an fragment has not been released, but might be processed for the shortest path.
349355
/* c8 ignore next 3 */
350356
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)