@@ -29,6 +29,8 @@ var mixInto = require('mixInto');
2929var warning = require ( 'warning' ) ;
3030
3131var dirtyComponents = [ ] ;
32+ var setImmediateCallbackQueue = CallbackQueue . getPooled ( ) ;
33+ var setImmediateEnqueued = false ;
3234
3335var batchingStrategy = null ;
3436
@@ -73,7 +75,7 @@ var TRANSACTION_WRAPPERS = [NESTED_UPDATES, UPDATE_QUEUEING];
7375function ReactUpdatesFlushTransaction ( ) {
7476 this . reinitializeTransaction ( ) ;
7577 this . dirtyComponentsLength = null ;
76- this . callbackQueue = CallbackQueue . getPooled ( null ) ;
78+ this . callbackQueue = CallbackQueue . getPooled ( ) ;
7779 this . reconcileTransaction =
7880 ReactUpdates . ReactReconcileTransaction . getPooled ( ) ;
7981}
@@ -170,8 +172,8 @@ var flushBatchedUpdates = ReactPerf.measure(
170172 // ReactUpdatesFlushTransaction's wrappers will clear the dirtyComponents
171173 // array and perform any updates enqueued by mount-ready handlers (i.e.,
172174 // componentDidUpdate) but we need to check here too in order to catch
173- // updates enqueued by setState callbacks.
174- while ( dirtyComponents . length ) {
175+ // updates enqueued by setState callbacks and setImmediate calls .
176+ while ( dirtyComponents . length || setImmediateEnqueued ) {
175177 var allUnmounted = true ;
176178 for ( var i = 0 , l = dirtyComponents . length ; i < l ; i ++ ) {
177179 if ( dirtyComponents [ i ] . isMounted ( ) ) {
@@ -194,6 +196,10 @@ var flushBatchedUpdates = ReactPerf.measure(
194196 var transaction = ReactUpdatesFlushTransaction . getPooled ( ) ;
195197 transaction . perform ( runBatchedUpdates , null , transaction ) ;
196198 ReactUpdatesFlushTransaction . release ( transaction ) ;
199+
200+ setImmediateCallbackQueue . notifyAll ( ) ;
201+ setImmediateCallbackQueue . reset ( ) ;
202+ setImmediateEnqueued = false ;
197203 }
198204 }
199205) ;
@@ -240,6 +246,20 @@ function enqueueUpdate(component, callback) {
240246 }
241247}
242248
249+ /**
250+ * Enqueue a callback to be run at the end of the current batching cycle. Throws
251+ * if no updates are currently being performed.
252+ */
253+ function setImmediate ( callback , context ) {
254+ invariant (
255+ batchingStrategy . isBatchingUpdates ,
256+ 'ReactUpdates.setImmediate: Can\'t enqueue an immediate callback in a ' +
257+ 'context where updates are not being batched.'
258+ ) ;
259+ setImmediateCallbackQueue . enqueue ( callback , context ) ;
260+ setImmediateEnqueued = true ;
261+ }
262+
243263var ReactUpdatesInjection = {
244264 injectReconcileTransaction : function ( ReconcileTransaction ) {
245265 invariant (
@@ -278,7 +298,8 @@ var ReactUpdates = {
278298 batchedUpdates : batchedUpdates ,
279299 enqueueUpdate : enqueueUpdate ,
280300 flushBatchedUpdates : flushBatchedUpdates ,
281- injection : ReactUpdatesInjection
301+ injection : ReactUpdatesInjection ,
302+ setImmediate : setImmediate
282303} ;
283304
284305module . exports = ReactUpdates ;
0 commit comments