Skip to content

Commit beb4a4d

Browse files
committed
[RSC @ Meta] Simplify implementation of isClientReference, getClientReferenceKey, 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)
1 parent 7fb394d commit beb4a4d

File tree

5 files changed

+87
-115
lines changed

5 files changed

+87
-115
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
63310df2b243b6c3b2f01e8b121e7d115e839cfb
1+
cb2439624f43c510007f65aea5c50a8bb97917e4

compiled/facebook-www/ReactDOMTesting-prod.modern.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16454,7 +16454,7 @@ Internals.Events = [
1645416454
var devToolsConfig$jscomp$inline_1784 = {
1645516455
findFiberByHostInstance: getClosestInstanceFromNode,
1645616456
bundleType: 0,
16457-
version: "18.3.0-www-modern-42373634",
16457+
version: "18.3.0-www-modern-760a7212",
1645816458
rendererPackageName: "react-dom"
1645916459
};
1646016460
var internals$jscomp$inline_2140 = {
@@ -16485,7 +16485,7 @@ var internals$jscomp$inline_2140 = {
1648516485
scheduleRoot: null,
1648616486
setRefreshHandler: null,
1648716487
getCurrentFiber: null,
16488-
reconcilerVersion: "18.3.0-www-modern-42373634"
16488+
reconcilerVersion: "18.3.0-www-modern-760a7212"
1648916489
};
1649016490
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
1649116491
var hook$jscomp$inline_2141 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
@@ -16901,4 +16901,4 @@ exports.useFormState = function () {
1690116901
exports.useFormStatus = function () {
1690216902
throw Error(formatProdErrorMessage(248));
1690316903
};
16904-
exports.version = "18.3.0-www-modern-42373634";
16904+
exports.version = "18.3.0-www-modern-760a7212";

compiled/facebook-www/ReactFlightDOMServer-dev.modern.js

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,48 @@ if (__DEV__) {
1919
var ReactDOM = require("react-dom");
2020
var React = require("react");
2121

22+
// eslint-disable-next-line no-unused-vars
23+
// eslint-disable-next-line no-unused-vars
24+
var requestedClientReferencesKeys = new Set();
25+
var checkIsClientReference;
26+
function setCheckIsClientReference(impl) {
27+
checkIsClientReference = impl;
28+
}
29+
function registerClientReference(clientReference) {}
30+
function isClientReference(reference) {
31+
if (checkIsClientReference == null) {
32+
throw new Error("Expected implementation for checkIsClientReference.");
33+
}
34+
35+
return checkIsClientReference(reference);
36+
}
37+
function getClientReferenceKey(clientReference) {
38+
var moduleId = clientReference.getModuleId();
39+
requestedClientReferencesKeys.add(moduleId);
40+
return clientReference.getModuleId();
41+
}
42+
function resolveClientReferenceMetadata(config, clientReference) {
43+
return {
44+
moduleId: clientReference.getModuleId(),
45+
exportName: "default"
46+
};
47+
}
48+
function registerServerReference(serverReference, id, exportName) {
49+
throw new Error("registerServerReference: Not Implemented.");
50+
}
51+
function isServerReference(reference) {
52+
throw new Error("isServerReference: Not Implemented.");
53+
}
54+
function getServerReferenceId(config, serverReference) {
55+
throw new Error("getServerReferenceId: Not Implemented.");
56+
}
57+
function getRequestedClientReferencesKeys() {
58+
return Array.from(requestedClientReferencesKeys);
59+
}
60+
function clearRequestedClientReferencesKeysSet() {
61+
requestedClientReferencesKeys.clear();
62+
}
63+
2264
// This refers to a WWW module.
2365
var warningWWW = require("warning");
2466
function error(format) {
@@ -108,61 +150,6 @@ if (__DEV__) {
108150
destination.close();
109151
}
110152

111-
// eslint-disable-next-line no-unused-vars
112-
// eslint-disable-next-line no-unused-vars
113-
var registeredClientReferences = new Map();
114-
var requestedClientReferencesKeys = new Set();
115-
function registerClientReference(clientReference, moduleId) {
116-
var exportName = "default"; // Currently, we only support modules with `default` export
117-
118-
registeredClientReferences.set(clientReference, {
119-
moduleId: moduleId,
120-
exportName: exportName
121-
});
122-
return clientReference;
123-
}
124-
function isClientReference(reference) {
125-
return registeredClientReferences.has(reference);
126-
}
127-
function getClientReferenceKey(clientReference) {
128-
var reference = registeredClientReferences.get(clientReference);
129-
130-
if (reference != null) {
131-
requestedClientReferencesKeys.add(reference.moduleId);
132-
return reference.moduleId;
133-
}
134-
135-
throw new Error(
136-
"Expected client reference " + clientReference + " to be registered."
137-
);
138-
}
139-
function resolveClientReferenceMetadata(config, clientReference) {
140-
var metadata = registeredClientReferences.get(clientReference);
141-
142-
if (metadata != null) {
143-
return metadata;
144-
}
145-
146-
throw new Error(
147-
"Expected client reference " + clientReference + " to be registered."
148-
);
149-
}
150-
function registerServerReference(serverReference, exportName) {
151-
throw new Error("registerServerReference: Not Implemented.");
152-
}
153-
function isServerReference(reference) {
154-
throw new Error("isServerReference: Not Implemented.");
155-
}
156-
function getServerReferenceId(config, serverReference) {
157-
throw new Error("getServerReferenceId: Not Implemented.");
158-
}
159-
function getRequestedClientReferencesKeys() {
160-
return Array.from(requestedClientReferencesKeys);
161-
}
162-
function clearRequestedClientReferencesKeysSet() {
163-
requestedClientReferencesKeys.clear();
164-
}
165-
166153
function getServerReferenceBoundArguments(config, serverReference) {
167154
throw new Error("getServerReferenceBoundArguments: Not Implemented.");
168155
}
@@ -2546,7 +2533,7 @@ if (__DEV__) {
25462533
return rootContextSnapshot;
25472534
}
25482535

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

25562543
var request = createRequest(
25572544
model,
2558-
bundlerConfig,
2545+
null,
25592546
options ? options.onError : undefined
25602547
);
25612548
startWork(request);
@@ -2566,6 +2553,7 @@ if (__DEV__) {
25662553

25672554
function setConfig(config) {
25682555
setByteLengthOfChunkImplementation(config.byteLength);
2556+
setCheckIsClientReference(config.isClientReference);
25692557
configured = true;
25702558
}
25712559

@@ -2575,6 +2563,7 @@ if (__DEV__) {
25752563
exports.registerClientReference = registerClientReference;
25762564
exports.registerServerReference = registerServerReference;
25772565
exports.renderToDestination = renderToDestination;
2566+
exports.setCheckIsClientReference = setCheckIsClientReference;
25782567
exports.setConfig = setConfig;
25792568
})();
25802569
}

compiled/facebook-www/ReactFlightDOMServer-prod.modern.js

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,29 @@
1212

1313
"use strict";
1414
var ReactDOM = require("react-dom"),
15-
React = require("react");
15+
React = require("react"),
16+
requestedClientReferencesKeys = new Set(),
17+
checkIsClientReference;
18+
function isClientReference(reference) {
19+
if (null == checkIsClientReference)
20+
throw Error("Expected implementation for checkIsClientReference.");
21+
return checkIsClientReference(reference);
22+
}
1623
require("ReactFeatureFlags");
1724
var byteLengthImpl = null;
1825
function writeChunkAndReturn(destination, chunk) {
1926
destination.write(chunk);
2027
return !0;
2128
}
22-
var registeredClientReferences = new Map(),
23-
requestedClientReferencesKeys = new Set(),
24-
ReactDOMFlightServerDispatcher = {
25-
prefetchDNS: prefetchDNS,
26-
preconnect: preconnect,
27-
preload: preload,
28-
preloadModule: preloadModule,
29-
preinitStyle: preinitStyle,
30-
preinitScript: preinitScript,
31-
preinitModuleScript: preinitModuleScript
32-
};
29+
var ReactDOMFlightServerDispatcher = {
30+
prefetchDNS: prefetchDNS,
31+
preconnect: preconnect,
32+
preload: preload,
33+
preloadModule: preloadModule,
34+
preinitStyle: preinitStyle,
35+
preinitScript: preinitScript,
36+
preinitModuleScript: preinitModuleScript
37+
};
3338
function prefetchDNS(href) {
3439
if ("string" === typeof href && href) {
3540
var request = currentRequest ? currentRequest : null;
@@ -647,8 +652,7 @@ function attemptResolveElement(
647652
"Refs cannot be used in Server Components, nor passed to Client Components."
648653
);
649654
if ("function" === typeof type) {
650-
if (registeredClientReferences.has(type))
651-
return [REACT_ELEMENT_TYPE, type, key, props];
655+
if (isClientReference(type)) return [REACT_ELEMENT_TYPE, type, key, props];
652656
thenableIndexCounter = 0;
653657
thenableState = prevThenableState;
654658
props = type(props);
@@ -666,8 +670,7 @@ function attemptResolveElement(
666670
? props.children
667671
: [REACT_ELEMENT_TYPE, type, key, props];
668672
if (null != type && "object" === typeof type) {
669-
if (registeredClientReferences.has(type))
670-
return [REACT_ELEMENT_TYPE, type, key, props];
673+
if (isClientReference(type)) return [REACT_ELEMENT_TYPE, type, key, props];
671674
switch (type.$$typeof) {
672675
case REACT_LAZY_TYPE:
673676
var init = type._init;
@@ -727,32 +730,23 @@ function serializeByValueID(id) {
727730
return "$" + id.toString(16);
728731
}
729732
function serializeClientReference(request, parent, key, clientReference) {
730-
var JSCompiler_inline_result =
731-
registeredClientReferences.get(clientReference);
732-
if (null != JSCompiler_inline_result)
733-
requestedClientReferencesKeys.add(JSCompiler_inline_result.moduleId),
734-
(JSCompiler_inline_result = JSCompiler_inline_result.moduleId);
735-
else
736-
throw Error(
737-
"Expected client reference " + clientReference + " to be registered."
738-
);
733+
var JSCompiler_inline_result = clientReference.getModuleId();
734+
requestedClientReferencesKeys.add(JSCompiler_inline_result);
735+
JSCompiler_inline_result = clientReference.getModuleId();
739736
var writtenClientReferences = request.writtenClientReferences,
740737
existingId = writtenClientReferences.get(JSCompiler_inline_result);
741738
if (void 0 !== existingId)
742739
return parent[0] === REACT_ELEMENT_TYPE && "1" === key
743740
? "$L" + existingId.toString(16)
744741
: serializeByValueID(existingId);
745742
try {
746-
var metadata = registeredClientReferences.get(clientReference);
747-
if (null != metadata) var JSCompiler_inline_result$jscomp$0 = metadata;
748-
else
749-
throw Error(
750-
"Expected client reference " + clientReference + " to be registered."
751-
);
752-
clientReference = JSCompiler_inline_result$jscomp$0;
743+
var clientReferenceMetadata = {
744+
moduleId: clientReference.getModuleId(),
745+
exportName: "default"
746+
};
753747
request.pendingChunks++;
754748
var importId = request.nextChunkId++,
755-
json = stringify(clientReference),
749+
json = stringify(clientReferenceMetadata),
756750
processedChunk = importId.toString(16) + ":I" + json + "\n";
757751
request.completedImportChunks.push(processedChunk);
758752
writtenClientReferences.set(JSCompiler_inline_result, importId);
@@ -852,7 +846,7 @@ function resolveModelToJSON(request, parent, key, value) {
852846
}
853847
if (null === value) return null;
854848
if ("object" === typeof value) {
855-
if (registeredClientReferences.has(value))
849+
if (isClientReference(value))
856850
return serializeClientReference(request, parent, key, value);
857851
parent = request.writtenObjects;
858852
key = parent.get(value);
@@ -944,7 +938,7 @@ function resolveModelToJSON(request, parent, key, value) {
944938
);
945939
if ("undefined" === typeof value) return "$undefined";
946940
if ("function" === typeof value) {
947-
if (registeredClientReferences.has(value))
941+
if (isClientReference(value))
948942
return serializeClientReference(request, parent, key, value);
949943
throw Error("isServerReference: Not Implemented.");
950944
}
@@ -1135,31 +1129,16 @@ exports.clearRequestedClientReferencesKeysSet = function () {
11351129
exports.getRequestedClientReferencesKeys = function () {
11361130
return Array.from(requestedClientReferencesKeys);
11371131
};
1138-
exports.registerClientReference = function (clientReference, moduleId) {
1139-
registeredClientReferences.set(clientReference, {
1140-
moduleId: moduleId,
1141-
exportName: "default"
1142-
});
1143-
return clientReference;
1144-
};
1132+
exports.registerClientReference = function () {};
11451133
exports.registerServerReference = function () {
11461134
throw Error("registerServerReference: Not Implemented.");
11471135
};
1148-
exports.renderToDestination = function (
1149-
destination,
1150-
model,
1151-
bundlerConfig,
1152-
options
1153-
) {
1136+
exports.renderToDestination = function (destination, model, options) {
11541137
if (!configured)
11551138
throw Error(
11561139
"Please make sure to call `setConfig(...)` before calling `renderToDestination`."
11571140
);
1158-
model = createRequest(
1159-
model,
1160-
bundlerConfig,
1161-
options ? options.onError : void 0
1162-
);
1141+
model = createRequest(model, null, options ? options.onError : void 0);
11631142
model.flushScheduled = null !== model.destination;
11641143
performWork(model);
11651144
if (1 === model.status)
@@ -1175,7 +1154,11 @@ exports.renderToDestination = function (
11751154
}
11761155
}
11771156
};
1157+
exports.setCheckIsClientReference = function (impl) {
1158+
checkIsClientReference = impl;
1159+
};
11781160
exports.setConfig = function (config) {
11791161
byteLengthImpl = config.byteLength;
1162+
checkIsClientReference = config.isClientReference;
11801163
configured = !0;
11811164
};

compiled/facebook-www/ReactTestRenderer-dev.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25931,7 +25931,7 @@ if (__DEV__) {
2593125931
return root;
2593225932
}
2593325933

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

2593625936
// Might add PROFILE later.
2593725937

0 commit comments

Comments
 (0)