Skip to content

Commit 56c3960

Browse files
committed
Reuse symbol ids that have already been written earlier in the stream
1 parent 29b9646 commit 56c3960

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

packages/react-server/src/ReactFlightServer.js

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export type Request = {
7878
completedModuleChunks: Array<Chunk>,
7979
completedJSONChunks: Array<Chunk>,
8080
completedErrorChunks: Array<Chunk>,
81+
writtenSymbols: Map<Symbol, number>,
8182
flowing: boolean,
8283
toJSON: (key: string, value: ReactModel) => ReactJSONValue,
8384
};
@@ -99,6 +100,7 @@ export function createRequest(
99100
completedModuleChunks: [],
100101
completedJSONChunks: [],
101102
completedErrorChunks: [],
103+
writtenSymbols: new Map(),
102104
flowing: false,
103105
toJSON: function(key: string, value: ReactModel): ReactJSONValue {
104106
return resolveModelToJSON(request, this, key, value);
@@ -517,6 +519,11 @@ export function resolveModelToJSON(
517519
}
518520

519521
if (typeof value === 'symbol') {
522+
const writtenSymbols = request.writtenSymbols;
523+
const existingId = writtenSymbols.get(value);
524+
if (existingId !== undefined) {
525+
return serializeByValueID(existingId);
526+
}
520527
const name = value.description;
521528
invariant(
522529
Symbol.for(name) === value,
@@ -530,6 +537,7 @@ export function resolveModelToJSON(
530537
request.pendingChunks++;
531538
const symbolId = request.nextChunkId++;
532539
emitSymbolChunk(request, symbolId, name);
540+
writtenSymbols.set(value, symbolId);
533541
return serializeByValueID(symbolId);
534542
}
535543

0 commit comments

Comments
 (0)