12
12
13
13
"use strict" ;
14
14
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
+ }
16
23
require ( "ReactFeatureFlags" ) ;
17
24
var byteLengthImpl = null ;
18
25
function writeChunkAndReturn ( destination , chunk ) {
19
26
destination . write ( chunk ) ;
20
27
return ! 0 ;
21
28
}
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
+ } ;
33
38
function prefetchDNS ( href ) {
34
39
if ( "string" === typeof href && href ) {
35
40
var request = currentRequest ? currentRequest : null ;
@@ -647,8 +652,7 @@ function attemptResolveElement(
647
652
"Refs cannot be used in Server Components, nor passed to Client Components."
648
653
) ;
649
654
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 ] ;
652
656
thenableIndexCounter = 0 ;
653
657
thenableState = prevThenableState ;
654
658
props = type ( props ) ;
@@ -666,8 +670,7 @@ function attemptResolveElement(
666
670
? props . children
667
671
: [ REACT_ELEMENT_TYPE , type , key , props ] ;
668
672
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 ] ;
671
674
switch ( type . $$typeof ) {
672
675
case REACT_LAZY_TYPE :
673
676
var init = type . _init ;
@@ -727,32 +730,23 @@ function serializeByValueID(id) {
727
730
return "$" + id . toString ( 16 ) ;
728
731
}
729
732
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 ( ) ;
739
736
var writtenClientReferences = request . writtenClientReferences ,
740
737
existingId = writtenClientReferences . get ( JSCompiler_inline_result ) ;
741
738
if ( void 0 !== existingId )
742
739
return parent [ 0 ] === REACT_ELEMENT_TYPE && "1" === key
743
740
? "$L" + existingId . toString ( 16 )
744
741
: serializeByValueID ( existingId ) ;
745
742
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
+ } ;
753
747
request . pendingChunks ++ ;
754
748
var importId = request . nextChunkId ++ ,
755
- json = stringify ( clientReference ) ,
749
+ json = stringify ( clientReferenceMetadata ) ,
756
750
processedChunk = importId . toString ( 16 ) + ":I" + json + "\n" ;
757
751
request . completedImportChunks . push ( processedChunk ) ;
758
752
writtenClientReferences . set ( JSCompiler_inline_result , importId ) ;
@@ -852,7 +846,7 @@ function resolveModelToJSON(request, parent, key, value) {
852
846
}
853
847
if ( null === value ) return null ;
854
848
if ( "object" === typeof value ) {
855
- if ( registeredClientReferences . has ( value ) )
849
+ if ( isClientReference ( value ) )
856
850
return serializeClientReference ( request , parent , key , value ) ;
857
851
parent = request . writtenObjects ;
858
852
key = parent . get ( value ) ;
@@ -944,7 +938,7 @@ function resolveModelToJSON(request, parent, key, value) {
944
938
) ;
945
939
if ( "undefined" === typeof value ) return "$undefined" ;
946
940
if ( "function" === typeof value ) {
947
- if ( registeredClientReferences . has ( value ) )
941
+ if ( isClientReference ( value ) )
948
942
return serializeClientReference ( request , parent , key , value ) ;
949
943
throw Error ( "isServerReference: Not Implemented." ) ;
950
944
}
@@ -1135,31 +1129,16 @@ exports.clearRequestedClientReferencesKeysSet = function () {
1135
1129
exports . getRequestedClientReferencesKeys = function ( ) {
1136
1130
return Array . from ( requestedClientReferencesKeys ) ;
1137
1131
} ;
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 ( ) { } ;
1145
1133
exports . registerServerReference = function ( ) {
1146
1134
throw Error ( "registerServerReference: Not Implemented." ) ;
1147
1135
} ;
1148
- exports . renderToDestination = function (
1149
- destination ,
1150
- model ,
1151
- bundlerConfig ,
1152
- options
1153
- ) {
1136
+ exports . renderToDestination = function ( destination , model , options ) {
1154
1137
if ( ! configured )
1155
1138
throw Error (
1156
1139
"Please make sure to call `setConfig(...)` before calling `renderToDestination`."
1157
1140
) ;
1158
- model = createRequest (
1159
- model ,
1160
- bundlerConfig ,
1161
- options ? options . onError : void 0
1162
- ) ;
1141
+ model = createRequest ( model , null , options ? options . onError : void 0 ) ;
1163
1142
model . flushScheduled = null !== model . destination ;
1164
1143
performWork ( model ) ;
1165
1144
if ( 1 === model . status )
@@ -1175,7 +1154,11 @@ exports.renderToDestination = function (
1175
1154
}
1176
1155
}
1177
1156
} ;
1157
+ exports . setCheckIsClientReference = function ( impl ) {
1158
+ checkIsClientReference = impl ;
1159
+ } ;
1178
1160
exports . setConfig = function ( config ) {
1179
1161
byteLengthImpl = config . byteLength ;
1162
+ checkIsClientReference = config . isClientReference ;
1180
1163
configured = ! 0 ;
1181
1164
} ;
0 commit comments