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 29b9646 commit 56c3960
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 @@ -78,6 +78,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 @@ -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);
Expand Down Expand Up @@ -517,6 +519,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,
Expand All @@ -530,6 +537,7 @@ export function resolveModelToJSON(
request.pendingChunks++;
const symbolId = request.nextChunkId++;
emitSymbolChunk(request, symbolId, name);
writtenSymbols.set(value, symbolId);
return serializeByValueID(symbolId);
}

Expand Down

0 comments on commit 56c3960

Please sign in to comment.