mirror of
https://github.com/status-im/react-native.git
synced 2025-01-17 21:11:45 +00:00
6c5024ec58
Summary: Concolidate the creation of the "update payload" into ReactNativeAttributePayload which now has a create and a diff version. The create version can be used by both mountComponent and setNativeProps. This means that diffRawProperties moves into ReactNativeAttributePayload. Instead of storing previousFlattenedStyle as memoized state in the component tree, I recalculate it every time. This allows better use of the generational GC. However, it is still probably a fairly expensive technique so I will follow it up with a diff that walks both nested array trees to do the diffing in a follow up. This is the first diff of several steps. @public Reviewed By: @vjeux Differential Revision: D2440644 fb-gh-sync-id: 1d0da4f6e2bf716f33e119df947c044abb918471
193 lines
5.9 KiB
JavaScript
193 lines
5.9 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule ReactNativeAttributePayload
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
var ReactNativeStyleAttributes = require('ReactNativeStyleAttributes');
|
|
|
|
var deepDiffer = require('deepDiffer');
|
|
var styleDiffer = require('styleDiffer');
|
|
var deepFreezeAndThrowOnMutationInDev = require('deepFreezeAndThrowOnMutationInDev');
|
|
var flattenStyle = require('flattenStyle');
|
|
var precomputeStyle = require('precomputeStyle');
|
|
|
|
/**
|
|
* diffRawProperties takes two sets of props and a set of valid attributes
|
|
* and write to updatePayload the values that changed or were deleted
|
|
*
|
|
* @param {?object} updatePayload Overriden with the props that changed.
|
|
* @param {!object} prevProps Previous properties to diff against current
|
|
* properties. These properties are as supplied to component construction.
|
|
* @param {!object} prevProps Next "current" properties to diff against
|
|
* previous. These properties are as supplied to component construction.
|
|
* @return {?object}
|
|
*/
|
|
function diffRawProperties(
|
|
updatePayload: ?Object,
|
|
prevProps: ?Object,
|
|
nextProps: ?Object,
|
|
validAttributes: Object
|
|
): ?Object {
|
|
var validAttributeConfig;
|
|
var nextProp;
|
|
var prevProp;
|
|
var isScalar;
|
|
var shouldUpdate;
|
|
var differ;
|
|
|
|
if (nextProps) {
|
|
for (var propKey in nextProps) {
|
|
validAttributeConfig = validAttributes[propKey];
|
|
if (!validAttributeConfig) {
|
|
continue; // not a valid native prop
|
|
}
|
|
prevProp = prevProps && prevProps[propKey];
|
|
nextProp = nextProps[propKey];
|
|
|
|
// functions are converted to booleans as markers that the associated
|
|
// events should be sent from native.
|
|
if (typeof prevProp === 'function') {
|
|
prevProp = true;
|
|
}
|
|
if (typeof nextProp === 'function') {
|
|
nextProp = true;
|
|
}
|
|
|
|
if (prevProp !== nextProp) {
|
|
// Scalars and new props are always updated. Objects use deepDiffer by
|
|
// default, but can be optimized with custom differs.
|
|
differ = validAttributeConfig.diff || deepDiffer;
|
|
isScalar = typeof nextProp !== 'object' || nextProp === null;
|
|
shouldUpdate = isScalar || !prevProp || differ(prevProp, nextProp);
|
|
if (shouldUpdate) {
|
|
updatePayload = updatePayload || {};
|
|
updatePayload[propKey] = nextProp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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.
|
|
if (prevProps) {
|
|
for (var propKey in prevProps) {
|
|
validAttributeConfig = validAttributes[propKey];
|
|
if (!validAttributeConfig) {
|
|
continue; // not a valid native prop
|
|
}
|
|
if (updatePayload && updatePayload[propKey] !== undefined) {
|
|
continue; // Prop already specified
|
|
}
|
|
prevProp = prevProps[propKey];
|
|
nextProp = nextProps && nextProps[propKey];
|
|
|
|
// functions are converted to booleans as markers that the associated
|
|
// events should be sent from native.
|
|
if (typeof prevProp === 'function') {
|
|
prevProp = true;
|
|
}
|
|
if (typeof nextProp === 'function') {
|
|
nextProp = true;
|
|
}
|
|
|
|
if (prevProp !== nextProp) {
|
|
if (nextProp === undefined) {
|
|
nextProp = null; // null is a sentinel we explicitly send to native
|
|
}
|
|
// Scalars and new props are always updated. Objects use deepDiffer by
|
|
// default, but can be optimized with custom differs.
|
|
differ = validAttributeConfig.diff || deepDiffer;
|
|
isScalar = typeof nextProp !== 'object' || nextProp === null;
|
|
shouldUpdate =
|
|
isScalar &&
|
|
prevProp !== nextProp ||
|
|
differ(prevProp, nextProp);
|
|
if (shouldUpdate) {
|
|
updatePayload = updatePayload || {};
|
|
updatePayload[propKey] = nextProp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return updatePayload;
|
|
}
|
|
|
|
var ReactNativeAttributePayload = {
|
|
|
|
create: function(
|
|
props : Object,
|
|
validAttributes : Object
|
|
) : ?Object {
|
|
return ReactNativeAttributePayload.diff({}, props, validAttributes);
|
|
},
|
|
|
|
diff: function(
|
|
prevProps : Object,
|
|
nextProps : Object,
|
|
validAttributes : Object
|
|
) : ?Object {
|
|
|
|
if (__DEV__) {
|
|
for (var key in nextProps) {
|
|
if (nextProps.hasOwnProperty(key) &&
|
|
nextProps[key] &&
|
|
validAttributes[key]) {
|
|
deepFreezeAndThrowOnMutationInDev(nextProps[key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
var updatePayload = diffRawProperties(
|
|
null, // updatePayload
|
|
prevProps,
|
|
nextProps,
|
|
validAttributes
|
|
);
|
|
|
|
for (var key in updatePayload) {
|
|
var process = validAttributes[key] && validAttributes[key].process;
|
|
if (process) {
|
|
updatePayload[key] = process(updatePayload[key]);
|
|
}
|
|
}
|
|
|
|
// The style property is a deeply nested element which includes numbers
|
|
// to represent static objects. Most of the time, it doesn't change across
|
|
// renders, so it's faster to spend the time checking if it is different
|
|
// before actually doing the expensive flattening operation in order to
|
|
// compute the diff.
|
|
if (styleDiffer(nextProps.style, prevProps.style)) {
|
|
// TODO: Use a cached copy of previousFlattenedStyle, or walk both
|
|
// props in parallel.
|
|
var previousFlattenedStyle = precomputeStyle(
|
|
flattenStyle(prevProps.style),
|
|
validAttributes
|
|
);
|
|
var nextFlattenedStyle = precomputeStyle(
|
|
flattenStyle(nextProps.style),
|
|
validAttributes
|
|
);
|
|
updatePayload = diffRawProperties(
|
|
updatePayload,
|
|
previousFlattenedStyle,
|
|
nextFlattenedStyle,
|
|
ReactNativeStyleAttributes
|
|
);
|
|
}
|
|
|
|
return updatePayload;
|
|
}
|
|
|
|
};
|
|
|
|
module.exports = ReactNativeAttributePayload;
|