Skip to content

Commit

Permalink
Reuse symbol ids that have already been written earlier in the stream
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Nov 6, 2020
1 parent 5d91fed commit 1ffb19e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export type Request = {
completedModuleChunks: Array<Chunk>,
completedJSONChunks: Array<Chunk>,
completedErrorChunks: Array<Chunk>,
writtenSymbols: Map<Symbol, number>,
flowing: boolean,
toJSON: (key: string, value: ReactModel) => ReactJSONValue,
};
Expand All @@ -100,6 +101,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);
Expand Down Expand Up @@ -528,9 +530,15 @@ export function resolveModelToJSON(
describeKeyForErrorMessage(key),
describeObjectForErrorMessage(parent),
);
const writtenSymbols = request.writtenSymbols;
const existingId = writtenSymbols.get(value);
if (existingId !== undefined) {
return serializeByValueID(existingId);
}
request.pendingChunks++;
const symbolId = request.nextChunkId++;
emitSymbolChunk(request, symbolId, name);
writtenSymbols.set(value, symbolId);
return serializeByValueID(symbolId);
}

Expand Down

0 comments on commit 1ffb19e

Please sign in to comment.