Skip to content

Commit

Permalink
Move Module terminology to Client References
Browse files Browse the repository at this point in the history
Module is ambiguous since there's both client and server modules.
It also actually refers to a specific export name.
  • Loading branch information
sebmarkbage committed Feb 9, 2023
1 parent 384fb81 commit f928095
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 146 deletions.
19 changes: 11 additions & 8 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {LazyComponent} from 'react/src/ReactLazy';

import type {
ClientReference,
ModuleMetaData,
ClientReferenceMetadata,
UninitializedModel,
Response,
BundlerConfig,
Expand Down Expand Up @@ -640,16 +640,19 @@ export function resolveModule(
): void {
const chunks = response._chunks;
const chunk = chunks.get(id);
const moduleMetaData: ModuleMetaData = parseModel(response, model);
const moduleReference = resolveClientReference(
const clientReferenceMetadata: ClientReferenceMetadata = parseModel(
response,
model,
);
const clientReference = resolveClientReference(
response._bundlerConfig,
moduleMetaData,
clientReferenceMetadata,
);

// TODO: Add an option to encode modules that are lazy loaded.
// For now we preload all modules as early as possible since it's likely
// that we'll need them.
const promise = preloadModule(moduleReference);
const promise = preloadModule(clientReference);
if (promise) {
let blockedChunk: BlockedChunk<any>;
if (!chunk) {
Expand All @@ -664,16 +667,16 @@ export function resolveModule(
blockedChunk.status = BLOCKED;
}
promise.then(
() => resolveModuleChunk(blockedChunk, moduleReference),
() => resolveModuleChunk(blockedChunk, clientReference),
error => triggerErrorOnChunk(blockedChunk, error),
);
} else {
if (!chunk) {
chunks.set(id, createResolvedModuleChunk(response, moduleReference));
chunks.set(id, createResolvedModuleChunk(response, clientReference));
} else {
// This can't actually happen because we don't have any forward
// references to modules.
resolveModuleChunk(chunk, moduleReference);
resolveModuleChunk(chunk, clientReference);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare var $$$hostConfig: any;

export type Response = any;
export opaque type BundlerConfig = mixed;
export opaque type ModuleMetaData = mixed;
export opaque type ClientReferenceMetadata = mixed;
export opaque type ClientReference<T> = mixed; // eslint-disable-line no-unused-vars
export const resolveClientReference = $$$hostConfig.resolveClientReference;
export const preloadModule = $$$hostConfig.preloadModule;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-noop-renderer/src/ReactNoopFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const ReactNoopFlightServer = ReactFlightServer({
getClientReferenceKey(reference: Object): Object {
return reference;
},
resolveModuleMetaData(
resolveClientReferenceMetadata(
config: void,
reference: {$$typeof: symbol, value: any},
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {JSONValue, ResponseBase} from 'react-client/src/ReactFlightClient';

import type {JSResourceReference} from 'JSResourceReference';

import type {ModuleMetaData} from 'ReactFlightDOMRelayClientIntegration';
import type {ClientReferenceMetadata} from 'ReactFlightDOMRelayClientIntegration';

export type ClientReference<T> = JSResourceReference<T>;

Expand All @@ -29,7 +29,7 @@ import {resolveClientReference as resolveClientReferenceImpl} from 'ReactFlightD

import isArray from 'shared/isArray';

export type {ModuleMetaData} from 'ReactFlightDOMRelayClientIntegration';
export type {ClientReferenceMetadata} from 'ReactFlightDOMRelayClientIntegration';

export type BundlerConfig = null;

Expand All @@ -39,9 +39,9 @@ export type Response = ResponseBase;

export function resolveClientReference<T>(
bundlerConfig: BundlerConfig,
moduleData: ModuleMetaData,
metadata: ClientReferenceMetadata,
): ClientReference<T> {
return resolveClientReferenceImpl(moduleData);
return resolveClientReferenceImpl(metadata);
}

// $FlowFixMe[missing-local-annot]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @flow
*/

import type {ModuleMetaData} from 'ReactFlightDOMRelayServerIntegration';
import type {ClientReferenceMetadata} from 'ReactFlightDOMRelayServerIntegration';

export type JSONValue =
| string
Expand All @@ -19,7 +19,7 @@ export type JSONValue =

export type RowEncoding =
| ['O', number, JSONValue]
| ['I', number, ModuleMetaData]
| ['I', number, ClientReferenceMetadata]
| ['P', number, string]
| ['S', number, string]
| [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ import isArray from 'shared/isArray';

export type ClientReference<T> = JSResourceReference<T>;
export type ServerReference<T> = T;
export type ServerReferenceMetaData = {};
export type ServerReferenceMetadata = {};

import type {
Destination,
BundlerConfig,
ModuleMetaData,
ClientReferenceMetadata,
} from 'ReactFlightDOMRelayServerIntegration';

import {resolveModelToJSON} from 'react-server/src/ReactFlightServer';

import {
emitRow,
resolveModuleMetaData as resolveModuleMetaDataImpl,
resolveClientReferenceMetadata as resolveClientReferenceMetadataImpl,
close,
} from 'ReactFlightDOMRelayServerIntegration';

export type {
Destination,
BundlerConfig,
ModuleMetaData,
ClientReferenceMetadata,
} from 'ReactFlightDOMRelayServerIntegration';

export function isClientReference(reference: Object): boolean {
Expand All @@ -59,17 +59,17 @@ export function getClientReferenceKey(
return reference;
}

export function resolveModuleMetaData<T>(
export function resolveClientReferenceMetadata<T>(
config: BundlerConfig,
resource: ClientReference<T>,
): ModuleMetaData {
return resolveModuleMetaDataImpl(config, resource);
): ClientReferenceMetadata {
return resolveClientReferenceMetadataImpl(config, resource);
}

export function resolveServerReferenceMetaData<T>(
export function resolveServerReferenceMetadata<T>(
config: BundlerConfig,
resource: ServerReference<T>,
): ServerReferenceMetaData {
): ServerReferenceMetadata {
throw new Error('Not implemented.');
}

Expand Down Expand Up @@ -175,13 +175,13 @@ export function processReferenceChunk(
return ['O', id, reference];
}

export function processModuleChunk(
export function processImportChunk(
request: Request,
id: number,
moduleMetaData: ModuleMetaData,
clientReferenceMetadata: ClientReferenceMetadata,
): Chunk {
// The moduleMetaData is already a JSON serializable value.
return ['I', id, moduleMetaData];
// The clientReferenceMetadata is already a JSON serializable value.
return ['I', id, clientReferenceMetadata];
}

export function scheduleWork(callback: () => void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import JSResourceReferenceImpl from 'JSResourceReferenceImpl';

const ReactFlightDOMRelayClientIntegration = {
resolveClientReference(moduleData) {
return new JSResourceReferenceImpl(moduleData);
resolveClientReference(metadata) {
return new JSResourceReferenceImpl(metadata);
},
preloadModule(moduleReference) {},
requireModule(moduleReference) {
return moduleReference._moduleId;
preloadModule(clientReference) {},
requireModule(clientReference) {
return clientReference._moduleId;
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const ReactFlightDOMRelayServerIntegration = {
destination.push(json);
},
close(destination) {},
resolveModuleMetaData(config, resource) {
resolveClientReferenceMetadata(config, resource) {
return resource._moduleId;
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@ import type {

export type WebpackSSRMap = {
[clientId: string]: {
[clientExportName: string]: ModuleMetaData,
[clientExportName: string]: ClientReferenceMetadata,
},
};

export type BundlerConfig = null | WebpackSSRMap;

export opaque type ModuleMetaData = {
export opaque type ClientReferenceMetadata = {
id: string,
chunks: Array<string>,
name: string,
async: boolean,
};

// eslint-disable-next-line no-unused-vars
export opaque type ClientReference<T> = ModuleMetaData;
export opaque type ClientReference<T> = ClientReferenceMetadata;

export function resolveClientReference<T>(
bundlerConfig: BundlerConfig,
moduleData: ModuleMetaData,
metadata: ClientReferenceMetadata,
): ClientReference<T> {
if (bundlerConfig) {
const resolvedModuleData = bundlerConfig[moduleData.id][moduleData.name];
if (moduleData.async) {
const resolvedModuleData = bundlerConfig[metadata.id][metadata.name];
if (metadata.async) {
return {
id: resolvedModuleData.id,
chunks: resolvedModuleData.chunks,
Expand All @@ -48,7 +48,7 @@ export function resolveClientReference<T>(
return resolvedModuleData;
}
}
return moduleData;
return metadata;
}

// The chunk cache contains all the chunks we've preloaded so far.
Expand All @@ -64,9 +64,9 @@ function ignoreReject() {
// Start preloading the modules since we might need them soon.
// This function doesn't suspend.
export function preloadModule<T>(
moduleData: ClientReference<T>,
metadata: ClientReference<T>,
): null | Thenable<any> {
const chunks = moduleData.chunks;
const chunks = metadata.chunks;
const promises = [];
for (let i = 0; i < chunks.length; i++) {
const chunkId = chunks[i];
Expand All @@ -82,16 +82,16 @@ export function preloadModule<T>(
promises.push(entry);
}
}
if (moduleData.async) {
const existingPromise = asyncModuleCache.get(moduleData.id);
if (metadata.async) {
const existingPromise = asyncModuleCache.get(metadata.id);
if (existingPromise) {
if (existingPromise.status === 'fulfilled') {
return null;
}
return existingPromise;
} else {
const modulePromise: Thenable<T> = Promise.all(promises).then(() => {
return __webpack_require__(moduleData.id);
return __webpack_require__(metadata.id);
});
modulePromise.then(
value => {
Expand All @@ -107,7 +107,7 @@ export function preloadModule<T>(
rejectedThenable.reason = reason;
},
);
asyncModuleCache.set(moduleData.id, modulePromise);
asyncModuleCache.set(metadata.id, modulePromise);
return modulePromise;
}
} else if (promises.length > 0) {
Expand All @@ -119,29 +119,29 @@ export function preloadModule<T>(

// Actually require the module or suspend if it's not yet ready.
// Increase priority if necessary.
export function requireModule<T>(moduleData: ClientReference<T>): T {
export function requireModule<T>(metadata: ClientReference<T>): T {
let moduleExports;
if (moduleData.async) {
if (metadata.async) {
// We assume that preloadModule has been called before, which
// should have added something to the module cache.
const promise: any = asyncModuleCache.get(moduleData.id);
const promise: any = asyncModuleCache.get(metadata.id);
if (promise.status === 'fulfilled') {
moduleExports = promise.value;
} else {
throw promise.reason;
}
} else {
moduleExports = __webpack_require__(moduleData.id);
moduleExports = __webpack_require__(metadata.id);
}
if (moduleData.name === '*') {
if (metadata.name === '*') {
// This is a placeholder value that represents that the caller imported this
// as a CommonJS module as is.
return moduleExports;
}
if (moduleData.name === '') {
if (metadata.name === '') {
// This is a placeholder value that represents that the caller accessed the
// default property of this if it was an ESM interop module.
return moduleExports.__esModule ? moduleExports.default : moduleExports;
}
return moduleExports[moduleData.name];
return moduleExports[metadata.name];
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {ReactModel} from 'react-server/src/ReactFlightServer';

type WebpackMap = {
[filepath: string]: {
[name: string]: ModuleMetaData,
[name: string]: ClientReferenceMetadata,
},
};

Expand All @@ -24,7 +24,7 @@ export type ServerReference<T: Function> = T & {
$$bound: Array<ReactModel>,
};

export type ServerReferenceMetaData = {
export type ServerReferenceMetadata = {
id: string,
name: string,
bound: Promise<Array<ReactModel>>,
Expand All @@ -38,7 +38,7 @@ export type ClientReference<T> = {
async: boolean,
};

export type ModuleMetaData = {
export type ClientReferenceMetadata = {
id: string,
chunks: Array<string>,
name: string,
Expand Down Expand Up @@ -69,10 +69,10 @@ export function isServerReference(reference: Object): boolean {
return reference.$$typeof === SERVER_REFERENCE_TAG;
}

export function resolveModuleMetaData<T>(
export function resolveClientReferenceMetadata<T>(
config: BundlerConfig,
clientReference: ClientReference<T>,
): ModuleMetaData {
): ClientReferenceMetadata {
const resolvedModuleData =
config[clientReference.filepath][clientReference.name];
if (clientReference.async) {
Expand All @@ -87,10 +87,10 @@ export function resolveModuleMetaData<T>(
}
}

export function resolveServerReferenceMetaData<T>(
export function resolveServerReferenceMetadata<T>(
config: BundlerConfig,
serverReference: ServerReference<T>,
): ServerReferenceMetaData {
): ServerReferenceMetadata {
return {
id: serverReference.$$filepath,
name: serverReference.$$name,
Expand Down
Loading

0 comments on commit f928095

Please sign in to comment.