Skip to content

Commit

Permalink
Rename RESOLVED_VALUE to RESOLVED_JSON_VALUE in Flight client & F…
Browse files Browse the repository at this point in the history
…light Reply server
  • Loading branch information
unstubbable committed Mar 13, 2023
1 parent f7deeb2 commit 36e576f
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 69 deletions.
54 changes: 27 additions & 27 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
resolveClientReference,
preloadModule,
requireModule,
parseValue,
parseJSONValue,
} from './ReactFlightClientHostConfig';

import {knownServerReferences} from './ReactFlightServerReferenceRegistry';
Expand All @@ -43,7 +43,7 @@ export type JSONValue =

const PENDING = 'pending';
const BLOCKED = 'blocked';
const RESOLVED_VALUE = 'resolved_value';
const RESOLVED_JSON_VALUE = 'resolved_json_value';
const RESOLVED_MODULE = 'resolved_module';
const INITIALIZED = 'fulfilled';
const ERRORED = 'rejected';
Expand All @@ -62,8 +62,8 @@ type BlockedChunk<T> = {
_response: Response,
then(resolve: (T) => mixed, reject: (mixed) => mixed): void,
};
type ResolvedValueChunk<T> = {
status: 'resolved_value',
type ResolvedJSONValueChunk<T> = {
status: 'resolved_json_value',
value: UninitializedValue,
reason: null,
_response: Response,
Expand Down Expand Up @@ -93,7 +93,7 @@ type ErroredChunk<T> = {
type SomeChunk<T> =
| PendingChunk<T>
| BlockedChunk<T>
| ResolvedValueChunk<T>
| ResolvedJSONValueChunk<T>
| ResolvedModuleChunk<T>
| InitializedChunk<T>
| ErroredChunk<T>;
Expand All @@ -117,8 +117,8 @@ Chunk.prototype.then = function <T>(
// If we have resolved content, we try to initialize it first which
// might put us back into one of the other states.
switch (chunk.status) {
case RESOLVED_VALUE:
initializeValueChunk(chunk);
case RESOLVED_JSON_VALUE:
initializeJSONValueChunk(chunk);
break;
case RESOLVED_MODULE:
initializeModuleChunk(chunk);
Expand Down Expand Up @@ -163,8 +163,8 @@ function readChunk<T>(chunk: SomeChunk<T>): T {
// If we have resolved content, we try to initialize it first which
// might put us back into one of the other states.
switch (chunk.status) {
case RESOLVED_VALUE:
initializeValueChunk(chunk);
case RESOLVED_JSON_VALUE:
initializeJSONValueChunk(chunk);
break;
case RESOLVED_MODULE:
initializeModuleChunk(chunk);
Expand Down Expand Up @@ -249,12 +249,12 @@ function triggerErrorOnChunk<T>(chunk: SomeChunk<T>, error: mixed): void {
}
}

function createResolvedValueChunk<T>(
function createResolvedJSONValueChunk<T>(
response: Response,
value: UninitializedValue,
): ResolvedValueChunk<T> {
): ResolvedJSONValueChunk<T> {
// $FlowFixMe Flow doesn't support functions as constructors
return new Chunk(RESOLVED_VALUE, value, null, response);
return new Chunk(RESOLVED_JSON_VALUE, value, null, response);
}

function createResolvedModuleChunk<T>(
Expand All @@ -265,7 +265,7 @@ function createResolvedModuleChunk<T>(
return new Chunk(RESOLVED_MODULE, value, null, response);
}

function resolveValueChunk<T>(
function resolveJSONValueChunk<T>(
chunk: SomeChunk<T>,
value: UninitializedValue,
): void {
Expand All @@ -275,14 +275,14 @@ function resolveValueChunk<T>(
}
const resolveListeners = chunk.value;
const rejectListeners = chunk.reason;
const resolvedChunk: ResolvedValueChunk<T> = (chunk: any);
resolvedChunk.status = RESOLVED_VALUE;
const resolvedChunk: ResolvedJSONValueChunk<T> = (chunk: any);
resolvedChunk.status = RESOLVED_JSON_VALUE;
resolvedChunk.value = value;
if (resolveListeners !== null) {
// This is unfortunate that we're reading this eagerly if
// we already have listeners attached since they might no
// longer be rendered or might not be the highest pri.
initializeValueChunk(resolvedChunk);
initializeJSONValueChunk(resolvedChunk);
// The status might have changed after initialization.
wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
}
Expand All @@ -307,15 +307,15 @@ function resolveModuleChunk<T>(
}
}

let initializingChunk: ResolvedValueChunk<any> = (null: any);
let initializingChunk: ResolvedJSONValueChunk<any> = (null: any);
let initializingChunkBlockedValue: null | {deps: number, value: any} = null;
function initializeValueChunk<T>(chunk: ResolvedValueChunk<T>): void {
function initializeJSONValueChunk<T>(chunk: ResolvedJSONValueChunk<T>): void {
const prevChunk = initializingChunk;
const prevBlocked = initializingChunkBlockedValue;
initializingChunk = chunk;
initializingChunkBlockedValue = null;
try {
const value: T = parseValue(chunk._response, chunk.value);
const value: T = parseJSONValue(chunk._response, chunk.value);
if (
initializingChunkBlockedValue !== null &&
initializingChunkBlockedValue.deps > 0
Expand Down Expand Up @@ -544,8 +544,8 @@ export function parseValueString(
const id = parseInt(value.substring(2), 16);
const chunk = getChunk(response, id);
switch (chunk.status) {
case RESOLVED_VALUE:
initializeValueChunk(chunk);
case RESOLVED_JSON_VALUE:
initializeJSONValueChunk(chunk);
break;
}
// The status might have changed after initialization.
Expand All @@ -569,8 +569,8 @@ export function parseValueString(
const id = parseInt(value.substring(1), 16);
const chunk = getChunk(response, id);
switch (chunk.status) {
case RESOLVED_VALUE:
initializeValueChunk(chunk);
case RESOLVED_JSON_VALUE:
initializeJSONValueChunk(chunk);
break;
case RESOLVED_MODULE:
initializeModuleChunk(chunk);
Expand Down Expand Up @@ -631,17 +631,17 @@ export function createResponse(
return response;
}

export function resolveValue(
export function resolveJSONValue(
response: Response,
id: number,
value: UninitializedValue,
): void {
const chunks = response._chunks;
const chunk = chunks.get(id);
if (!chunk) {
chunks.set(id, createResolvedValueChunk(response, value));
chunks.set(id, createResolvedJSONValueChunk(response, value));
} else {
resolveValueChunk(chunk, value);
resolveJSONValueChunk(chunk, value);
}
}

Expand All @@ -652,7 +652,7 @@ export function resolveModule(
): void {
const chunks = response._chunks;
const chunk = chunks.get(id);
const clientReferenceMetadata: ClientReferenceMetadata = parseValue(
const clientReferenceMetadata: ClientReferenceMetadata = parseJSONValue(
response,
value,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export type Response = ResponseBase & {

export type UninitializedValue = string;

export function parseValue<T>(response: Response, json: UninitializedValue): T {
return JSON.parse(json, response._fromJSON);
export function parseJSONValue<T>(
response: Response,
value: UninitializedValue,
): T {
return JSON.parse(value, response._fromJSON);
}
4 changes: 2 additions & 2 deletions packages/react-client/src/ReactFlightClientStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {SSRManifest} from './ReactFlightClientHostConfig';

import {
resolveModule,
resolveValue,
resolveJSONValue,
resolveErrorProd,
resolveErrorDev,
createResponse as createResponseBase,
Expand Down Expand Up @@ -63,7 +63,7 @@ function processFullRow(response: Response, row: string): void {
}
default: {
// We assume anything else is JSON.
resolveValue(response, id, row.substring(colon + 1));
resolveJSONValue(response, id, row.substring(colon + 1));
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const requireModule = $$$hostConfig.requireModule;
export opaque type Source = mixed;

export type UninitializedValue = string;
export const parseValue = $$$hostConfig.parseValue;
export const parseJSONValue = $$$hostConfig.parseJSONValue;

export opaque type StringDecoder = mixed; // eslint-disable-line no-undef

Expand Down
4 changes: 2 additions & 2 deletions packages/react-noop-renderer/src/ReactNoopFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const {createResponse, processStringChunk, getRoot, close} = ReactFlightClient({
requireModule(idx: string) {
return readModule(idx);
},
parseValue(response: Response, json) {
return JSON.parse(json, response._fromJSON);
parseJSONValue(response: Response, value) {
return JSON.parse(value, response._fromJSON);
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {Response} from 'react-client/src/ReactFlightClient';

import {
createResponse,
resolveValue,
resolveJSONValue,
resolveModule,
resolveErrorDev,
resolveErrorProd,
Expand All @@ -26,7 +26,7 @@ export {createResponse, close, getRoot};
export function resolveRow(response: Response, chunk: RowEncoding): void {
if (chunk[0] === 'O') {
// $FlowFixMe unable to refine on array indices
resolveValue(response, chunk[1], chunk[2]);
resolveJSONValue(response, chunk[1], chunk[2]);
} else if (chunk[0] === 'I') {
// $FlowFixMe unable to refine on array indices
resolveModule(response, chunk[1], chunk[2]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function resolveServerReference<T>(
throw new Error('Not implemented.');
}

function parseValueRecursively(
function parseJSONValueRecursively(
response: Response,
parentObj: {+[key: string]: JSONValue} | $ReadOnlyArray<JSONValue>,
key: string,
Expand All @@ -66,7 +66,7 @@ function parseValueRecursively(
if (isArray(value)) {
const parsedValue: Array<$FlowFixMe> = [];
for (let i = 0; i < value.length; i++) {
(parsedValue: any)[i] = parseValueRecursively(
(parsedValue: any)[i] = parseJSONValueRecursively(
response,
value,
'' + i,
Expand All @@ -77,7 +77,7 @@ function parseValueRecursively(
} else {
const parsedValue = {};
for (const innerKey in value) {
(parsedValue: any)[innerKey] = parseValueRecursively(
(parsedValue: any)[innerKey] = parseJSONValueRecursively(
response,
value,
innerKey,
Expand All @@ -92,6 +92,9 @@ function parseValueRecursively(

const dummy = {};

export function parseValue<T>(response: Response, json: UninitializedValue): T {
return (parseValueRecursively(response, dummy, '', json): any);
export function parseJSONValue<T>(
response: Response,
value: UninitializedValue,
): T {
return (parseJSONValueRecursively(response, dummy, '', value): any);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {Response} from 'react-client/src/ReactFlightClient';

import {
createResponse,
resolveValue,
resolveJSONValue,
resolveModule,
resolveErrorDev,
resolveErrorProd,
Expand All @@ -26,7 +26,7 @@ export {createResponse, close, getRoot};
export function resolveRow(response: Response, chunk: RowEncoding): void {
if (chunk[0] === 'O') {
// $FlowFixMe `Chunk` doesn't flow into `JSONValue` because of the `E` row type.
resolveValue(response, chunk[1], chunk[2]);
resolveJSONValue(response, chunk[1], chunk[2]);
} else if (chunk[0] === 'I') {
// $FlowFixMe `Chunk` doesn't flow into `JSONValue` because of the `E` row type.
resolveModule(response, chunk[1], chunk[2]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function resolveServerReference<T>(
throw new Error('Not implemented.');
}

function parseValueRecursively(
function parseJSONValueRecursively(
response: Response,
parentObj: {+[key: string]: JSONValue} | $ReadOnlyArray<JSONValue>,
key: string,
Expand All @@ -66,7 +66,7 @@ function parseValueRecursively(
if (isArray(value)) {
const parsedValue: Array<$FlowFixMe> = [];
for (let i = 0; i < value.length; i++) {
(parsedValue: any)[i] = parseValueRecursively(
(parsedValue: any)[i] = parseJSONValueRecursively(
response,
value,
'' + i,
Expand All @@ -77,7 +77,7 @@ function parseValueRecursively(
} else {
const parsedValue = {};
for (const innerKey in value) {
(parsedValue: any)[innerKey] = parseValueRecursively(
(parsedValue: any)[innerKey] = parseJSONValueRecursively(
response,
value,
innerKey,
Expand All @@ -92,6 +92,9 @@ function parseValueRecursively(

const dummy = {};

export function parseValue<T>(response: Response, json: UninitializedValue): T {
return (parseValueRecursively(response, dummy, '', json): any);
export function parseJSONValue<T>(
response: Response,
json: UninitializedValue,
): T {
return (parseJSONValueRecursively(response, dummy, '', json): any);
}
Loading

0 comments on commit 36e576f

Please sign in to comment.