unstable_batchedUpdates

Implements batchedUpdates and exposes as unstable_batchedUpdates. All
nested synchronous updates are automatically batched.
This commit is contained in:
Andrew Clark
2016-10-30 22:26:57 -07:00
parent d153b1e27b
commit b41883fc70
3 changed files with 28 additions and 7 deletions
+4
View File
@@ -165,6 +165,10 @@ var ReactDOM = {
return DOMRenderer.findHostInstance(component);
},
unstable_batchedUpdates(fn : Function) : void {
DOMRenderer.batchedUpdates(fn);
},
};
module.exports = ReactDOM;
@@ -68,6 +68,7 @@ export type Reconciler<C, I, TI> = {
updateContainer(element : ReactElement<any>, container : OpaqueNode) : void,
unmountContainer(container : OpaqueNode) : void,
performWithPriority(priorityLevel : PriorityLevel, fn : Function) : void,
batchedUpdates(fn: Function) : void,
// Used to extract the return value from the initial render. Legacy API.
getPublicRootInstance(container : OpaqueNode) : (ReactComponent<any, any, any> | TI | I | null),
@@ -78,7 +79,11 @@ export type Reconciler<C, I, TI> = {
module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) : Reconciler<C, I, TI> {
var { scheduleWork, performWithPriority } = ReactFiberScheduler(config);
var {
scheduleWork,
performWithPriority,
batchedUpdates,
} = ReactFiberScheduler(config);
return {
@@ -142,6 +147,8 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) :
performWithPriority,
batchedUpdates,
getPublicRootInstance(container : OpaqueNode) : (ReactComponent<any, any, any> | I | TI | null) {
const root : FiberRoot = (container.stateNode : any);
const containerFiber = root.current;
@@ -508,12 +508,10 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
}
function performSynchronousWork() {
if (useSyncScheduling) {
// Start batching updates
shouldBatchUpdates = true;
}
performAndHandleErrors(performSynchronousWorkUnsafe);
shouldBatchUpdates = false;
// All nested updates are batched
batchedUpdates(() => {
performAndHandleErrors(performSynchronousWorkUnsafe);
});
}
function scheduleSynchronousWork(root : FiberRoot) {
@@ -534,6 +532,7 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
lastScheduledRoot = root;
if (!shouldBatchUpdates) {
// Unless in batched mode, perform work immediately
performSynchronousWork();
}
}
@@ -690,9 +689,20 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
}
}
function batchedUpdates(fn: Function) {
const prev = shouldBatchUpdates;
shouldBatchUpdates = true;
try {
fn();
} finally {
shouldBatchUpdates = prev;
}
}
return {
scheduleWork: scheduleWork,
scheduleDeferredWork: scheduleDeferredWork,
performWithPriority: performWithPriority,
batchedUpdates: batchedUpdates,
};
};