1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var _prodInvariant = require('./reactProdInvariant'),
|
12 | _assign = require('object-assign');
|
13 |
|
14 | var CallbackQueue = require('./CallbackQueue');
|
15 | var PooledClass = require('./PooledClass');
|
16 | var ReactFeatureFlags = require('./ReactFeatureFlags');
|
17 | var ReactReconciler = require('./ReactReconciler');
|
18 | var Transaction = require('./Transaction');
|
19 |
|
20 | var invariant = require('fbjs/lib/invariant');
|
21 |
|
22 | var dirtyComponents = [];
|
23 | var updateBatchNumber = 0;
|
24 | var asapCallbackQueue = CallbackQueue.getPooled();
|
25 | var asapEnqueued = false;
|
26 |
|
27 | var batchingStrategy = null;
|
28 |
|
29 | function ensureInjected() {
|
30 | !(ReactUpdates.ReactReconcileTransaction && batchingStrategy) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactUpdates: must inject a reconcile transaction class and batching strategy') : _prodInvariant('123') : void 0;
|
31 | }
|
32 |
|
33 | var NESTED_UPDATES = {
|
34 | initialize: function () {
|
35 | this.dirtyComponentsLength = dirtyComponents.length;
|
36 | },
|
37 | close: function () {
|
38 | if (this.dirtyComponentsLength !== dirtyComponents.length) {
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | dirtyComponents.splice(0, this.dirtyComponentsLength);
|
45 | flushBatchedUpdates();
|
46 | } else {
|
47 | dirtyComponents.length = 0;
|
48 | }
|
49 | }
|
50 | };
|
51 |
|
52 | var UPDATE_QUEUEING = {
|
53 | initialize: function () {
|
54 | this.callbackQueue.reset();
|
55 | },
|
56 | close: function () {
|
57 | this.callbackQueue.notifyAll();
|
58 | }
|
59 | };
|
60 |
|
61 | var TRANSACTION_WRAPPERS = [NESTED_UPDATES, UPDATE_QUEUEING];
|
62 |
|
63 | function ReactUpdatesFlushTransaction() {
|
64 | this.reinitializeTransaction();
|
65 | this.dirtyComponentsLength = null;
|
66 | this.callbackQueue = CallbackQueue.getPooled();
|
67 | this.reconcileTransaction = ReactUpdates.ReactReconcileTransaction.getPooled(
|
68 | true);
|
69 | }
|
70 |
|
71 | _assign(ReactUpdatesFlushTransaction.prototype, Transaction, {
|
72 | getTransactionWrappers: function () {
|
73 | return TRANSACTION_WRAPPERS;
|
74 | },
|
75 |
|
76 | destructor: function () {
|
77 | this.dirtyComponentsLength = null;
|
78 | CallbackQueue.release(this.callbackQueue);
|
79 | this.callbackQueue = null;
|
80 | ReactUpdates.ReactReconcileTransaction.release(this.reconcileTransaction);
|
81 | this.reconcileTransaction = null;
|
82 | },
|
83 |
|
84 | perform: function (method, scope, a) {
|
85 |
|
86 |
|
87 | return Transaction.perform.call(this, this.reconcileTransaction.perform, this.reconcileTransaction, method, scope, a);
|
88 | }
|
89 | });
|
90 |
|
91 | PooledClass.addPoolingTo(ReactUpdatesFlushTransaction);
|
92 |
|
93 | function batchedUpdates(callback, a, b, c, d, e) {
|
94 | ensureInjected();
|
95 | return batchingStrategy.batchedUpdates(callback, a, b, c, d, e);
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | function mountOrderComparator(c1, c2) {
|
106 | return c1._mountOrder - c2._mountOrder;
|
107 | }
|
108 |
|
109 | function runBatchedUpdates(transaction) {
|
110 | var len = transaction.dirtyComponentsLength;
|
111 | !(len === dirtyComponents.length) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Expected flush transaction\'s stored dirty-components length (%s) to match dirty-components array length (%s).', len, dirtyComponents.length) : _prodInvariant('124', len, dirtyComponents.length) : void 0;
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | dirtyComponents.sort(mountOrderComparator);
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | updateBatchNumber++;
|
124 |
|
125 | for (var i = 0; i < len; i++) {
|
126 |
|
127 |
|
128 |
|
129 | var component = dirtyComponents[i];
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | var callbacks = component._pendingCallbacks;
|
135 | component._pendingCallbacks = null;
|
136 |
|
137 | var markerName;
|
138 | if (ReactFeatureFlags.logTopLevelRenders) {
|
139 | var namedComponent = component;
|
140 |
|
141 | if (component._currentElement.type.isReactTopLevelWrapper) {
|
142 | namedComponent = component._renderedComponent;
|
143 | }
|
144 | markerName = 'React update: ' + namedComponent.getName();
|
145 | console.time(markerName);
|
146 | }
|
147 |
|
148 | ReactReconciler.performUpdateIfNecessary(component, transaction.reconcileTransaction, updateBatchNumber);
|
149 |
|
150 | if (markerName) {
|
151 | console.timeEnd(markerName);
|
152 | }
|
153 |
|
154 | if (callbacks) {
|
155 | for (var j = 0; j < callbacks.length; j++) {
|
156 | transaction.callbackQueue.enqueue(callbacks[j], component.getPublicInstance());
|
157 | }
|
158 | }
|
159 | }
|
160 | }
|
161 |
|
162 | var flushBatchedUpdates = function () {
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | while (dirtyComponents.length || asapEnqueued) {
|
168 | if (dirtyComponents.length) {
|
169 | var transaction = ReactUpdatesFlushTransaction.getPooled();
|
170 | transaction.perform(runBatchedUpdates, null, transaction);
|
171 | ReactUpdatesFlushTransaction.release(transaction);
|
172 | }
|
173 |
|
174 | if (asapEnqueued) {
|
175 | asapEnqueued = false;
|
176 | var queue = asapCallbackQueue;
|
177 | asapCallbackQueue = CallbackQueue.getPooled();
|
178 | queue.notifyAll();
|
179 | CallbackQueue.release(queue);
|
180 | }
|
181 | }
|
182 | };
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | function enqueueUpdate(component) {
|
189 | ensureInjected();
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | if (!batchingStrategy.isBatchingUpdates) {
|
198 | batchingStrategy.batchedUpdates(enqueueUpdate, component);
|
199 | return;
|
200 | }
|
201 |
|
202 | dirtyComponents.push(component);
|
203 | if (component._updateBatchNumber == null) {
|
204 | component._updateBatchNumber = updateBatchNumber + 1;
|
205 | }
|
206 | }
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | function asap(callback, context) {
|
213 | invariant(batchingStrategy.isBatchingUpdates, "ReactUpdates.asap: Can't enqueue an asap callback in a context where" + 'updates are not being batched.');
|
214 | asapCallbackQueue.enqueue(callback, context);
|
215 | asapEnqueued = true;
|
216 | }
|
217 |
|
218 | var ReactUpdatesInjection = {
|
219 | injectReconcileTransaction: function (ReconcileTransaction) {
|
220 | !ReconcileTransaction ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactUpdates: must provide a reconcile transaction class') : _prodInvariant('126') : void 0;
|
221 | ReactUpdates.ReactReconcileTransaction = ReconcileTransaction;
|
222 | },
|
223 |
|
224 | injectBatchingStrategy: function (_batchingStrategy) {
|
225 | !_batchingStrategy ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactUpdates: must provide a batching strategy') : _prodInvariant('127') : void 0;
|
226 | !(typeof _batchingStrategy.batchedUpdates === 'function') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactUpdates: must provide a batchedUpdates() function') : _prodInvariant('128') : void 0;
|
227 | !(typeof _batchingStrategy.isBatchingUpdates === 'boolean') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'ReactUpdates: must provide an isBatchingUpdates boolean attribute') : _prodInvariant('129') : void 0;
|
228 | batchingStrategy = _batchingStrategy;
|
229 | }
|
230 | };
|
231 |
|
232 | var ReactUpdates = {
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 | ReactReconcileTransaction: null,
|
240 |
|
241 | batchedUpdates: batchedUpdates,
|
242 | enqueueUpdate: enqueueUpdate,
|
243 | flushBatchedUpdates: flushBatchedUpdates,
|
244 | injection: ReactUpdatesInjection,
|
245 | asap: asap
|
246 | };
|
247 |
|
248 | module.exports = ReactUpdates; |
\ | No newline at end of file |