2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* 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.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-10-06 02:19:16 +00:00
|
|
|
* @providesModule ReactNativeAttributePayload
|
2015-03-26 00:49:46 +00:00
|
|
|
* @flow
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2015-10-06 22:19:59 +00:00
|
|
|
var Platform = require('Platform');
|
2016-03-24 22:12:36 +00:00
|
|
|
var ReactNativePropRegistry = require('ReactNativePropRegistry');
|
2015-10-06 22:19:59 +00:00
|
|
|
|
2015-07-15 00:06:03 +00:00
|
|
|
var deepDiffer = require('deepDiffer');
|
2015-10-06 02:19:16 +00:00
|
|
|
var flattenStyle = require('flattenStyle');
|
2015-07-15 00:06:03 +00:00
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
var emptyObject = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 AttributeDiffer = (prevProp: mixed, nextProp: mixed) => boolean;
|
2015-10-06 03:21:48 +00:00
|
|
|
type AttributePreprocessor = (nextProp: mixed) => mixed;
|
|
|
|
|
|
|
|
type CustomAttributeConfiguration =
|
2016-03-24 22:12:36 +00:00
|
|
|
{ diff: AttributeDiffer, process: AttributePreprocessor } |
|
|
|
|
{ diff: AttributeDiffer } |
|
|
|
|
{ process: AttributePreprocessor };
|
2015-10-06 03:21:48 +00:00
|
|
|
|
|
|
|
type AttributeConfiguration =
|
2016-03-24 22:12:36 +00:00
|
|
|
{ [key: string]: (
|
2015-10-06 03:21:48 +00:00
|
|
|
CustomAttributeConfiguration | AttributeConfiguration /*| boolean*/
|
|
|
|
) };
|
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
type NestedNode = Array<NestedNode> | Object | number;
|
|
|
|
|
|
|
|
// Tracks removed keys
|
|
|
|
var removedKeys = null;
|
|
|
|
var removedKeyCount = 0;
|
|
|
|
|
|
|
|
function translateKey(propKey: string) : string {
|
2015-10-06 22:19:59 +00:00
|
|
|
if (propKey === 'transform') {
|
|
|
|
// We currently special case the key for `transform`. iOS uses the
|
|
|
|
// transformMatrix name and Android uses the decomposedMatrix name.
|
|
|
|
// TODO: We could unify these names and just use the name `transform`
|
|
|
|
// all the time. Just need to update the native side.
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
return 'decomposedMatrix';
|
|
|
|
} else {
|
|
|
|
return 'transformMatrix';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return propKey;
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:21:48 +00:00
|
|
|
function defaultDiffer(prevProp: mixed, nextProp: mixed) : boolean {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
function resolveObject(idOrObject: number | Object) : Object {
|
|
|
|
if (typeof idOrObject === 'number') {
|
|
|
|
return ReactNativePropRegistry.getByID(idOrObject);
|
|
|
|
}
|
|
|
|
return idOrObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
function restoreDeletedValuesInNestedArray(
|
|
|
|
updatePayload: Object,
|
|
|
|
node: NestedNode,
|
|
|
|
validAttributes: AttributeConfiguration
|
|
|
|
) {
|
|
|
|
if (Array.isArray(node)) {
|
|
|
|
var i = node.length;
|
|
|
|
while (i-- && removedKeyCount > 0) {
|
|
|
|
restoreDeletedValuesInNestedArray(
|
|
|
|
updatePayload,
|
|
|
|
node[i],
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (node && removedKeyCount > 0) {
|
|
|
|
var obj = resolveObject(node);
|
|
|
|
for (var propKey in removedKeys) {
|
|
|
|
if (!removedKeys[propKey]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var nextProp = obj[propKey];
|
|
|
|
if (nextProp === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var attributeConfig = validAttributes[propKey];
|
|
|
|
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;
|
|
|
|
} else if (typeof attributeConfig.diff === 'function' ||
|
|
|
|
typeof attributeConfig.process === 'function') {
|
|
|
|
// case: CustomAttributeConfiguration
|
|
|
|
var nextValue = typeof attributeConfig.process === 'function' ?
|
|
|
|
attributeConfig.process(nextProp) :
|
|
|
|
nextProp;
|
|
|
|
updatePayload[propKey] = nextValue;
|
|
|
|
}
|
|
|
|
removedKeys[propKey] = false;
|
|
|
|
removedKeyCount--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function diffNestedArrayProperty(
|
|
|
|
updatePayload:? Object,
|
|
|
|
prevArray: Array<NestedNode>,
|
|
|
|
nextArray: Array<NestedNode>,
|
|
|
|
validAttributes: AttributeConfiguration
|
2015-10-06 03:21:48 +00:00
|
|
|
) : ?Object {
|
2016-03-24 22:12:36 +00:00
|
|
|
var minLength = prevArray.length < nextArray.length ?
|
|
|
|
prevArray.length :
|
|
|
|
nextArray.length;
|
|
|
|
var i;
|
|
|
|
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],
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
for (; i < prevArray.length; i++) {
|
|
|
|
// Clear out all remaining properties.
|
|
|
|
updatePayload = clearNestedProperty(
|
|
|
|
updatePayload,
|
|
|
|
prevArray[i],
|
|
|
|
validAttributes
|
|
|
|
);
|
2015-10-06 03:21:48 +00:00
|
|
|
}
|
2016-03-24 22:12:36 +00:00
|
|
|
for (; i < nextArray.length; i++) {
|
|
|
|
// Add all remaining properties.
|
|
|
|
updatePayload = addNestedProperty(
|
|
|
|
updatePayload,
|
|
|
|
nextArray[i],
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return updatePayload;
|
|
|
|
}
|
2015-10-06 03:21:48 +00:00
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
function diffNestedProperty(
|
|
|
|
updatePayload:? Object,
|
|
|
|
prevProp: NestedNode,
|
|
|
|
nextProp: NestedNode,
|
|
|
|
validAttributes: AttributeConfiguration
|
|
|
|
) : ?Object {
|
2015-10-06 03:21:48 +00:00
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
if (!updatePayload && prevProp === nextProp) {
|
|
|
|
// If no properties have been added, then we can bail out quickly on object
|
|
|
|
// equality.
|
|
|
|
return updatePayload;
|
|
|
|
}
|
2015-10-06 03:21:48 +00:00
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
if (!prevProp || !nextProp) {
|
|
|
|
if (nextProp) {
|
|
|
|
return addNestedProperty(
|
2015-10-06 03:21:48 +00:00
|
|
|
updatePayload,
|
2016-03-24 22:12:36 +00:00
|
|
|
nextProp,
|
2015-10-06 03:21:48 +00:00
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
2016-03-24 22:12:36 +00:00
|
|
|
if (prevProp) {
|
|
|
|
return clearNestedProperty(
|
2015-10-06 03:21:48 +00:00
|
|
|
updatePayload,
|
2016-03-24 22:12:36 +00:00
|
|
|
prevProp,
|
2015-10-06 03:21:48 +00:00
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return updatePayload;
|
|
|
|
}
|
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
if (!Array.isArray(prevProp) && !Array.isArray(nextProp)) {
|
|
|
|
// Both are leaves, we can diff the leaves.
|
|
|
|
return diffProperties(
|
|
|
|
updatePayload,
|
|
|
|
resolveObject(prevProp),
|
|
|
|
resolveObject(nextProp),
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(prevProp) && Array.isArray(nextProp)) {
|
|
|
|
// Both are arrays, we can diff the arrays.
|
|
|
|
return diffNestedArrayProperty(
|
|
|
|
updatePayload,
|
|
|
|
prevProp,
|
|
|
|
nextProp,
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
resolveObject(nextProp),
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:21:48 +00:00
|
|
|
return diffProperties(
|
|
|
|
updatePayload,
|
2016-03-24 22:12:36 +00:00
|
|
|
resolveObject(prevProp),
|
|
|
|
// $FlowFixMe - We know that this is always an object when the input is.
|
|
|
|
flattenStyle(nextProp),
|
2015-10-06 03:21:48 +00:00
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-10-06 03:21:48 +00: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(
|
2016-03-24 22:12:36 +00:00
|
|
|
updatePayload:? Object,
|
|
|
|
nextProp: NestedNode,
|
|
|
|
validAttributes: AttributeConfiguration
|
2015-10-06 03:21:48 +00:00
|
|
|
) {
|
2016-03-24 22:12:36 +00:00
|
|
|
if (!nextProp) {
|
|
|
|
return updatePayload;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(nextProp)) {
|
|
|
|
// Add each property of the leaf.
|
|
|
|
return addProperties(
|
|
|
|
updatePayload,
|
|
|
|
resolveObject(nextProp),
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < nextProp.length; i++) {
|
|
|
|
// Add all the properties of the array.
|
|
|
|
updatePayload = addNestedProperty(
|
|
|
|
updatePayload,
|
|
|
|
nextProp[i],
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return updatePayload;
|
2015-10-06 03:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clearNestedProperty takes a single set of props and valid attributes. It
|
|
|
|
* adds a null sentinel to the updatePayload, for each prop key.
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2015-10-06 03:21:48 +00:00
|
|
|
function clearNestedProperty(
|
2016-03-24 22:12:36 +00:00
|
|
|
updatePayload:? Object,
|
|
|
|
prevProp: NestedNode,
|
|
|
|
validAttributes: AttributeConfiguration
|
2015-10-06 03:21:48 +00:00
|
|
|
) : ?Object {
|
2016-03-24 22:12:36 +00:00
|
|
|
if (!prevProp) {
|
|
|
|
return updatePayload;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(prevProp)) {
|
|
|
|
// Add each property of the leaf.
|
|
|
|
return clearProperties(
|
|
|
|
updatePayload,
|
|
|
|
resolveObject(prevProp),
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < prevProp.length; i++) {
|
|
|
|
// Add all the properties of the array.
|
|
|
|
updatePayload = clearNestedProperty(
|
|
|
|
updatePayload,
|
|
|
|
prevProp[i],
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return updatePayload;
|
2015-10-06 03:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(
|
2016-03-24 22:12:36 +00:00
|
|
|
updatePayload: ?Object,
|
|
|
|
prevProps: Object,
|
|
|
|
nextProps: Object,
|
|
|
|
validAttributes: AttributeConfiguration
|
2015-03-26 00:49:46 +00:00
|
|
|
): ?Object {
|
2015-12-19 01:03:59 +00:00
|
|
|
var attributeConfig : ?(CustomAttributeConfiguration | AttributeConfiguration);
|
2015-02-20 04:10:52 +00:00
|
|
|
var nextProp;
|
|
|
|
var prevProp;
|
2016-03-24 22:12:36 +00:00
|
|
|
var altKey;
|
[ReactNative] Introduce onLayout events
Summary:
Simply add an `onLayout` callback to a native view component, and the callback
will be invoked with the current layout information when the view is mounted and
whenever the layout changes.
The only limitation is that scroll position and other stuff the layout system
isn't aware of is not taken into account. This is because onLayout events
wouldn't be triggered for these changes and if they are desired they should be
tracked separately (e.g. with `onScroll`) and combined.
Also fixes some bugs with LayoutAnimation callbacks.
@public
Test Plan:
- Run new LayoutEventsExample in UIExplorer and see it work correctly.
- New integration test passes internally (IntegrationTest project seems busted).
- New jest test case passes.
{F22318433}
```
2015-05-06 15:45:05.848 [info][tid:com.facebook.React.JavaScript] "Running application "UIExplorerApp" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF"
2015-05-06 15:45:05.881 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":123,"x":12.5,"width":140.5,"height":18}}
2015-05-06 15:45:05.882 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":122,"width":50,"height":50}}
2015-05-06 15:45:05.883 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":70.5,"x":20,"width":294,"height":204}}
2015-05-06 15:45:05.897 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":206.5,"x":12.5,"width":140.5,"height":18}}
2015-05-06 15:45:05.897 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":70.5,"x":20,"width":294,"height":287.5}}
2015-05-06 15:45:09.847 [info][tid:com.facebook.React.JavaScript] "layout animation done."
2015-05-06 15:45:09.847 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":82,"width":50,"height":50}}
2015-05-06 15:45:09.848 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":110.5,"x":60,"width":214,"height":287.5}}
2015-05-06 15:45:09.862 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":206.5,"x":12.5,"width":120,"height":68}}
2015-05-06 15:45:09.863 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":55,"width":50,"height":50}}
2015-05-06 15:45:09.863 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":128,"x":60,"width":160,"height":337.5}}
```
2015-05-07 19:11:02 +00:00
|
|
|
|
2015-10-06 03:21:48 +00:00
|
|
|
for (var propKey in nextProps) {
|
|
|
|
attributeConfig = validAttributes[propKey];
|
|
|
|
if (!attributeConfig) {
|
|
|
|
continue; // not a valid native prop
|
|
|
|
}
|
2015-10-06 22:19:59 +00:00
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
altKey = translateKey(propKey);
|
2015-10-06 22:19:59 +00:00
|
|
|
if (!validAttributes[altKey]) {
|
|
|
|
// If there is no config for the alternative, bail out. Helps ART.
|
|
|
|
altKey = propKey;
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:21:48 +00:00
|
|
|
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') {
|
2016-03-24 22:12:36 +00:00
|
|
|
nextProp = (true : any);
|
2015-10-06 03:21:48 +00:00
|
|
|
// If nextProp is not a function, then don't bother changing prevProp
|
|
|
|
// since nextProp will win and go into the updatePayload regardless.
|
[ReactNative] Introduce onLayout events
Summary:
Simply add an `onLayout` callback to a native view component, and the callback
will be invoked with the current layout information when the view is mounted and
whenever the layout changes.
The only limitation is that scroll position and other stuff the layout system
isn't aware of is not taken into account. This is because onLayout events
wouldn't be triggered for these changes and if they are desired they should be
tracked separately (e.g. with `onScroll`) and combined.
Also fixes some bugs with LayoutAnimation callbacks.
@public
Test Plan:
- Run new LayoutEventsExample in UIExplorer and see it work correctly.
- New integration test passes internally (IntegrationTest project seems busted).
- New jest test case passes.
{F22318433}
```
2015-05-06 15:45:05.848 [info][tid:com.facebook.React.JavaScript] "Running application "UIExplorerApp" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF"
2015-05-06 15:45:05.881 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":123,"x":12.5,"width":140.5,"height":18}}
2015-05-06 15:45:05.882 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":122,"width":50,"height":50}}
2015-05-06 15:45:05.883 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":70.5,"x":20,"width":294,"height":204}}
2015-05-06 15:45:05.897 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":206.5,"x":12.5,"width":140.5,"height":18}}
2015-05-06 15:45:05.897 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":70.5,"x":20,"width":294,"height":287.5}}
2015-05-06 15:45:09.847 [info][tid:com.facebook.React.JavaScript] "layout animation done."
2015-05-06 15:45:09.847 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":82,"width":50,"height":50}}
2015-05-06 15:45:09.848 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":110.5,"x":60,"width":214,"height":287.5}}
2015-05-06 15:45:09.862 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":206.5,"x":12.5,"width":120,"height":68}}
2015-05-06 15:45:09.863 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":55,"width":50,"height":50}}
2015-05-06 15:45:09.863 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":128,"x":60,"width":160,"height":337.5}}
```
2015-05-07 19:11:02 +00:00
|
|
|
if (typeof prevProp === 'function') {
|
2016-03-24 22:12:36 +00:00
|
|
|
prevProp = (true : any);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// An explicit value of undefined is treated as a null because it overrides
|
|
|
|
// any other preceeding value.
|
|
|
|
if (typeof nextProp === 'undefined') {
|
|
|
|
nextProp = (null : any);
|
|
|
|
if (typeof prevProp === 'undefined') {
|
|
|
|
prevProp = (null : any);
|
[ReactNative] Introduce onLayout events
Summary:
Simply add an `onLayout` callback to a native view component, and the callback
will be invoked with the current layout information when the view is mounted and
whenever the layout changes.
The only limitation is that scroll position and other stuff the layout system
isn't aware of is not taken into account. This is because onLayout events
wouldn't be triggered for these changes and if they are desired they should be
tracked separately (e.g. with `onScroll`) and combined.
Also fixes some bugs with LayoutAnimation callbacks.
@public
Test Plan:
- Run new LayoutEventsExample in UIExplorer and see it work correctly.
- New integration test passes internally (IntegrationTest project seems busted).
- New jest test case passes.
{F22318433}
```
2015-05-06 15:45:05.848 [info][tid:com.facebook.React.JavaScript] "Running application "UIExplorerApp" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF"
2015-05-06 15:45:05.881 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":123,"x":12.5,"width":140.5,"height":18}}
2015-05-06 15:45:05.882 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":122,"width":50,"height":50}}
2015-05-06 15:45:05.883 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":70.5,"x":20,"width":294,"height":204}}
2015-05-06 15:45:05.897 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":206.5,"x":12.5,"width":140.5,"height":18}}
2015-05-06 15:45:05.897 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":70.5,"x":20,"width":294,"height":287.5}}
2015-05-06 15:45:09.847 [info][tid:com.facebook.React.JavaScript] "layout animation done."
2015-05-06 15:45:09.847 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":82,"width":50,"height":50}}
2015-05-06 15:45:09.848 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":110.5,"x":60,"width":214,"height":287.5}}
2015-05-06 15:45:09.862 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":206.5,"x":12.5,"width":120,"height":68}}
2015-05-06 15:45:09.863 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":55,"width":50,"height":50}}
2015-05-06 15:45:09.863 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":128,"x":60,"width":160,"height":337.5}}
```
2015-05-07 19:11:02 +00:00
|
|
|
}
|
2015-10-06 03:21:48 +00:00
|
|
|
}
|
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
if (removedKeys) {
|
|
|
|
removedKeys[propKey] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updatePayload && updatePayload[altKey] !== undefined) {
|
|
|
|
// 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[altKey] = nextProp;
|
|
|
|
} else if (typeof attributeConfig.diff === 'function' ||
|
|
|
|
typeof attributeConfig.process === 'function') {
|
|
|
|
// case: CustomAttributeConfiguration
|
|
|
|
var nextValue = typeof attributeConfig.process === 'function' ?
|
|
|
|
attributeConfig.process(nextProp) :
|
|
|
|
nextProp;
|
|
|
|
updatePayload[altKey] = nextValue;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:21:48 +00:00
|
|
|
if (prevProp === nextProp) {
|
|
|
|
continue; // nothing changed
|
|
|
|
}
|
[ReactNative] Introduce onLayout events
Summary:
Simply add an `onLayout` callback to a native view component, and the callback
will be invoked with the current layout information when the view is mounted and
whenever the layout changes.
The only limitation is that scroll position and other stuff the layout system
isn't aware of is not taken into account. This is because onLayout events
wouldn't be triggered for these changes and if they are desired they should be
tracked separately (e.g. with `onScroll`) and combined.
Also fixes some bugs with LayoutAnimation callbacks.
@public
Test Plan:
- Run new LayoutEventsExample in UIExplorer and see it work correctly.
- New integration test passes internally (IntegrationTest project seems busted).
- New jest test case passes.
{F22318433}
```
2015-05-06 15:45:05.848 [info][tid:com.facebook.React.JavaScript] "Running application "UIExplorerApp" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF"
2015-05-06 15:45:05.881 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":123,"x":12.5,"width":140.5,"height":18}}
2015-05-06 15:45:05.882 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":122,"width":50,"height":50}}
2015-05-06 15:45:05.883 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":70.5,"x":20,"width":294,"height":204}}
2015-05-06 15:45:05.897 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":206.5,"x":12.5,"width":140.5,"height":18}}
2015-05-06 15:45:05.897 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":70.5,"x":20,"width":294,"height":287.5}}
2015-05-06 15:45:09.847 [info][tid:com.facebook.React.JavaScript] "layout animation done."
2015-05-06 15:45:09.847 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":82,"width":50,"height":50}}
2015-05-06 15:45:09.848 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":110.5,"x":60,"width":214,"height":287.5}}
2015-05-06 15:45:09.862 [info][tid:com.facebook.React.JavaScript] "received text layout event
", {"target":27,"layout":{"y":206.5,"x":12.5,"width":120,"height":68}}
2015-05-06 15:45:09.863 [info][tid:com.facebook.React.JavaScript] "received image layout event
", {"target":23,"layout":{"y":12.5,"x":55,"width":50,"height":50}}
2015-05-06 15:45:09.863 [info][tid:com.facebook.React.JavaScript] "received view layout event
", {"target":22,"layout":{"y":128,"x":60,"width":160,"height":337.5}}
```
2015-05-07 19:11:02 +00:00
|
|
|
|
2015-10-06 03:21:48 +00:00
|
|
|
// Pattern match on: attributeConfig
|
|
|
|
if (typeof attributeConfig !== 'object') {
|
|
|
|
// case: !Object is the default case
|
|
|
|
if (defaultDiffer(prevProp, nextProp)) {
|
|
|
|
// a normal leaf has changed
|
2015-10-06 22:19:59 +00:00
|
|
|
(updatePayload || (updatePayload = {}))[altKey] = nextProp;
|
2015-10-06 03:21:48 +00:00
|
|
|
}
|
|
|
|
} else if (typeof attributeConfig.diff === 'function' ||
|
|
|
|
typeof attributeConfig.process === 'function') {
|
|
|
|
// case: CustomAttributeConfiguration
|
|
|
|
var shouldUpdate = prevProp === undefined || (
|
|
|
|
typeof attributeConfig.diff === 'function' ?
|
|
|
|
attributeConfig.diff(prevProp, nextProp) :
|
|
|
|
defaultDiffer(prevProp, nextProp)
|
|
|
|
);
|
|
|
|
if (shouldUpdate) {
|
|
|
|
var nextValue = typeof attributeConfig.process === 'function' ?
|
|
|
|
attributeConfig.process(nextProp) :
|
|
|
|
nextProp;
|
2015-10-06 22:19:59 +00:00
|
|
|
(updatePayload || (updatePayload = {}))[altKey] = nextValue;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-10-06 03:21:48 +00:00
|
|
|
} else {
|
|
|
|
// default: fallthrough case when nested properties are defined
|
2016-03-24 22:12:36 +00:00
|
|
|
removedKeys = null;
|
|
|
|
removedKeyCount = 0;
|
2015-10-06 03:21:48 +00:00
|
|
|
updatePayload = diffNestedProperty(
|
|
|
|
updatePayload,
|
|
|
|
prevProp,
|
|
|
|
nextProp,
|
|
|
|
attributeConfig
|
|
|
|
);
|
2016-03-24 22:12:36 +00:00
|
|
|
if (removedKeyCount > 0 && updatePayload) {
|
|
|
|
restoreDeletedValuesInNestedArray(
|
|
|
|
updatePayload,
|
|
|
|
nextProp,
|
|
|
|
attributeConfig
|
|
|
|
);
|
|
|
|
removedKeys = null;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-10-06 03:21:48 +00:00
|
|
|
for (var propKey in prevProps) {
|
|
|
|
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
|
|
|
|
}
|
2015-10-06 22:19:59 +00:00
|
|
|
|
2016-03-24 22:12:36 +00:00
|
|
|
altKey = translateKey(propKey);
|
|
|
|
if (!attributeConfig[altKey]) {
|
|
|
|
// If there is no config for the alternative, bail out. Helps ART.
|
|
|
|
altKey = propKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updatePayload && updatePayload[altKey] !== undefined) {
|
|
|
|
// This was already updated to a diff result earlier.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:21:48 +00:00
|
|
|
prevProp = prevProps[propKey];
|
|
|
|
if (prevProp === undefined) {
|
|
|
|
continue; // was already empty anyway
|
|
|
|
}
|
|
|
|
// Pattern match on: attributeConfig
|
|
|
|
if (typeof attributeConfig !== 'object' ||
|
|
|
|
typeof attributeConfig.diff === 'function' ||
|
|
|
|
typeof attributeConfig.process === 'function') {
|
2015-10-06 22:19:59 +00:00
|
|
|
|
2015-10-06 03:21:48 +00:00
|
|
|
// case: CustomAttributeConfiguration | !Object
|
|
|
|
// Flag the leaf property for removal by sending a sentinel.
|
2016-03-24 22:12:36 +00:00
|
|
|
(updatePayload || (updatePayload = {}))[altKey] = null;
|
|
|
|
if (!removedKeys) {
|
|
|
|
removedKeys = {};
|
|
|
|
}
|
|
|
|
if (!removedKeys[propKey]) {
|
|
|
|
removedKeys[propKey] = true;
|
|
|
|
removedKeyCount++;
|
|
|
|
}
|
2015-10-06 03:21:48 +00:00
|
|
|
} 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
|
|
|
|
);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return updatePayload;
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:21:48 +00:00
|
|
|
/**
|
|
|
|
* addProperties adds all the valid props to the payload after being processed.
|
|
|
|
*/
|
|
|
|
function addProperties(
|
2016-03-24 22:12:36 +00:00
|
|
|
updatePayload: ?Object,
|
|
|
|
props: Object,
|
|
|
|
validAttributes: AttributeConfiguration
|
2015-10-06 03:21:48 +00:00
|
|
|
) : ?Object {
|
2016-03-24 22:12:36 +00:00
|
|
|
// TODO: Fast path
|
|
|
|
return diffProperties(updatePayload, emptyObject, props, validAttributes);
|
2015-10-06 03:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* clearProperties clears all the previous props by adding a null sentinel
|
|
|
|
* to the payload for each valid key.
|
|
|
|
*/
|
|
|
|
function clearProperties(
|
2016-03-24 22:12:36 +00:00
|
|
|
updatePayload: ?Object,
|
|
|
|
prevProps: Object,
|
|
|
|
validAttributes: AttributeConfiguration
|
2015-10-06 03:21:48 +00:00
|
|
|
) :? Object {
|
2016-03-24 22:12:36 +00:00
|
|
|
// TODO: Fast path
|
|
|
|
return diffProperties(updatePayload, prevProps, emptyObject, validAttributes);
|
2015-10-06 03:21:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 02:19:16 +00:00
|
|
|
var ReactNativeAttributePayload = {
|
|
|
|
|
|
|
|
create: function(
|
2016-03-24 22:12:36 +00:00
|
|
|
props: Object,
|
|
|
|
validAttributes: AttributeConfiguration
|
2015-10-06 02:19:16 +00:00
|
|
|
) : ?Object {
|
2015-10-06 03:21:48 +00:00
|
|
|
return addProperties(
|
|
|
|
null, // updatePayload
|
|
|
|
props,
|
|
|
|
validAttributes
|
|
|
|
);
|
2015-10-06 02:19:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
diff: function(
|
2016-03-24 22:12:36 +00:00
|
|
|
prevProps: Object,
|
|
|
|
nextProps: Object,
|
|
|
|
validAttributes: AttributeConfiguration
|
2015-10-06 02:19:16 +00:00
|
|
|
) : ?Object {
|
2015-10-06 03:21:48 +00:00
|
|
|
return diffProperties(
|
2015-10-06 02:19:16 +00:00
|
|
|
null, // updatePayload
|
|
|
|
prevProps,
|
|
|
|
nextProps,
|
|
|
|
validAttributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = ReactNativeAttributePayload;
|