From 4e71e3a564645e83b6a69bb090b80ae1ba3fd027 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Thu, 5 Nov 2020 19:45:21 -0500 Subject: [PATCH] Reuse symbol ids that have already been written earlier in the stream --- packages/react-server/src/ReactFlightServer.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/react-server/src/ReactFlightServer.js b/packages/react-server/src/ReactFlightServer.js index b3a7d13bdc8d9..34404544efbdc 100644 --- a/packages/react-server/src/ReactFlightServer.js +++ b/packages/react-server/src/ReactFlightServer.js @@ -78,6 +78,7 @@ export type Request = { completedModuleChunks: Array, completedJSONChunks: Array, completedErrorChunks: Array, + writtenSymbols: Map, flowing: boolean, toJSON: (key: string, value: ReactModel) => ReactJSONValue, }; @@ -99,6 +100,7 @@ export function createRequest( completedModuleChunks: [], completedJSONChunks: [], completedErrorChunks: [], + writtenSymbols: new Map(), flowing: false, toJSON: function(key: string, value: ReactModel): ReactJSONValue { return resolveModelToJSON(request, this, key, value); @@ -522,6 +524,11 @@ export function resolveModelToJSON( } if (typeof value === 'symbol') { + const writtenSymbols = request.writtenSymbols; + const existingId = writtenSymbols.get(value); + if (existingId !== undefined) { + return serializeByValueID(existingId); + } const name = value.description; invariant( Symbol.for(name) === value, @@ -535,6 +542,7 @@ export function resolveModelToJSON( request.pendingChunks++; const symbolId = request.nextChunkId++; emitSymbolChunk(request, symbolId, name); + writtenSymbols.set(value, symbolId); return serializeByValueID(symbolId); }