-
Notifications
You must be signed in to change notification settings - Fork 24.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Timers] Batch setImmediate handlers #1242
Conversation
I was looking into it before, but I thought on doing it on var flushedQueue;
React.batchedUpdates(() => {
flushedQueue = guardReturn(...);
});
return flushedQueue; |
I realized batchedUpdates needs to be inside of the guard. Update coming. |
Looking some more, the current diff appears correct to me. Both batchedUpdate calls (the one in It's easy to reduce the batching to one batch by moving |
It is unintuitive to me that setImmediate calls would get batched together into the same React reconciliation. I'll have to think about it some more. |
@spicyj totally understand. What if they were process.nextTick calls? The motivation behind this diff is to batch the following updates: var resolve1, resolve2;
new Promise((resolve) => resolve1 = resolve).then(() => {
this.setState({...});
});
new Promise((resolve) => resolve2 = resolve).then(() => {
this.setState({...});
});
// Resolve both promises in the same run of the event loop
resolve1();
resolve2(); Promises internally schedule a microtask (in RN this currently means setImmediate) but we want to batch the updates. One catch is that if we use native Promises then they won't call setImmediate nor nextTick so maybe this diff isn't the best approach anyway. Feel free to take or reject it. |
@ide we can't use native promises as they don't respect React Native scheduling with the batch, so I don't think that's a big issue anyway |
Actually, assigning to @spicyj since he has some thoughts on the matter. |
I think this is all right. |
@facebook-github-bot import plz |
@facebook-github-bot import |
strange that it didn't work for you @spicyj |
Thanks for importing. If you are an FB employee go to https://our.intern.facebook.com/intern/opensource/github/pull_request/439269476233905/int_phab to review. |
This got reverted internally. Will try to re-land. |
@ide Anything change here? Still had this on my (long) list of things to do. |
@spicyj this issue should be open. I must have closed it when rebasing since it was merged once before being reverted. Reapplied the commit on top of master and retested. |
Wraps the setImmediate handlers in a `batchedUpdates` call before they are synchronously executed at the end of the JS execution loop. 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
510c69d
to
6e30141
Compare
Closing this because setImmediates were batched in 4c74f01. |
…acebook#1242) * imp: snack example for components * revert pretter stuff
* Manual version bump to 0.68.3 * Use react-native-macos instead of react-native in template version bump
Wraps the setImmediate handlers in a
batchUpdates
call before they are synchronously executed at the end of the JS execution loop.Test Plan: Added two
setImmediate
calls tocomponentDidMount
in UIExplorerApp. Each handler callssetState
, andcomponentWillUpdate
logs its state. With this diff, we can see the state updates are successfully batched.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