Skip to content

Commit

Permalink
Clean up inconsistent naming in Flight implementation, part 2
Browse files Browse the repository at this point in the history
After `ReactModel` has been renamed to `ReactClientValue` in #26351, I
think we should also rename the params, variables and functions that
handle this type accordingly from `model` to `value`.
  • Loading branch information
unstubbable committed Mar 9, 2023
1 parent 5392e75 commit a13bf97
Show file tree
Hide file tree
Showing 23 changed files with 212 additions and 209 deletions.
10 changes: 5 additions & 5 deletions fixtures/flight-browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ <h1>Flight Example</h1>
return 'Title';
}

let model = {
let value = {
title: <Title />,
content: <HTML />,
};

let stream = ReactServerDOMServer.renderToReadableStream(model);
let stream = ReactServerDOMServer.renderToReadableStream(value);
let response = new Response(stream, {
headers: {'Content-Type': 'text/html'},
});
Expand All @@ -83,12 +83,12 @@ <h1>Flight Example</h1>
}

function Shell({ data }) {
let model = React.use(data);
let value = React.use(data);
return <div>
<Suspense fallback="...">
<h1>{model.title}</h1>
<h1>{value.title}</h1>
</Suspense>
{model.content}
{value.content}
</div>;
}

Expand Down
98 changes: 49 additions & 49 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {LazyComponent} from 'react/src/ReactLazy';
import type {
ClientReference,
ClientReferenceMetadata,
UninitializedModel,
UninitializedValue,
Response,
SSRManifest,
} from './ReactFlightClientHostConfig';
Expand All @@ -22,7 +22,7 @@ import {
resolveClientReference,
preloadModule,
requireModule,
parseModel,
parseValue,
} from './ReactFlightClientHostConfig';

import {REACT_LAZY_TYPE, REACT_ELEMENT_TYPE} from 'shared/ReactSymbols';
Expand All @@ -41,7 +41,7 @@ export type JSONValue =

const PENDING = 'pending';
const BLOCKED = 'blocked';
const RESOLVED_MODEL = 'resolved_model';
const RESOLVED_VALUE = 'resolved_value';
const RESOLVED_MODULE = 'resolved_module';
const INITIALIZED = 'fulfilled';
const ERRORED = 'rejected';
Expand All @@ -60,9 +60,9 @@ type BlockedChunk<T> = {
_response: Response,
then(resolve: (T) => mixed, reject: (mixed) => mixed): void,
};
type ResolvedModelChunk<T> = {
status: 'resolved_model',
value: UninitializedModel,
type ResolvedValueChunk<T> = {
status: 'resolved_value',
value: UninitializedValue,
reason: null,
_response: Response,
then(resolve: (T) => mixed, reject: (mixed) => mixed): void,
Expand Down Expand Up @@ -91,7 +91,7 @@ type ErroredChunk<T> = {
type SomeChunk<T> =
| PendingChunk<T>
| BlockedChunk<T>
| ResolvedModelChunk<T>
| ResolvedValueChunk<T>
| ResolvedModuleChunk<T>
| InitializedChunk<T>
| ErroredChunk<T>;
Expand All @@ -115,8 +115,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_MODEL:
initializeModelChunk(chunk);
case RESOLVED_VALUE:
initializeValueChunk(chunk);
break;
case RESOLVED_MODULE:
initializeModuleChunk(chunk);
Expand Down Expand Up @@ -161,8 +161,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_MODEL:
initializeModelChunk(chunk);
case RESOLVED_VALUE:
initializeValueChunk(chunk);
break;
case RESOLVED_MODULE:
initializeModuleChunk(chunk);
Expand Down Expand Up @@ -247,12 +247,12 @@ function triggerErrorOnChunk<T>(chunk: SomeChunk<T>, error: mixed): void {
}
}

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

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

function resolveModelChunk<T>(
function resolveValueChunk<T>(
chunk: SomeChunk<T>,
value: UninitializedModel,
value: UninitializedValue,
): void {
if (chunk.status !== PENDING) {
// We already resolved. We didn't expect to see this.
return;
}
const resolveListeners = chunk.value;
const rejectListeners = chunk.reason;
const resolvedChunk: ResolvedModelChunk<T> = (chunk: any);
resolvedChunk.status = RESOLVED_MODEL;
const resolvedChunk: ResolvedValueChunk<T> = (chunk: any);
resolvedChunk.status = RESOLVED_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.
initializeModelChunk(resolvedChunk);
initializeValueChunk(resolvedChunk);
// The status might have changed after initialization.
wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
}
Expand All @@ -305,20 +305,20 @@ function resolveModuleChunk<T>(
}
}

let initializingChunk: ResolvedModelChunk<any> = (null: any);
let initializingChunkBlockedModel: null | {deps: number, value: any} = null;
function initializeModelChunk<T>(chunk: ResolvedModelChunk<T>): void {
let initializingChunk: ResolvedValueChunk<any> = (null: any);
let initializingChunkBlockedValue: null | {deps: number, value: any} = null;
function initializeValueChunk<T>(chunk: ResolvedValueChunk<T>): void {
const prevChunk = initializingChunk;
const prevBlocked = initializingChunkBlockedModel;
const prevBlocked = initializingChunkBlockedValue;
initializingChunk = chunk;
initializingChunkBlockedModel = null;
initializingChunkBlockedValue = null;
try {
const value: T = parseModel(chunk._response, chunk.value);
const value: T = parseValue(chunk._response, chunk.value);
if (
initializingChunkBlockedModel !== null &&
initializingChunkBlockedModel.deps > 0
initializingChunkBlockedValue !== null &&
initializingChunkBlockedValue.deps > 0
) {
initializingChunkBlockedModel.value = value;
initializingChunkBlockedValue.value = value;
// We discovered new dependencies on modules that are not yet resolved.
// We have to go the BLOCKED state until they're resolved.
const blockedChunk: BlockedChunk<T> = (chunk: any);
Expand All @@ -336,7 +336,7 @@ function initializeModelChunk<T>(chunk: ResolvedModelChunk<T>): void {
erroredChunk.reason = error;
} finally {
initializingChunk = prevChunk;
initializingChunkBlockedModel = prevBlocked;
initializingChunkBlockedValue = prevBlocked;
}
}

Expand Down Expand Up @@ -434,17 +434,17 @@ function getChunk(response: Response, id: number): SomeChunk<any> {
return chunk;
}

function createModelResolver<T>(
function createValueResolver<T>(
chunk: SomeChunk<T>,
parentObject: Object,
key: string,
): (value: any) => void {
let blocked;
if (initializingChunkBlockedModel) {
blocked = initializingChunkBlockedModel;
if (initializingChunkBlockedValue) {
blocked = initializingChunkBlockedValue;
blocked.deps++;
} else {
blocked = initializingChunkBlockedModel = {
blocked = initializingChunkBlockedValue = {
deps: 1,
value: null,
};
Expand All @@ -467,7 +467,7 @@ function createModelResolver<T>(
};
}

function createModelReject<T>(chunk: SomeChunk<T>): (error: mixed) => void {
function createValueReject<T>(chunk: SomeChunk<T>): (error: mixed) => void {
return (error: mixed) => triggerErrorOnChunk(chunk, error);
}

Expand Down Expand Up @@ -498,7 +498,7 @@ function createServerReferenceProxy<A: Iterable<any>, T>(
return proxy;
}

export function parseModelString(
export function parseValueString(
response: Response,
parentObject: Object,
key: string,
Expand Down Expand Up @@ -541,8 +541,8 @@ export function parseModelString(
const id = parseInt(value.substring(2), 16);
const chunk = getChunk(response, id);
switch (chunk.status) {
case RESOLVED_MODEL:
initializeModelChunk(chunk);
case RESOLVED_VALUE:
initializeValueChunk(chunk);
break;
}
// The status might have changed after initialization.
Expand All @@ -561,8 +561,8 @@ export function parseModelString(
const id = parseInt(value.substring(1), 16);
const chunk = getChunk(response, id);
switch (chunk.status) {
case RESOLVED_MODEL:
initializeModelChunk(chunk);
case RESOLVED_VALUE:
initializeValueChunk(chunk);
break;
case RESOLVED_MODULE:
initializeModuleChunk(chunk);
Expand All @@ -576,8 +576,8 @@ export function parseModelString(
case BLOCKED:
const parentChunk = initializingChunk;
chunk.then(
createModelResolver(parentChunk, parentObject, key),
createModelReject(parentChunk),
createValueResolver(parentChunk, parentObject, key),
createValueReject(parentChunk),
);
return null;
default:
Expand All @@ -589,7 +589,7 @@ export function parseModelString(
return value;
}

export function parseModelTuple(
export function parseValueTuple(
response: Response,
value: {+[key: string]: JSONValue} | $ReadOnlyArray<JSONValue>,
): any {
Expand Down Expand Up @@ -626,27 +626,27 @@ export function createResponse(
export function resolveModel(
response: Response,
id: number,
model: UninitializedModel,
value: UninitializedValue,
): void {
const chunks = response._chunks;
const chunk = chunks.get(id);
if (!chunk) {
chunks.set(id, createResolvedModelChunk(response, model));
chunks.set(id, createResolvedValueChunk(response, value));
} else {
resolveModelChunk(chunk, model);
resolveValueChunk(chunk, value);
}
}

export function resolveModule(
response: Response,
id: number,
model: UninitializedModel,
value: UninitializedValue,
): void {
const chunks = response._chunks;
const chunk = chunks.get(id);
const clientReferenceMetadata: ClientReferenceMetadata = parseModel(
const clientReferenceMetadata: ClientReferenceMetadata = parseValue(
response,
model,
value,
);
const clientReference = resolveClientReference<$FlowFixMe>(
response._ssrManifest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export type Response = ResponseBase & {
_stringDecoder: StringDecoder,
};

export type UninitializedModel = string;
export type UninitializedValue = string;

export function parseModel<T>(response: Response, json: UninitializedModel): T {
export function parseValue<T>(response: Response, json: UninitializedValue): T {
return JSON.parse(json, response._fromJSON);
}
8 changes: 4 additions & 4 deletions packages/react-client/src/ReactFlightClientStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {
resolveErrorProd,
resolveErrorDev,
createResponse as createResponseBase,
parseModelString,
parseModelTuple,
parseValueString,
parseValueTuple,
} from './ReactFlightClient';

import {
Expand Down Expand Up @@ -111,10 +111,10 @@ function createFromJSONCallback(response: Response) {
return function (key: string, value: JSONValue) {
if (typeof value === 'string') {
// We can't use .bind here because we need the "this" value.
return parseModelString(response, this, key, value);
return parseValueString(response, this, key, value);
}
if (typeof value === 'object' && value !== null) {
return parseModelTuple(response, value);
return parseValueTuple(response, value);
}
return value;
};
Expand Down
Loading

0 comments on commit a13bf97

Please sign in to comment.