Skip to content

Commit 4dd03e3

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

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);
@@ -527,9 +529,15 @@ export function resolveModelToJSON(
527529
describeKeyForErrorMessage(key),
528530
describeObjectForErrorMessage(parent),
529531
);
532+
const writtenSymbols = request.writtenSymbols;
533+
const existingId = writtenSymbols.get(value);
534+
if (existingId !== undefined) {
535+
return serializeByValueID(existingId);
536+
}
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)