Skip to content

Commit

Permalink
Serialize Provider as an inline JSON string instead of special row
Browse files Browse the repository at this point in the history
We still emit it as a separate row in case it's reused but it's not needed.
  • Loading branch information
sebmarkbage committed Jan 31, 2023
1 parent d407b90 commit 70963f8
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 55 deletions.
26 changes: 3 additions & 23 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,6 @@ function createErrorChunk<T>(
return new Chunk(ERRORED, null, error, response);
}

function createInitializedChunk<T>(
response: Response,
value: T,
): InitializedChunk<T> {
// $FlowFixMe Flow doesn't support functions as constructors
return new Chunk(INITIALIZED, value, null, response);
}

function wakeChunk<T>(listeners: Array<(T) => mixed>, value: T): void {
for (let i = 0; i < listeners.length; i++) {
const listener = listeners[i];
Expand Down Expand Up @@ -504,6 +496,9 @@ export function parseModelString(
case 'S': {
return Symbol.for(value.substring(2));
}
case 'P': {
return getOrCreateServerContext(value.substring(2)).Provider;
}
default: {
// We assume that anything else is a reference ID.
const id = parseInt(value.substring(1), 16);
Expand Down Expand Up @@ -574,21 +569,6 @@ export function resolveModel(
}
}

export function resolveProvider(
response: Response,
id: number,
contextName: string,
): void {
const chunks = response._chunks;
chunks.set(
id,
createInitializedChunk(
response,
getOrCreateServerContext(contextName).Provider,
),
);
}

export function resolveModule(
response: Response,
id: number,
Expand Down
5 changes: 0 additions & 5 deletions packages/react-client/src/ReactFlightClientStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {BundlerConfig} from './ReactFlightClientHostConfig';
import {
resolveModule,
resolveModel,
resolveProvider,
resolveErrorProd,
resolveErrorDev,
createResponse as createResponseBase,
Expand Down Expand Up @@ -47,10 +46,6 @@ function processFullRow(response: Response, row: string): void {
resolveModule(response, id, row.substring(colon + 2));
return;
}
case 'P': {
resolveProvider(response, id, row.substring(colon + 2));
return;
}
case 'E': {
const errorInfo = JSON.parse(row.substring(colon + 2));
if (__DEV__) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,6 @@ export function processModuleChunk(
return ['I', id, moduleMetaData];
}

export function processProviderChunk(
request: Request,
id: number,
contextName: string,
): Chunk {
return ['P', id, contextName];
}

export function scheduleWork(callback: () => void) {
callback();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,6 @@ export function processModuleChunk(
return ['I', id, moduleMetaData];
}

export function processProviderChunk(
request: Request,
id: number,
contextName: string,
): Chunk {
return ['P', id, contextName];
}

export function scheduleWork(callback: () => void) {
callback();
}
Expand Down
8 changes: 6 additions & 2 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
closeWithError,
processModelChunk,
processModuleChunk,
processProviderChunk,
processErrorChunkProd,
processErrorChunkDev,
processReferenceChunk,
Expand Down Expand Up @@ -423,6 +422,10 @@ function serializeSymbolReference(name: string): string {
return '$S' + name;
}

function serializeProviderReference(name: string): string {
return '$P' + name;
}

function serializeClientReference(
request: Request,
parent: {+[key: string | number]: ReactModel} | $ReadOnlyArray<ReactModel>,
Expand Down Expand Up @@ -1122,7 +1125,8 @@ function emitProviderChunk(
id: number,
contextName: string,
): void {
const processedChunk = processProviderChunk(request, id, contextName);
const contextReference = serializeProviderReference(contextName);
const processedChunk = processReferenceChunk(request, id, contextReference);
request.completedJSONChunks.push(processedChunk);
}

Expand Down
9 changes: 0 additions & 9 deletions packages/react-server/src/ReactFlightServerConfigStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,6 @@ export function processModuleChunk(
return stringToChunk(row);
}

export function processProviderChunk(
request: Request,
id: number,
contextName: string,
): Chunk {
const row = serializeRowHeader('P', id) + contextName + '\n';
return stringToChunk(row);
}

export {
scheduleWork,
flushBuffered,
Expand Down

0 comments on commit 70963f8

Please sign in to comment.