[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 <ide@jameside.com>

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
This commit is contained in:
James Ide 2015-06-01 15:58:22 -07:00
parent 38f57ee18c
commit 2a6fe079c0
1 changed files with 4 additions and 2 deletions

View File

@ -472,8 +472,10 @@ var MessageQueueMixin = {
},
_flushedQueueUnguarded: function() {
// Call the functions registred via setImmediate
ReactUpdates.batchedUpdates(() => {
// Call the functions registered via setImmediate
JSTimersExecution.callImmediates();
});
var currentOutgoingItems = this._outgoingItems;
this._swapAndReinitializeBuffer();