From 2a6fe079c08231389b56370a027f851258578770 Mon Sep 17 00:00:00 2001 From: James Ide Date: Mon, 1 Jun 2015 15:58:22 -0700 Subject: [PATCH] [Timers] Batch setImmediate handlers Summary: Wraps the setImmediate handlers in a `batchUpdates` call before they are synchronously executed at the end of the JS execution loop. Closes https://github.com/facebook/react-native/pull/1242 Github Author: James Ide Test Plan: Added two `setImmediate` calls to `componentDidMount` in UIExplorerApp. Each handler calls `setState`, and `componentWillUpdate` logs its state. With this diff, we can see the state updates are successfully batched. ```javascript componentDidMount() { setImmediate(() => { console.log('immediate 1'); this.setState({a: 1}); }); setImmediate(() => { console.log('immediate 2'); this.setState({a: 2}); }); }, componentWillUpdate(nextProps, nextState) { console.log('componentWillUpdate with next state.a =', nextState.a); }, ``` **Before:** "immediate 1" "componentWillUpdate with next state.a =", 1 "immediate 2" "componentWillUpdate with next state.a =", 2 **After:** "immediate 1" "immediate 2" "componentWillUpdate with next state.a =", 2 Addresses the batching issue in #1232. cc @vjeux @spicyj --- Libraries/Utilities/MessageQueue.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Libraries/Utilities/MessageQueue.js b/Libraries/Utilities/MessageQueue.js index 5b1989c28..2ff8b81d7 100644 --- a/Libraries/Utilities/MessageQueue.js +++ b/Libraries/Utilities/MessageQueue.js @@ -472,8 +472,10 @@ var MessageQueueMixin = { }, _flushedQueueUnguarded: function() { - // Call the functions registred via setImmediate - JSTimersExecution.callImmediates(); + ReactUpdates.batchedUpdates(() => { + // Call the functions registered via setImmediate + JSTimersExecution.callImmediates(); + }); var currentOutgoingItems = this._outgoingItems; this._swapAndReinitializeBuffer();