1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var ReactReconciler = require('./ReactReconciler');
|
12 |
|
13 | var instantiateReactComponent = require('./instantiateReactComponent');
|
14 | var KeyEscapeUtils = require('./KeyEscapeUtils');
|
15 | var shouldUpdateReactComponent = require('./shouldUpdateReactComponent');
|
16 | var traverseAllChildren = require('./traverseAllChildren');
|
17 | var warning = require('fbjs/lib/warning');
|
18 |
|
19 | var ReactComponentTreeHook;
|
20 |
|
21 | if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'test') {
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
|
28 | }
|
29 |
|
30 | function instantiateChild(childInstances, child, name, selfDebugID) {
|
31 |
|
32 | var keyUnique = childInstances[name] === undefined;
|
33 | if (process.env.NODE_ENV !== 'production') {
|
34 | if (!ReactComponentTreeHook) {
|
35 | ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
|
36 | }
|
37 | if (!keyUnique) {
|
38 | process.env.NODE_ENV !== 'production' ? warning(false, 'flattenChildren(...): Encountered two children with the same key, ' + '`%s`. Child keys must be unique; when two children share a key, only ' + 'the first child will be used.%s', KeyEscapeUtils.unescape(name), ReactComponentTreeHook.getStackAddendumByID(selfDebugID)) : void 0;
|
39 | }
|
40 | }
|
41 | if (child != null && keyUnique) {
|
42 | childInstances[name] = instantiateReactComponent(child, true);
|
43 | }
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | var ReactChildReconciler = {
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | instantiateChildren: function (nestedChildNodes, transaction, context, selfDebugID) // 0 in production and for roots
|
61 | {
|
62 | if (nestedChildNodes == null) {
|
63 | return null;
|
64 | }
|
65 | var childInstances = {};
|
66 |
|
67 | if (process.env.NODE_ENV !== 'production') {
|
68 | traverseAllChildren(nestedChildNodes, function (childInsts, child, name) {
|
69 | return instantiateChild(childInsts, child, name, selfDebugID);
|
70 | }, childInstances);
|
71 | } else {
|
72 | traverseAllChildren(nestedChildNodes, instantiateChild, childInstances);
|
73 | }
|
74 | return childInstances;
|
75 | },
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | updateChildren: function (prevChildren, nextChildren, mountImages, removedNodes, transaction, hostParent, hostContainerInfo, context, selfDebugID) // 0 in production and for roots
|
88 | {
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | if (!nextChildren && !prevChildren) {
|
95 | return;
|
96 | }
|
97 | var name;
|
98 | var prevChild;
|
99 | for (name in nextChildren) {
|
100 | if (!nextChildren.hasOwnProperty(name)) {
|
101 | continue;
|
102 | }
|
103 | prevChild = prevChildren && prevChildren[name];
|
104 | var prevElement = prevChild && prevChild._currentElement;
|
105 | var nextElement = nextChildren[name];
|
106 | if (prevChild != null && shouldUpdateReactComponent(prevElement, nextElement)) {
|
107 | ReactReconciler.receiveComponent(prevChild, nextElement, transaction, context);
|
108 | nextChildren[name] = prevChild;
|
109 | } else {
|
110 | if (prevChild) {
|
111 | removedNodes[name] = ReactReconciler.getHostNode(prevChild);
|
112 | ReactReconciler.unmountComponent(prevChild, false);
|
113 | }
|
114 |
|
115 | var nextChildInstance = instantiateReactComponent(nextElement, true);
|
116 | nextChildren[name] = nextChildInstance;
|
117 |
|
118 |
|
119 | var nextChildMountImage = ReactReconciler.mountComponent(nextChildInstance, transaction, hostParent, hostContainerInfo, context, selfDebugID);
|
120 | mountImages.push(nextChildMountImage);
|
121 | }
|
122 | }
|
123 |
|
124 | for (name in prevChildren) {
|
125 | if (prevChildren.hasOwnProperty(name) && !(nextChildren && nextChildren.hasOwnProperty(name))) {
|
126 | prevChild = prevChildren[name];
|
127 | removedNodes[name] = ReactReconciler.getHostNode(prevChild);
|
128 | ReactReconciler.unmountComponent(prevChild, false);
|
129 | }
|
130 | }
|
131 | },
|
132 |
|
133 | |
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | unmountChildren: function (renderedChildren, safely) {
|
141 | for (var name in renderedChildren) {
|
142 | if (renderedChildren.hasOwnProperty(name)) {
|
143 | var renderedChild = renderedChildren[name];
|
144 | ReactReconciler.unmountComponent(renderedChild, safely);
|
145 | }
|
146 | }
|
147 | }
|
148 | };
|
149 |
|
150 | module.exports = ReactChildReconciler; |
\ | No newline at end of file |