10
import type {
11
Destination,
12
Chunk,
13
- BundlerConfig,
13
+ ClientManifest,
14
ClientReferenceMetadata,
15
ClientReference,
16
ClientReferenceKey,
27
PendingThenable,
28
FulfilledThenable,
29
RejectedThenable,
30
+ ReactServerContext,
31
} from 'shared/ReactTypes';
32
import type {LazyComponent} from 'react/src/ReactLazy';
33
45
processErrorChunkDev,
46
processReferenceChunk,
47
resolveClientReferenceMetadata,
47
- resolveServerReferenceMetadata,
48
+ getServerReferenceId,
49
+ getServerReferenceBoundArguments,
50
getClientReferenceKey,
51
isClientReference,
52
isServerReference,
97
| number
98
| null
99
| $ReadOnlyArray<ReactJSONValue>
98
- | ReactModelObject;
99
-
100
-export type ReactModel =
101
- | React$Element<any>
102
- | LazyComponent<any, any>
100
+ | ReactClientObject;
101
+
102
+// Serializable values
103
+export type ReactClientValue =
104
+ // Server Elements and Lazy Components are unwrapped on the Server
105
+ | React$Element<React$AbstractComponent<any, any>>
106
+ | LazyComponent<ReactClientValue, any>
107
+ // References are passed by their value
108
+ | ClientReference<any>
109
+ | ServerReference<any>
110
+ // The rest are passed as is. Sub-types can be passed in but lose their
111
+ // subtype, so the receiver can only accept once of these.
112
+ | React$Element<string>
113
+ | React$Element<ClientReference<any> & any>
114
+ | ReactServerContext<any>
115
| string
116
| boolean
117
| number
118
| symbol
119
| null
108
- | Iterable<ReactModel>
109
- | ReactModelObject
110
- | Promise<ReactModel>;
120
+ | Iterable<ReactClientValue>
121
+ | Array<ReactClientValue>
122
+ | ReactClientObject
123
+ | Promise<ReactClientValue>; // Thenable<ReactClientValue>
124
112
-type ReactModelObject = {+[key: string]: ReactModel};
125
+type ReactClientObject = {+[key: string]: ReactClientValue};
126
127
const PENDING = 0;
128
const COMPLETED = 1;
132
type Task = {
133
id: number,
134
status: 0 | 1 | 3 | 4,
122
- model: ReactModel,
135
+ model: ReactClientValue,
136
ping: () => void,
137
context: ContextSnapshot,
138
thenableState: ThenableState | null,
142
status: 0 | 1 | 2,
143
fatalError: mixed,
144
destination: null | Destination,
132
- bundlerConfig: BundlerConfig,
145
+ bundlerConfig: ClientManifest,
146
cache: Map<Function, mixed>,
147
nextChunkId: number,
148
pendingChunks: number,
158
identifierPrefix: string,
159
identifierCount: number,
160
onError: (error: mixed) => ?string,
148
- toJSON: (key: string, value: ReactModel) => ReactJSONValue,
161
+ toJSON: (key: string, value: ReactClientValue) => ReactJSONValue,
162
};
163
164
const ReactCurrentDispatcher = ReactSharedInternals.ReactCurrentDispatcher;
174
const CLOSED = 2;
175
176
export function createRequest(
164
- model: ReactModel,
165
- bundlerConfig: BundlerConfig,
177
+ model: ReactClientValue,
178
+ bundlerConfig: ClientManifest,
179
onError: void | ((error: mixed) => ?string),
180
context?: Array<[string, ServerContextJSONValue]>,
181
identifierPrefix?: string,
213
identifierCount: 1,
214
onError: onError === undefined ? defaultErrorHandler : onError,
215
// $FlowFixMe[missing-this-annot]
203
- toJSON: function (key: string, value: ReactModel): ReactJSONValue {
216
+ toJSON: function (key: string, value: ReactClientValue): ReactJSONValue {
217
return resolveModelToJSON(request, this, key, value);
218
},
219
};
377
ref: mixed,
378
props: any,
379
prevThenableState: ThenableState | null,
367
-): ReactModel {
380
+): ReactClientValue {
381
if (ref !== null && ref !== undefined) {
382
// When the ref moves to the regular props object this will implicitly
383
// throw for functions. We could probably relax it to a DEV warning for other
505
506
function createTask(
507
request: Request,
495
- model: ReactModel,
508
+ model: ReactClientValue,
509
context: ContextSnapshot,
510
abortSet: Set<Task>,
511
): Task {
548
549
function serializeClientReference(
550
request: Request,
538
- parent: {+[key: string | number]: ReactModel} | $ReadOnlyArray<ReactModel>,
551
+ parent:
552
+ | {+[key: string | number]: ReactClientValue}
553
+ | $ReadOnlyArray<ReactClientValue>,
554
key: string,
555
clientReference: ClientReference<any>,
556
): string {
601
602
function serializeServerReference(
603
request: Request,
589
- parent: {+[key: string | number]: ReactModel} | $ReadOnlyArray<ReactModel>,
604
+ parent:
605
+ | {+[key: string | number]: ReactClientValue}
606
+ | $ReadOnlyArray<ReactClientValue>,
607
key: string,
608
serverReference: ServerReference<any>,
609
): string {
612
if (existingId !== undefined) {
613
return serializeServerReferenceID(existingId);
614
}
615
+
616
+ const bound: null | Array<any> = getServerReferenceBoundArguments(
617
+ request.bundlerConfig,
618
+ serverReference,
619
+ );
620
const serverReferenceMetadata: {
621
id: ServerReferenceId,
600
- bound: Promise<Array<any>>,
601
- } = resolveServerReferenceMetadata(request.bundlerConfig, serverReference);
622
+ bound: null | Promise<Array<any>>,
623
+ } = {
624
+ id: getServerReferenceId(request.bundlerConfig, serverReference),
625
+ bound: bound ? Promise.resolve(bound) : null,
626
+ };
627
request.pendingChunks++;
628
const metadataId = request.nextChunkId++;
629
// We assume that this object doesn't suspend.
708
return '"' + key + '"' === encodedKey ? key : encodedKey;
709
}
710
686
-function describeValueForErrorMessage(value: ReactModel): string {
711
+function describeValueForErrorMessage(value: ReactClientValue): string {
712
switch (typeof value) {
713
case 'string': {
714
return JSON.stringify(
765
766
function describeObjectForErrorMessage(
767
objectOrArray:
743
- | {+[key: string | number]: ReactModel, ...}
744
- | $ReadOnlyArray<ReactModel>,
768
+ | {+[key: string | number]: ReactClientValue, ...}
769
+ | $ReadOnlyArray<ReactClientValue>,
770
expandedName?: string,
771
): string {
772
const objKind = objectName(objectOrArray);
781
// Print JSX Children
782
const type = jsxChildrenParents.get(objectOrArray);
783
str = '<' + describeElementType(type) + '>';
759
- const array: $ReadOnlyArray<ReactModel> = objectOrArray;
784
+ const array: $ReadOnlyArray<ReactClientValue> = objectOrArray;
785
for (let i = 0; i < array.length; i++) {
786
const value = array[i];
787
let substr;
807
} else {
808
// Print Array
809
str = '[';
785
- const array: $ReadOnlyArray<ReactModel> = objectOrArray;
810
+ const array: $ReadOnlyArray<ReactClientValue> = objectOrArray;
811
for (let i = 0; i < array.length; i++) {
812
if (i > 0) {
813
str += ', ';
839
// Print JSX
840
const type = jsxPropsParents.get(objectOrArray);
841
str = '<' + (describeElementType(type) || '...');
817
- const object: {+[key: string | number]: ReactModel, ...} = objectOrArray;
842
+ const object: {+[key: string | number]: ReactClientValue, ...} =
843
+ objectOrArray;
844
const names = Object.keys(object);
845
for (let i = 0; i < names.length; i++) {
846
str += ' ';
875
} else {
876
// Print Object
877
str = '{';
852
- const object: {+[key: string | number]: ReactModel, ...} = objectOrArray;
878
+ const object: {+[key: string | number]: ReactClientValue, ...} =
879
+ objectOrArray;
880
const names = Object.keys(object);
881
for (let i = 0; i < names.length; i++) {
882
if (i > 0) {
920
921
export function resolveModelToJSON(
922
request: Request,
896
- parent: {+[key: string | number]: ReactModel} | $ReadOnlyArray<ReactModel>,
923
+ parent:
924
+ | {+[key: string | number]: ReactClientValue}
925
+ | $ReadOnlyArray<ReactClientValue>,
926
key: string,
898
- value: ReactModel,
927
+ value: ReactClientValue,
928
): ReactJSONValue {
929
if (__DEV__) {
930
// $FlowFixMe
967
if (
968
parent[0] === REACT_ELEMENT_TYPE &&
969
parent[1] &&
941
- parent[1].$$typeof === REACT_PROVIDER_TYPE &&
970
+ (parent[1]: any).$$typeof === REACT_PROVIDER_TYPE &&
971
key === '3'
972
) {
973
insideContextProps = value;