react/packages/react-native-renderer/src/ReactNativeAttributePayload.js

484 lines
14 KiB
JavaScript
Raw Normal View History

2016-03-25 06:40:52 +08:00
/**
* Copyright (c) Facebook, Inc. and its affiliates.
2016-03-25 06:40:52 +08:00
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
2016-03-25 06:40:52 +08:00
*
* @flow
*/
// Modules provided by RN:
[react-native] Use path-based imports instead of Haste for the RN renderer (#15604) * [react-native] Use path-based imports instead of Haste for the RN renderer To move React Native to standard path-based imports instead of Haste, the RN renderer that is generated from the code in this repo needs to use path-based imports as well since the generated code is vendored by RN. This commit makes it so the interface between the generated renderers and RN does not rely on Haste and instead uses a private interface explicitly defined by RN. This inverts control of the abstraction so that RN decides the internals to export rather than React deciding what to import. On RN's side, a new module named `react-native/Libraries/ReactPrivate/ReactNativePrivateInterface` explicitly exports the modules used by the renderers in this repo. (There is also a private module for InitializeCore so that we can import it just for the side effects.) On React's side, the various renderer modules access RN internals through the explicit private interface. The Rollup configuration becomes slimmer since the only external package is now `react-native`, and the individual modules are instead listed out in `ReactNativePrivateInterface`. Task description: https://github.com/facebook/react-native/issues/24770 Sister RN PR (needs to land before this one): https://github.com/facebook/react-native/pull/24782 Test Plan: Ran unit tests and Flow in this repo. Generated the renderers and manually copied them over to the RN repo. Ran the RN tests and launched the RNTester app. * Access natively defined "nativeFabricUIManager" instead of importing it Some places in the Fabric renderers access `nativeFabricUIManager` (a natively defined global) instead of importing UIManager. While this is coupling across repos that depends on the timing of events, it is necessary until we have a way to defer top-level imports to run after `nativeFabricUIManager` is defined. So for consistency we use `nativeFabricUIManager` everywhere (see the comment in https://github.com/facebook/react/pull/15604#pullrequestreview-236842223 for more context).
2019-05-23 15:23:54 +08:00
import {
deepDiffer,
flattenStyle,
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
2016-03-25 06:40:52 +08:00
import type {AttributeConfiguration} from './ReactNativeTypes';
const emptyObject = {};
2016-03-25 06:40:52 +08:00
/**
* Create a payload that contains all the updates between two sets of props.
*
* These helpers are all encapsulated into a single module, because they use
* mutation as a performance optimization which leads to subtle shared
* dependencies between the code paths. To avoid this mutable state leaking
* across modules, I've kept them isolated to this module.
*/
type NestedNode = Array<NestedNode> | Object;
2016-03-25 06:40:52 +08:00
// Tracks removed keys
let removedKeys = null;
let removedKeyCount = 0;
2016-03-25 06:40:52 +08:00
const deepDifferOptions = {
unsafelyIgnoreFunctions: true,
};
2017-03-14 08:05:18 +08:00
function defaultDiffer(prevProp: mixed, nextProp: mixed): boolean {
2016-03-25 06:40:52 +08:00
if (typeof nextProp !== 'object' || nextProp === null) {
// Scalars have already been checked for equality
return true;
} else {
// For objects and arrays, the default diffing algorithm is a deep compare
return deepDiffer(prevProp, nextProp, deepDifferOptions);
2016-03-25 06:40:52 +08:00
}
}
function restoreDeletedValuesInNestedArray(
updatePayload: Object,
node: NestedNode,
validAttributes: AttributeConfiguration<>,
2016-03-25 06:40:52 +08:00
) {
if (Array.isArray(node)) {
let i = node.length;
2016-03-25 06:40:52 +08:00
while (i-- && removedKeyCount > 0) {
restoreDeletedValuesInNestedArray(
updatePayload,
node[i],
2017-03-14 08:05:18 +08:00
validAttributes,
2016-03-25 06:40:52 +08:00
);
}
} else if (node && removedKeyCount > 0) {
const obj = node;
for (const propKey in removedKeys) {
2016-03-25 06:40:52 +08:00
if (!removedKeys[propKey]) {
continue;
}
let nextProp = obj[propKey];
2016-03-25 06:40:52 +08:00
if (nextProp === undefined) {
continue;
}
const attributeConfig = validAttributes[propKey];
2016-03-25 06:40:52 +08:00
if (!attributeConfig) {
continue; // not a valid native prop
}
if (typeof nextProp === 'function') {
nextProp = true;
}
if (typeof nextProp === 'undefined') {
nextProp = null;
}
if (typeof attributeConfig !== 'object') {
// case: !Object is the default case
updatePayload[propKey] = nextProp;
2017-03-14 08:05:18 +08:00
} else if (
typeof attributeConfig.diff === 'function' ||
typeof attributeConfig.process === 'function'
) {
2016-03-25 06:40:52 +08:00
// case: CustomAttributeConfiguration
const nextValue =
typeof attributeConfig.process === 'function'
? attributeConfig.process(nextProp)
: nextProp;
2016-03-25 06:40:52 +08:00
updatePayload[propKey] = nextValue;
}
removedKeys[propKey] = false;
removedKeyCount--;
}
}
}
function diffNestedArrayProperty(
updatePayload: null | Object,
2016-03-25 06:40:52 +08:00
prevArray: Array<NestedNode>,
nextArray: Array<NestedNode>,
validAttributes: AttributeConfiguration<>,
): null | Object {
const minLength =
prevArray.length < nextArray.length ? prevArray.length : nextArray.length;
let i;
2016-03-25 06:40:52 +08:00
for (i = 0; i < minLength; i++) {
// Diff any items in the array in the forward direction. Repeated keys
// will be overwritten by later values.
updatePayload = diffNestedProperty(
updatePayload,
prevArray[i],
nextArray[i],
2017-03-14 08:05:18 +08:00
validAttributes,
2016-03-25 06:40:52 +08:00
);
}
for (; i < prevArray.length; i++) {
// Clear out all remaining properties.
updatePayload = clearNestedProperty(
updatePayload,
prevArray[i],
2017-03-14 08:05:18 +08:00
validAttributes,
2016-03-25 06:40:52 +08:00
);
}
for (; i < nextArray.length; i++) {
// Add all remaining properties.
updatePayload = addNestedProperty(
updatePayload,
nextArray[i],
2017-03-14 08:05:18 +08:00
validAttributes,
2016-03-25 06:40:52 +08:00
);
}
return updatePayload;
}
function diffNestedProperty(
updatePayload: null | Object,
2016-03-25 06:40:52 +08:00
prevProp: NestedNode,
nextProp: NestedNode,
validAttributes: AttributeConfiguration<>,
): null | Object {
2016-03-25 06:40:52 +08:00
if (!updatePayload && prevProp === nextProp) {
// If no properties have been added, then we can bail out quickly on object
// equality.
return updatePayload;
}
if (!prevProp || !nextProp) {
if (nextProp) {
2017-03-14 08:05:18 +08:00
return addNestedProperty(updatePayload, nextProp, validAttributes);
2016-03-25 06:40:52 +08:00
}
if (prevProp) {
2017-03-14 08:05:18 +08:00
return clearNestedProperty(updatePayload, prevProp, validAttributes);
2016-03-25 06:40:52 +08:00
}
return updatePayload;
}
if (!Array.isArray(prevProp) && !Array.isArray(nextProp)) {
// Both are leaves, we can diff the leaves.
return diffProperties(updatePayload, prevProp, nextProp, validAttributes);
2016-03-25 06:40:52 +08:00
}
if (Array.isArray(prevProp) && Array.isArray(nextProp)) {
// Both are arrays, we can diff the arrays.
return diffNestedArrayProperty(
updatePayload,
prevProp,
nextProp,
2017-03-14 08:05:18 +08:00
validAttributes,
2016-03-25 06:40:52 +08:00
);
}
if (Array.isArray(prevProp)) {
return diffProperties(
updatePayload,
// $FlowFixMe - We know that this is always an object when the input is.
flattenStyle(prevProp),
// $FlowFixMe - We know that this isn't an array because of above flow.
nextProp,
2017-03-14 08:05:18 +08:00
validAttributes,
2016-03-25 06:40:52 +08:00
);
}
return diffProperties(
updatePayload,
prevProp,
2017-03-14 08:05:18 +08:00
// $FlowFixMe - We know that this is always an object when the input is.
2016-03-25 06:40:52 +08:00
flattenStyle(nextProp),
2017-03-14 08:05:18 +08:00
validAttributes,
2016-03-25 06:40:52 +08:00
);
}
/**
* addNestedProperty takes a single set of props and valid attribute
* attribute configurations. It processes each prop and adds it to the
* updatePayload.
*/
function addNestedProperty(
updatePayload: null | Object,
2016-03-25 06:40:52 +08:00
nextProp: NestedNode,
validAttributes: AttributeConfiguration<>,
2016-03-25 06:40:52 +08:00
) {
if (!nextProp) {
return updatePayload;
}
if (!Array.isArray(nextProp)) {
// Add each property of the leaf.
return addProperties(updatePayload, nextProp, validAttributes);
2016-03-25 06:40:52 +08:00
}
for (let i = 0; i < nextProp.length; i++) {
2016-03-25 06:40:52 +08:00
// Add all the properties of the array.
updatePayload = addNestedProperty(
updatePayload,
nextProp[i],
2017-03-14 08:05:18 +08:00
validAttributes,
2016-03-25 06:40:52 +08:00
);
}
return updatePayload;
}
/**
* clearNestedProperty takes a single set of props and valid attributes. It
* adds a null sentinel to the updatePayload, for each prop key.
*/
function clearNestedProperty(
updatePayload: null | Object,
2016-03-25 06:40:52 +08:00
prevProp: NestedNode,
validAttributes: AttributeConfiguration<>,
): null | Object {
2016-03-25 06:40:52 +08:00
if (!prevProp) {
return updatePayload;
}
if (!Array.isArray(prevProp)) {
// Add each property of the leaf.
return clearProperties(updatePayload, prevProp, validAttributes);
2016-03-25 06:40:52 +08:00
}
for (let i = 0; i < prevProp.length; i++) {
2016-03-25 06:40:52 +08:00
// Add all the properties of the array.
updatePayload = clearNestedProperty(
updatePayload,
prevProp[i],
2017-03-14 08:05:18 +08:00
validAttributes,
2016-03-25 06:40:52 +08:00
);
}
return updatePayload;
}
/**
* diffProperties takes two sets of props and a set of valid attributes
* and write to updatePayload the values that changed or were deleted.
* If no updatePayload is provided, a new one is created and returned if
* anything changed.
*/
function diffProperties(
updatePayload: null | Object,
2016-03-25 06:40:52 +08:00
prevProps: Object,
nextProps: Object,
validAttributes: AttributeConfiguration<>,
): null | Object {
let attributeConfig;
let nextProp;
let prevProp;
2016-03-25 06:40:52 +08:00
for (const propKey in nextProps) {
2016-03-25 06:40:52 +08:00
attributeConfig = validAttributes[propKey];
if (!attributeConfig) {
continue; // not a valid native prop
}
prevProp = prevProps[propKey];
nextProp = nextProps[propKey];
// functions are converted to booleans as markers that the associated
// events should be sent from native.
if (typeof nextProp === 'function') {
2017-03-14 08:05:18 +08:00
nextProp = (true: any);
2016-03-25 06:40:52 +08:00
// If nextProp is not a function, then don't bother changing prevProp
// since nextProp will win and go into the updatePayload regardless.
if (typeof prevProp === 'function') {
2017-03-14 08:05:18 +08:00
prevProp = (true: any);
2016-03-25 06:40:52 +08:00
}
}
// An explicit value of undefined is treated as a null because it overrides
// any other preceding value.
2016-03-25 06:40:52 +08:00
if (typeof nextProp === 'undefined') {
2017-03-14 08:05:18 +08:00
nextProp = (null: any);
2016-03-25 06:40:52 +08:00
if (typeof prevProp === 'undefined') {
2017-03-14 08:05:18 +08:00
prevProp = (null: any);
2016-03-25 06:40:52 +08:00
}
}
if (removedKeys) {
removedKeys[propKey] = false;
}
if (updatePayload && updatePayload[propKey] !== undefined) {
2016-03-25 06:40:52 +08:00
// Something else already triggered an update to this key because another
// value diffed. Since we're now later in the nested arrays our value is
// more important so we need to calculate it and override the existing
// value. It doesn't matter if nothing changed, we'll set it anyway.
// Pattern match on: attributeConfig
if (typeof attributeConfig !== 'object') {
// case: !Object is the default case
updatePayload[propKey] = nextProp;
2017-03-14 08:05:18 +08:00
} else if (
typeof attributeConfig.diff === 'function' ||
typeof attributeConfig.process === 'function'
) {
2016-03-25 06:40:52 +08:00
// case: CustomAttributeConfiguration
const nextValue =
typeof attributeConfig.process === 'function'
? attributeConfig.process(nextProp)
: nextProp;
updatePayload[propKey] = nextValue;
2016-03-25 06:40:52 +08:00
}
continue;
}
if (prevProp === nextProp) {
continue; // nothing changed
}
// Pattern match on: attributeConfig
if (typeof attributeConfig !== 'object') {
// case: !Object is the default case
if (defaultDiffer(prevProp, nextProp)) {
// a normal leaf has changed
(updatePayload || (updatePayload = {}))[propKey] = nextProp;
2016-03-25 06:40:52 +08:00
}
2017-03-14 08:05:18 +08:00
} else if (
typeof attributeConfig.diff === 'function' ||
typeof attributeConfig.process === 'function'
) {
2016-03-25 06:40:52 +08:00
// case: CustomAttributeConfiguration
const shouldUpdate =
2017-04-21 02:18:33 +08:00
prevProp === undefined ||
2017-03-14 08:05:18 +08:00
(typeof attributeConfig.diff === 'function'
? attributeConfig.diff(prevProp, nextProp)
: defaultDiffer(prevProp, nextProp));
2016-03-25 06:40:52 +08:00
if (shouldUpdate) {
const nextValue =
typeof attributeConfig.process === 'function'
? attributeConfig.process(nextProp)
: nextProp;
(updatePayload || (updatePayload = {}))[propKey] = nextValue;
2016-03-25 06:40:52 +08:00
}
} else {
// default: fallthrough case when nested properties are defined
removedKeys = null;
removedKeyCount = 0;
// We think that attributeConfig is not CustomAttributeConfiguration at
// this point so we assume it must be AttributeConfiguration.
2016-03-25 06:40:52 +08:00
updatePayload = diffNestedProperty(
updatePayload,
prevProp,
nextProp,
((attributeConfig: any): AttributeConfiguration<>),
2016-03-25 06:40:52 +08:00
);
if (removedKeyCount > 0 && updatePayload) {
restoreDeletedValuesInNestedArray(
updatePayload,
nextProp,
((attributeConfig: any): AttributeConfiguration<>),
2016-03-25 06:40:52 +08:00
);
removedKeys = null;
}
}
}
// Also iterate through all the previous props to catch any that have been
// removed and make sure native gets the signal so it can reset them to the
// default.
for (const propKey in prevProps) {
2016-03-25 06:40:52 +08:00
if (nextProps[propKey] !== undefined) {
continue; // we've already covered this key in the previous pass
}
attributeConfig = validAttributes[propKey];
if (!attributeConfig) {
continue; // not a valid native prop
}
if (updatePayload && updatePayload[propKey] !== undefined) {
2016-03-25 06:40:52 +08:00
// This was already updated to a diff result earlier.
continue;
}
prevProp = prevProps[propKey];
if (prevProp === undefined) {
continue; // was already empty anyway
}
// Pattern match on: attributeConfig
2017-03-14 08:05:18 +08:00
if (
typeof attributeConfig !== 'object' ||
typeof attributeConfig.diff === 'function' ||
typeof attributeConfig.process === 'function'
) {
2016-03-25 06:40:52 +08:00
// case: CustomAttributeConfiguration | !Object
// Flag the leaf property for removal by sending a sentinel.
(updatePayload || (updatePayload = {}))[propKey] = null;
2016-03-25 06:40:52 +08:00
if (!removedKeys) {
removedKeys = {};
}
if (!removedKeys[propKey]) {
removedKeys[propKey] = true;
removedKeyCount++;
}
} else {
// default:
// This is a nested attribute configuration where all the properties
// were removed so we need to go through and clear out all of them.
updatePayload = clearNestedProperty(
updatePayload,
prevProp,
((attributeConfig: any): AttributeConfiguration<>),
2016-03-25 06:40:52 +08:00
);
}
}
return updatePayload;
}
/**
* addProperties adds all the valid props to the payload after being processed.
*/
function addProperties(
updatePayload: null | Object,
2016-03-25 06:40:52 +08:00
props: Object,
validAttributes: AttributeConfiguration<>,
): null | Object {
2016-03-25 06:40:52 +08:00
// TODO: Fast path
return diffProperties(updatePayload, emptyObject, props, validAttributes);
}
/**
* clearProperties clears all the previous props by adding a null sentinel
* to the payload for each valid key.
*/
function clearProperties(
updatePayload: null | Object,
2016-03-25 06:40:52 +08:00
prevProps: Object,
validAttributes: AttributeConfiguration<>,
): null | Object {
2016-03-25 06:40:52 +08:00
// TODO: Fast path
return diffProperties(updatePayload, prevProps, emptyObject, validAttributes);
}
export function create(
props: Object,
validAttributes: AttributeConfiguration<>,
): null | Object {
return addProperties(
null, // updatePayload
props,
validAttributes,
);
}
2016-03-25 06:40:52 +08:00
export function diff(
prevProps: Object,
nextProps: Object,
validAttributes: AttributeConfiguration<>,
): null | Object {
return diffProperties(
null, // updatePayload
prevProps,
nextProps,
validAttributes,
);
}