mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
172e89b4bf
* Remove redundant initial of isArray (#21163) * Reapply prettier * Type the isArray function with refinement support This ensures that an argument gets refined just like it does if isArray is used directly. I'm not sure how to express with just a direct reference so I added a function wrapper and confirmed that this does get inlined properly by closure compiler. * A few more * Rename unit test to internal This is not testing a bundle. Co-authored-by: Behnam Mohammadi <itten@live.com>
70 lines
1.7 KiB
JavaScript
70 lines
1.7 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import type {JSONValue, ResponseBase} from 'react-client/src/ReactFlightClient';
|
|
|
|
import type JSResourceReference from 'JSResourceReference';
|
|
|
|
export type ModuleReference<T> = JSResourceReference<T>;
|
|
|
|
import {
|
|
parseModelString,
|
|
parseModelTuple,
|
|
} from 'react-client/src/ReactFlightClient';
|
|
|
|
export {
|
|
resolveModuleReference,
|
|
preloadModule,
|
|
requireModule,
|
|
} from 'ReactFlightNativeRelayClientIntegration';
|
|
|
|
import isArray from 'shared/isArray';
|
|
|
|
export type {ModuleMetaData} from 'ReactFlightNativeRelayClientIntegration';
|
|
|
|
export type UninitializedModel = JSONValue;
|
|
|
|
export type Response = ResponseBase;
|
|
|
|
function parseModelRecursively(response: Response, parentObj, value) {
|
|
if (typeof value === 'string') {
|
|
return parseModelString(response, parentObj, value);
|
|
}
|
|
if (typeof value === 'object' && value !== null) {
|
|
if (isArray(value)) {
|
|
const parsedValue = [];
|
|
for (let i = 0; i < value.length; i++) {
|
|
(parsedValue: any)[i] = parseModelRecursively(
|
|
response,
|
|
value,
|
|
value[i],
|
|
);
|
|
}
|
|
return parseModelTuple(response, parsedValue);
|
|
} else {
|
|
const parsedValue = {};
|
|
for (const innerKey in value) {
|
|
(parsedValue: any)[innerKey] = parseModelRecursively(
|
|
response,
|
|
value,
|
|
value[innerKey],
|
|
);
|
|
}
|
|
return parsedValue;
|
|
}
|
|
}
|
|
return value;
|
|
}
|
|
|
|
const dummy = {};
|
|
|
|
export function parseModel<T>(response: Response, json: UninitializedModel): T {
|
|
return (parseModelRecursively(response, dummy, json): any);
|
|
}
|