Skip to content

Commit

Permalink
[RSC @ Meta] Simplify implementation of isClientReference, getClientR…
Browse files Browse the repository at this point in the history
…eferenceKey, resolveClientReferenceMetadata (#27839)

For clientReferences we can just check the instance of the
`clientReference`.
The implementation of `isClientReference` is provided via configuration.
The class for ClientReference has to implement an interface that has
`getModuleId() method.

DiffTrain build for [cb24396](cb24396)
  • Loading branch information
alunyov committed Dec 19, 2023
1 parent 7fb394d commit beb4a4d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 115 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
63310df2b243b6c3b2f01e8b121e7d115e839cfb
cb2439624f43c510007f65aea5c50a8bb97917e4
6 changes: 3 additions & 3 deletions compiled/facebook-www/ReactDOMTesting-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -16454,7 +16454,7 @@ Internals.Events = [
var devToolsConfig$jscomp$inline_1784 = {
findFiberByHostInstance: getClosestInstanceFromNode,
bundleType: 0,
version: "18.3.0-www-modern-42373634",
version: "18.3.0-www-modern-760a7212",
rendererPackageName: "react-dom"
};
var internals$jscomp$inline_2140 = {
Expand Down Expand Up @@ -16485,7 +16485,7 @@ var internals$jscomp$inline_2140 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-modern-42373634"
reconcilerVersion: "18.3.0-www-modern-760a7212"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_2141 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down Expand Up @@ -16901,4 +16901,4 @@ exports.useFormState = function () {
exports.useFormStatus = function () {
throw Error(formatProdErrorMessage(248));
};
exports.version = "18.3.0-www-modern-42373634";
exports.version = "18.3.0-www-modern-760a7212";
103 changes: 46 additions & 57 deletions compiled/facebook-www/ReactFlightDOMServer-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,48 @@ if (__DEV__) {
var ReactDOM = require("react-dom");
var React = require("react");

// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line no-unused-vars
var requestedClientReferencesKeys = new Set();
var checkIsClientReference;
function setCheckIsClientReference(impl) {
checkIsClientReference = impl;
}
function registerClientReference(clientReference) {}
function isClientReference(reference) {
if (checkIsClientReference == null) {
throw new Error("Expected implementation for checkIsClientReference.");
}

return checkIsClientReference(reference);
}
function getClientReferenceKey(clientReference) {
var moduleId = clientReference.getModuleId();
requestedClientReferencesKeys.add(moduleId);
return clientReference.getModuleId();
}
function resolveClientReferenceMetadata(config, clientReference) {
return {
moduleId: clientReference.getModuleId(),
exportName: "default"
};
}
function registerServerReference(serverReference, id, exportName) {
throw new Error("registerServerReference: Not Implemented.");
}
function isServerReference(reference) {
throw new Error("isServerReference: Not Implemented.");
}
function getServerReferenceId(config, serverReference) {
throw new Error("getServerReferenceId: Not Implemented.");
}
function getRequestedClientReferencesKeys() {
return Array.from(requestedClientReferencesKeys);
}
function clearRequestedClientReferencesKeysSet() {
requestedClientReferencesKeys.clear();
}

// This refers to a WWW module.
var warningWWW = require("warning");
function error(format) {
Expand Down Expand Up @@ -108,61 +150,6 @@ if (__DEV__) {
destination.close();
}

// eslint-disable-next-line no-unused-vars
// eslint-disable-next-line no-unused-vars
var registeredClientReferences = new Map();
var requestedClientReferencesKeys = new Set();
function registerClientReference(clientReference, moduleId) {
var exportName = "default"; // Currently, we only support modules with `default` export

registeredClientReferences.set(clientReference, {
moduleId: moduleId,
exportName: exportName
});
return clientReference;
}
function isClientReference(reference) {
return registeredClientReferences.has(reference);
}
function getClientReferenceKey(clientReference) {
var reference = registeredClientReferences.get(clientReference);

if (reference != null) {
requestedClientReferencesKeys.add(reference.moduleId);
return reference.moduleId;
}

throw new Error(
"Expected client reference " + clientReference + " to be registered."
);
}
function resolveClientReferenceMetadata(config, clientReference) {
var metadata = registeredClientReferences.get(clientReference);

if (metadata != null) {
return metadata;
}

throw new Error(
"Expected client reference " + clientReference + " to be registered."
);
}
function registerServerReference(serverReference, exportName) {
throw new Error("registerServerReference: Not Implemented.");
}
function isServerReference(reference) {
throw new Error("isServerReference: Not Implemented.");
}
function getServerReferenceId(config, serverReference) {
throw new Error("getServerReferenceId: Not Implemented.");
}
function getRequestedClientReferencesKeys() {
return Array.from(requestedClientReferencesKeys);
}
function clearRequestedClientReferencesKeysSet() {
requestedClientReferencesKeys.clear();
}

function getServerReferenceBoundArguments(config, serverReference) {
throw new Error("getServerReferenceBoundArguments: Not Implemented.");
}
Expand Down Expand Up @@ -2546,7 +2533,7 @@ if (__DEV__) {
return rootContextSnapshot;
}

function renderToDestination(destination, model, bundlerConfig, options) {
function renderToDestination(destination, model, options) {
if (!configured) {
throw new Error(
"Please make sure to call `setConfig(...)` before calling `renderToDestination`."
Expand All @@ -2555,7 +2542,7 @@ if (__DEV__) {

var request = createRequest(
model,
bundlerConfig,
null,
options ? options.onError : undefined
);
startWork(request);
Expand All @@ -2566,6 +2553,7 @@ if (__DEV__) {

function setConfig(config) {
setByteLengthOfChunkImplementation(config.byteLength);
setCheckIsClientReference(config.isClientReference);
configured = true;
}

Expand All @@ -2575,6 +2563,7 @@ if (__DEV__) {
exports.registerClientReference = registerClientReference;
exports.registerServerReference = registerServerReference;
exports.renderToDestination = renderToDestination;
exports.setCheckIsClientReference = setCheckIsClientReference;
exports.setConfig = setConfig;
})();
}
89 changes: 36 additions & 53 deletions compiled/facebook-www/ReactFlightDOMServer-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,29 @@

"use strict";
var ReactDOM = require("react-dom"),
React = require("react");
React = require("react"),
requestedClientReferencesKeys = new Set(),
checkIsClientReference;
function isClientReference(reference) {
if (null == checkIsClientReference)
throw Error("Expected implementation for checkIsClientReference.");
return checkIsClientReference(reference);
}
require("ReactFeatureFlags");
var byteLengthImpl = null;
function writeChunkAndReturn(destination, chunk) {
destination.write(chunk);
return !0;
}
var registeredClientReferences = new Map(),
requestedClientReferencesKeys = new Set(),
ReactDOMFlightServerDispatcher = {
prefetchDNS: prefetchDNS,
preconnect: preconnect,
preload: preload,
preloadModule: preloadModule,
preinitStyle: preinitStyle,
preinitScript: preinitScript,
preinitModuleScript: preinitModuleScript
};
var ReactDOMFlightServerDispatcher = {
prefetchDNS: prefetchDNS,
preconnect: preconnect,
preload: preload,
preloadModule: preloadModule,
preinitStyle: preinitStyle,
preinitScript: preinitScript,
preinitModuleScript: preinitModuleScript
};
function prefetchDNS(href) {
if ("string" === typeof href && href) {
var request = currentRequest ? currentRequest : null;
Expand Down Expand Up @@ -647,8 +652,7 @@ function attemptResolveElement(
"Refs cannot be used in Server Components, nor passed to Client Components."
);
if ("function" === typeof type) {
if (registeredClientReferences.has(type))
return [REACT_ELEMENT_TYPE, type, key, props];
if (isClientReference(type)) return [REACT_ELEMENT_TYPE, type, key, props];
thenableIndexCounter = 0;
thenableState = prevThenableState;
props = type(props);
Expand All @@ -666,8 +670,7 @@ function attemptResolveElement(
? props.children
: [REACT_ELEMENT_TYPE, type, key, props];
if (null != type && "object" === typeof type) {
if (registeredClientReferences.has(type))
return [REACT_ELEMENT_TYPE, type, key, props];
if (isClientReference(type)) return [REACT_ELEMENT_TYPE, type, key, props];
switch (type.$$typeof) {
case REACT_LAZY_TYPE:
var init = type._init;
Expand Down Expand Up @@ -727,32 +730,23 @@ function serializeByValueID(id) {
return "$" + id.toString(16);
}
function serializeClientReference(request, parent, key, clientReference) {
var JSCompiler_inline_result =
registeredClientReferences.get(clientReference);
if (null != JSCompiler_inline_result)
requestedClientReferencesKeys.add(JSCompiler_inline_result.moduleId),
(JSCompiler_inline_result = JSCompiler_inline_result.moduleId);
else
throw Error(
"Expected client reference " + clientReference + " to be registered."
);
var JSCompiler_inline_result = clientReference.getModuleId();
requestedClientReferencesKeys.add(JSCompiler_inline_result);
JSCompiler_inline_result = clientReference.getModuleId();
var writtenClientReferences = request.writtenClientReferences,
existingId = writtenClientReferences.get(JSCompiler_inline_result);
if (void 0 !== existingId)
return parent[0] === REACT_ELEMENT_TYPE && "1" === key
? "$L" + existingId.toString(16)
: serializeByValueID(existingId);
try {
var metadata = registeredClientReferences.get(clientReference);
if (null != metadata) var JSCompiler_inline_result$jscomp$0 = metadata;
else
throw Error(
"Expected client reference " + clientReference + " to be registered."
);
clientReference = JSCompiler_inline_result$jscomp$0;
var clientReferenceMetadata = {
moduleId: clientReference.getModuleId(),
exportName: "default"
};
request.pendingChunks++;
var importId = request.nextChunkId++,
json = stringify(clientReference),
json = stringify(clientReferenceMetadata),
processedChunk = importId.toString(16) + ":I" + json + "\n";
request.completedImportChunks.push(processedChunk);
writtenClientReferences.set(JSCompiler_inline_result, importId);
Expand Down Expand Up @@ -852,7 +846,7 @@ function resolveModelToJSON(request, parent, key, value) {
}
if (null === value) return null;
if ("object" === typeof value) {
if (registeredClientReferences.has(value))
if (isClientReference(value))
return serializeClientReference(request, parent, key, value);
parent = request.writtenObjects;
key = parent.get(value);
Expand Down Expand Up @@ -944,7 +938,7 @@ function resolveModelToJSON(request, parent, key, value) {
);
if ("undefined" === typeof value) return "$undefined";
if ("function" === typeof value) {
if (registeredClientReferences.has(value))
if (isClientReference(value))
return serializeClientReference(request, parent, key, value);
throw Error("isServerReference: Not Implemented.");
}
Expand Down Expand Up @@ -1135,31 +1129,16 @@ exports.clearRequestedClientReferencesKeysSet = function () {
exports.getRequestedClientReferencesKeys = function () {
return Array.from(requestedClientReferencesKeys);
};
exports.registerClientReference = function (clientReference, moduleId) {
registeredClientReferences.set(clientReference, {
moduleId: moduleId,
exportName: "default"
});
return clientReference;
};
exports.registerClientReference = function () {};
exports.registerServerReference = function () {
throw Error("registerServerReference: Not Implemented.");
};
exports.renderToDestination = function (
destination,
model,
bundlerConfig,
options
) {
exports.renderToDestination = function (destination, model, options) {
if (!configured)
throw Error(
"Please make sure to call `setConfig(...)` before calling `renderToDestination`."
);
model = createRequest(
model,
bundlerConfig,
options ? options.onError : void 0
);
model = createRequest(model, null, options ? options.onError : void 0);
model.flushScheduled = null !== model.destination;
performWork(model);
if (1 === model.status)
Expand All @@ -1175,7 +1154,11 @@ exports.renderToDestination = function (
}
}
};
exports.setCheckIsClientReference = function (impl) {
checkIsClientReference = impl;
};
exports.setConfig = function (config) {
byteLengthImpl = config.byteLength;
checkIsClientReference = config.isClientReference;
configured = !0;
};
2 changes: 1 addition & 1 deletion compiled/facebook-www/ReactTestRenderer-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -25931,7 +25931,7 @@ if (__DEV__) {
return root;
}

var ReactVersion = "18.3.0-www-modern-25845ae4";
var ReactVersion = "18.3.0-www-modern-8e6c02f7";

// Might add PROFILE later.

Expand Down

0 comments on commit beb4a4d

Please sign in to comment.