react 15.0.1 → 15.0.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ * @providesModule ReactNative
10
+ *
11
+ */
12
+ 'use strict';
13
+
14
+ // Require ReactNativeDefaultInjection first for its side effects of setting up
15
+ // the JS environment
16
+
17
+ var ReactNativeComponentTree = require('./ReactNativeComponentTree');
18
+ var ReactNativeDefaultInjection = require('./ReactNativeDefaultInjection');
19
+
20
+ var ReactElement = require('./ReactElement');
21
+ var ReactNativeMount = require('./ReactNativeMount');
22
+ var ReactUpdates = require('./ReactUpdates');
23
+
24
+ var findNodeHandle = require('./findNodeHandle');
25
+
26
+ ReactNativeDefaultInjection.inject();
27
+
28
+ var render = function (element, mountInto, callback) {
29
+ return ReactNativeMount.renderComponent(element, mountInto, callback);
30
+ };
31
+
32
+ var ReactNative = {
33
+ hasReactNativeInitialized: false,
34
+ findNodeHandle: findNodeHandle,
35
+ render: render,
36
+ unmountComponentAtNode: ReactNativeMount.unmountComponentAtNode,
37
+
38
+ /* eslint-disable camelcase */
39
+ unstable_batchedUpdates: ReactUpdates.batchedUpdates,
40
+ /* eslint-enable camelcase */
41
+
42
+ unmountComponentAtNodeAndRemoveContainer: ReactNativeMount.unmountComponentAtNodeAndRemoveContainer
43
+ };
44
+
45
+ // Inject the runtime into a devtools global hook regardless of browser.
46
+ // Allows for debugging when the hook is injected on the page.
47
+ /* globals __REACT_DEVTOOLS_GLOBAL_HOOK__ */
48
+ if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' && typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.inject === 'function') {
49
+ __REACT_DEVTOOLS_GLOBAL_HOOK__.inject({
50
+ ComponentTree: {
51
+ getClosestInstanceFromNode: function (node) {
52
+ return ReactNativeComponentTree.getClosestInstanceFromNode(node);
53
+ },
54
+ getNodeFromInstance: function (inst) {
55
+ // inst is an internal instance (but could be a composite)
56
+ while (inst._renderedComponent) {
57
+ inst = inst._renderedComponent;
58
+ }
59
+ if (inst) {
60
+ return ReactNativeComponentTree.getNodeFromInstance(inst);
61
+ } else {
62
+ return null;
63
+ }
64
+ }
65
+ },
66
+ Mount: ReactNativeMount,
67
+ Reconciler: require('./ReactReconciler')
68
+ });
69
+ }
70
+
71
+ module.exports = ReactNative;
@@ -0,0 +1,397 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ * @providesModule ReactNativeAttributePayload
10
+ *
11
+ */
12
+ 'use strict';
13
+
14
+ var Platform = require('Platform');
15
+ var ReactNativePropRegistry = require('./ReactNativePropRegistry');
16
+
17
+ var deepDiffer = require('deepDiffer');
18
+ var flattenStyle = require('flattenStyle');
19
+
20
+ var emptyObject = {};
21
+
22
+ /**
23
+ * Create a payload that contains all the updates between two sets of props.
24
+ *
25
+ * These helpers are all encapsulated into a single module, because they use
26
+ * mutation as a performance optimization which leads to subtle shared
27
+ * dependencies between the code paths. To avoid this mutable state leaking
28
+ * across modules, I've kept them isolated to this module.
29
+ */
30
+
31
+ /*| boolean*/
32
+
33
+
34
+ // Tracks removed keys
35
+ var removedKeys = null;
36
+ var removedKeyCount = 0;
37
+
38
+ function translateKey(propKey) {
39
+ if (propKey === 'transform') {
40
+ // We currently special case the key for `transform`. iOS uses the
41
+ // transformMatrix name and Android uses the decomposedMatrix name.
42
+ // TODO: We could unify these names and just use the name `transform`
43
+ // all the time. Just need to update the native side.
44
+ if (Platform.OS === 'android') {
45
+ return 'decomposedMatrix';
46
+ } else {
47
+ return 'transformMatrix';
48
+ }
49
+ }
50
+ return propKey;
51
+ }
52
+
53
+ function defaultDiffer(prevProp, nextProp) {
54
+ if (typeof nextProp !== 'object' || nextProp === null) {
55
+ // Scalars have already been checked for equality
56
+ return true;
57
+ } else {
58
+ // For objects and arrays, the default diffing algorithm is a deep compare
59
+ return deepDiffer(prevProp, nextProp);
60
+ }
61
+ }
62
+
63
+ function resolveObject(idOrObject) {
64
+ if (typeof idOrObject === 'number') {
65
+ return ReactNativePropRegistry.getByID(idOrObject);
66
+ }
67
+ return idOrObject;
68
+ }
69
+
70
+ function restoreDeletedValuesInNestedArray(updatePayload, node, validAttributes) {
71
+ if (Array.isArray(node)) {
72
+ var i = node.length;
73
+ while (i-- && removedKeyCount > 0) {
74
+ restoreDeletedValuesInNestedArray(updatePayload, node[i], validAttributes);
75
+ }
76
+ } else if (node && removedKeyCount > 0) {
77
+ var obj = resolveObject(node);
78
+ for (var propKey in removedKeys) {
79
+ if (!removedKeys[propKey]) {
80
+ continue;
81
+ }
82
+ var nextProp = obj[propKey];
83
+ if (nextProp === undefined) {
84
+ continue;
85
+ }
86
+
87
+ var attributeConfig = validAttributes[propKey];
88
+ if (!attributeConfig) {
89
+ continue; // not a valid native prop
90
+ }
91
+
92
+ if (typeof nextProp === 'function') {
93
+ nextProp = true;
94
+ }
95
+ if (typeof nextProp === 'undefined') {
96
+ nextProp = null;
97
+ }
98
+
99
+ if (typeof attributeConfig !== 'object') {
100
+ // case: !Object is the default case
101
+ updatePayload[propKey] = nextProp;
102
+ } else if (typeof attributeConfig.diff === 'function' || typeof attributeConfig.process === 'function') {
103
+ // case: CustomAttributeConfiguration
104
+ var nextValue = typeof attributeConfig.process === 'function' ? attributeConfig.process(nextProp) : nextProp;
105
+ updatePayload[propKey] = nextValue;
106
+ }
107
+ removedKeys[propKey] = false;
108
+ removedKeyCount--;
109
+ }
110
+ }
111
+ }
112
+
113
+ function diffNestedArrayProperty(updatePayload, prevArray, nextArray, validAttributes) {
114
+ var minLength = prevArray.length < nextArray.length ? prevArray.length : nextArray.length;
115
+ var i;
116
+ for (i = 0; i < minLength; i++) {
117
+ // Diff any items in the array in the forward direction. Repeated keys
118
+ // will be overwritten by later values.
119
+ updatePayload = diffNestedProperty(updatePayload, prevArray[i], nextArray[i], validAttributes);
120
+ }
121
+ for (; i < prevArray.length; i++) {
122
+ // Clear out all remaining properties.
123
+ updatePayload = clearNestedProperty(updatePayload, prevArray[i], validAttributes);
124
+ }
125
+ for (; i < nextArray.length; i++) {
126
+ // Add all remaining properties.
127
+ updatePayload = addNestedProperty(updatePayload, nextArray[i], validAttributes);
128
+ }
129
+ return updatePayload;
130
+ }
131
+
132
+ function diffNestedProperty(updatePayload, prevProp, nextProp, validAttributes) {
133
+
134
+ if (!updatePayload && prevProp === nextProp) {
135
+ // If no properties have been added, then we can bail out quickly on object
136
+ // equality.
137
+ return updatePayload;
138
+ }
139
+
140
+ if (!prevProp || !nextProp) {
141
+ if (nextProp) {
142
+ return addNestedProperty(updatePayload, nextProp, validAttributes);
143
+ }
144
+ if (prevProp) {
145
+ return clearNestedProperty(updatePayload, prevProp, validAttributes);
146
+ }
147
+ return updatePayload;
148
+ }
149
+
150
+ if (!Array.isArray(prevProp) && !Array.isArray(nextProp)) {
151
+ // Both are leaves, we can diff the leaves.
152
+ return diffProperties(updatePayload, resolveObject(prevProp), resolveObject(nextProp), validAttributes);
153
+ }
154
+
155
+ if (Array.isArray(prevProp) && Array.isArray(nextProp)) {
156
+ // Both are arrays, we can diff the arrays.
157
+ return diffNestedArrayProperty(updatePayload, prevProp, nextProp, validAttributes);
158
+ }
159
+
160
+ if (Array.isArray(prevProp)) {
161
+ return diffProperties(updatePayload,
162
+ // $FlowFixMe - We know that this is always an object when the input is.
163
+ flattenStyle(prevProp),
164
+ // $FlowFixMe - We know that this isn't an array because of above flow.
165
+ resolveObject(nextProp), validAttributes);
166
+ }
167
+
168
+ return diffProperties(updatePayload, resolveObject(prevProp),
169
+ // $FlowFixMe - We know that this is always an object when the input is.
170
+ flattenStyle(nextProp), validAttributes);
171
+ }
172
+
173
+ /**
174
+ * addNestedProperty takes a single set of props and valid attribute
175
+ * attribute configurations. It processes each prop and adds it to the
176
+ * updatePayload.
177
+ */
178
+ function addNestedProperty(updatePayload, nextProp, validAttributes) {
179
+ if (!nextProp) {
180
+ return updatePayload;
181
+ }
182
+
183
+ if (!Array.isArray(nextProp)) {
184
+ // Add each property of the leaf.
185
+ return addProperties(updatePayload, resolveObject(nextProp), validAttributes);
186
+ }
187
+
188
+ for (var i = 0; i < nextProp.length; i++) {
189
+ // Add all the properties of the array.
190
+ updatePayload = addNestedProperty(updatePayload, nextProp[i], validAttributes);
191
+ }
192
+
193
+ return updatePayload;
194
+ }
195
+
196
+ /**
197
+ * clearNestedProperty takes a single set of props and valid attributes. It
198
+ * adds a null sentinel to the updatePayload, for each prop key.
199
+ */
200
+ function clearNestedProperty(updatePayload, prevProp, validAttributes) {
201
+ if (!prevProp) {
202
+ return updatePayload;
203
+ }
204
+
205
+ if (!Array.isArray(prevProp)) {
206
+ // Add each property of the leaf.
207
+ return clearProperties(updatePayload, resolveObject(prevProp), validAttributes);
208
+ }
209
+
210
+ for (var i = 0; i < prevProp.length; i++) {
211
+ // Add all the properties of the array.
212
+ updatePayload = clearNestedProperty(updatePayload, prevProp[i], validAttributes);
213
+ }
214
+ return updatePayload;
215
+ }
216
+
217
+ /**
218
+ * diffProperties takes two sets of props and a set of valid attributes
219
+ * and write to updatePayload the values that changed or were deleted.
220
+ * If no updatePayload is provided, a new one is created and returned if
221
+ * anything changed.
222
+ */
223
+ function diffProperties(updatePayload, prevProps, nextProps, validAttributes) {
224
+ var attributeConfig;
225
+ var nextProp;
226
+ var prevProp;
227
+ var altKey;
228
+
229
+ for (var propKey in nextProps) {
230
+ attributeConfig = validAttributes[propKey];
231
+ if (!attributeConfig) {
232
+ continue; // not a valid native prop
233
+ }
234
+
235
+ altKey = translateKey(propKey);
236
+ if (!validAttributes[altKey]) {
237
+ // If there is no config for the alternative, bail out. Helps ART.
238
+ altKey = propKey;
239
+ }
240
+
241
+ prevProp = prevProps[propKey];
242
+ nextProp = nextProps[propKey];
243
+
244
+ // functions are converted to booleans as markers that the associated
245
+ // events should be sent from native.
246
+ if (typeof nextProp === 'function') {
247
+ nextProp = true;
248
+ // If nextProp is not a function, then don't bother changing prevProp
249
+ // since nextProp will win and go into the updatePayload regardless.
250
+ if (typeof prevProp === 'function') {
251
+ prevProp = true;
252
+ }
253
+ }
254
+
255
+ // An explicit value of undefined is treated as a null because it overrides
256
+ // any other preceeding value.
257
+ if (typeof nextProp === 'undefined') {
258
+ nextProp = null;
259
+ if (typeof prevProp === 'undefined') {
260
+ prevProp = null;
261
+ }
262
+ }
263
+
264
+ if (removedKeys) {
265
+ removedKeys[propKey] = false;
266
+ }
267
+
268
+ if (updatePayload && updatePayload[altKey] !== undefined) {
269
+ // Something else already triggered an update to this key because another
270
+ // value diffed. Since we're now later in the nested arrays our value is
271
+ // more important so we need to calculate it and override the existing
272
+ // value. It doesn't matter if nothing changed, we'll set it anyway.
273
+
274
+ // Pattern match on: attributeConfig
275
+ if (typeof attributeConfig !== 'object') {
276
+ // case: !Object is the default case
277
+ updatePayload[altKey] = nextProp;
278
+ } else if (typeof attributeConfig.diff === 'function' || typeof attributeConfig.process === 'function') {
279
+ // case: CustomAttributeConfiguration
280
+ var nextValue = typeof attributeConfig.process === 'function' ? attributeConfig.process(nextProp) : nextProp;
281
+ updatePayload[altKey] = nextValue;
282
+ }
283
+ continue;
284
+ }
285
+
286
+ if (prevProp === nextProp) {
287
+ continue; // nothing changed
288
+ }
289
+
290
+ // Pattern match on: attributeConfig
291
+ if (typeof attributeConfig !== 'object') {
292
+ // case: !Object is the default case
293
+ if (defaultDiffer(prevProp, nextProp)) {
294
+ // a normal leaf has changed
295
+ (updatePayload || (updatePayload = {}))[altKey] = nextProp;
296
+ }
297
+ } else if (typeof attributeConfig.diff === 'function' || typeof attributeConfig.process === 'function') {
298
+ // case: CustomAttributeConfiguration
299
+ var shouldUpdate = prevProp === undefined || (typeof attributeConfig.diff === 'function' ? attributeConfig.diff(prevProp, nextProp) : defaultDiffer(prevProp, nextProp));
300
+ if (shouldUpdate) {
301
+ nextValue = typeof attributeConfig.process === 'function' ? attributeConfig.process(nextProp) : nextProp;
302
+ (updatePayload || (updatePayload = {}))[altKey] = nextValue;
303
+ }
304
+ } else {
305
+ // default: fallthrough case when nested properties are defined
306
+ removedKeys = null;
307
+ removedKeyCount = 0;
308
+ updatePayload = diffNestedProperty(updatePayload, prevProp, nextProp, attributeConfig);
309
+ if (removedKeyCount > 0 && updatePayload) {
310
+ restoreDeletedValuesInNestedArray(updatePayload, nextProp, attributeConfig);
311
+ removedKeys = null;
312
+ }
313
+ }
314
+ }
315
+
316
+ // Also iterate through all the previous props to catch any that have been
317
+ // removed and make sure native gets the signal so it can reset them to the
318
+ // default.
319
+ for (propKey in prevProps) {
320
+ if (nextProps[propKey] !== undefined) {
321
+ continue; // we've already covered this key in the previous pass
322
+ }
323
+ attributeConfig = validAttributes[propKey];
324
+ if (!attributeConfig) {
325
+ continue; // not a valid native prop
326
+ }
327
+
328
+ altKey = translateKey(propKey);
329
+ if (!attributeConfig[altKey]) {
330
+ // If there is no config for the alternative, bail out. Helps ART.
331
+ altKey = propKey;
332
+ }
333
+
334
+ if (updatePayload && updatePayload[altKey] !== undefined) {
335
+ // This was already updated to a diff result earlier.
336
+ continue;
337
+ }
338
+
339
+ prevProp = prevProps[propKey];
340
+ if (prevProp === undefined) {
341
+ continue; // was already empty anyway
342
+ }
343
+ // Pattern match on: attributeConfig
344
+ if (typeof attributeConfig !== 'object' || typeof attributeConfig.diff === 'function' || typeof attributeConfig.process === 'function') {
345
+
346
+ // case: CustomAttributeConfiguration | !Object
347
+ // Flag the leaf property for removal by sending a sentinel.
348
+ (updatePayload || (updatePayload = {}))[altKey] = null;
349
+ if (!removedKeys) {
350
+ removedKeys = {};
351
+ }
352
+ if (!removedKeys[propKey]) {
353
+ removedKeys[propKey] = true;
354
+ removedKeyCount++;
355
+ }
356
+ } else {
357
+ // default:
358
+ // This is a nested attribute configuration where all the properties
359
+ // were removed so we need to go through and clear out all of them.
360
+ updatePayload = clearNestedProperty(updatePayload, prevProp, attributeConfig);
361
+ }
362
+ }
363
+ return updatePayload;
364
+ }
365
+
366
+ /**
367
+ * addProperties adds all the valid props to the payload after being processed.
368
+ */
369
+ function addProperties(updatePayload, props, validAttributes) {
370
+ // TODO: Fast path
371
+ return diffProperties(updatePayload, emptyObject, props, validAttributes);
372
+ }
373
+
374
+ /**
375
+ * clearProperties clears all the previous props by adding a null sentinel
376
+ * to the payload for each valid key.
377
+ */
378
+ function clearProperties(updatePayload, prevProps, validAttributes) {
379
+ // TODO: Fast path
380
+ return diffProperties(updatePayload, prevProps, emptyObject, validAttributes);
381
+ }
382
+
383
+ var ReactNativeAttributePayload = {
384
+
385
+ create: function (props, validAttributes) {
386
+ return addProperties(null, // updatePayload
387
+ props, validAttributes);
388
+ },
389
+
390
+ diff: function (prevProps, nextProps, validAttributes) {
391
+ return diffProperties(null, // updatePayload
392
+ prevProps, nextProps, validAttributes);
393
+ }
394
+
395
+ };
396
+
397
+ module.exports = ReactNativeAttributePayload;