unstable_batchedUpdates
Implements batchedUpdates and exposes as unstable_batchedUpdates. All nested synchronous updates are automatically batched.
Andrew Clark committed
Oct 30, 2016 at 22:26 UTC
b41883fc708cd24d77dcaa767cde814b50b457fe
3 files changed
+28
-7
src/renderers/dom/fiber/ReactDOMFiber.js
+4
@@ -165,6 +165,10 @@ var ReactDOM = {
165
return DOMRenderer.findHostInstance(component);
166
},
167
168
+ unstable_batchedUpdates(fn : Function) : void {
169
+ DOMRenderer.batchedUpdates(fn);
170
+ },
171
+
172
};
173
174
module.exports = ReactDOM;
src/renderers/shared/fiber/ReactFiberReconciler.js
+8
-1
@@ -68,6 +68,7 @@ export type Reconciler<C, I, TI> = {
68
updateContainer(element : ReactElement<any>, container : OpaqueNode) : void,
69
unmountContainer(container : OpaqueNode) : void,
70
performWithPriority(priorityLevel : PriorityLevel, fn : Function) : void,
71
+ batchedUpdates(fn: Function) : void,
72
73
// Used to extract the return value from the initial render. Legacy API.
74
getPublicRootInstance(container : OpaqueNode) : (ReactComponent<any, any, any> | TI | I | null),
@@ -78,7 +79,11 @@ export type Reconciler<C, I, TI> = {
79
80
module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) : Reconciler<C, I, TI> {
81
81
- var { scheduleWork, performWithPriority } = ReactFiberScheduler(config);
82
+ var {
83
+ scheduleWork,
84
+ performWithPriority,
85
+ batchedUpdates,
86
+ } = ReactFiberScheduler(config);
87
88
return {
89
@@ -142,6 +147,8 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) :
147
148
performWithPriority,
149
150
+ batchedUpdates,
151
+
152
getPublicRootInstance(container : OpaqueNode) : (ReactComponent<any, any, any> | I | TI | null) {
153
const root : FiberRoot = (container.stateNode : any);
154
const containerFiber = root.current;
src/renderers/shared/fiber/ReactFiberScheduler.js
+16
-6
@@ -508,12 +508,10 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
508
}
509
510
function performSynchronousWork() {
511
- if (useSyncScheduling) {
512
- // Start batching updates
513
- shouldBatchUpdates = true;
514
- }
515
- performAndHandleErrors(performSynchronousWorkUnsafe);
516
- shouldBatchUpdates = false;
511
+ // All nested updates are batched
512
+ batchedUpdates(() => {
513
+ performAndHandleErrors(performSynchronousWorkUnsafe);
514
+ });
515
}
516
517
function scheduleSynchronousWork(root : FiberRoot) {
@@ -534,6 +532,7 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
532
lastScheduledRoot = root;
533
534
if (!shouldBatchUpdates) {
535
+ // Unless in batched mode, perform work immediately
536
performSynchronousWork();
537
}
538
}
@@ -690,9 +689,20 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
689
}
690
}
691
692
+ function batchedUpdates(fn: Function) {
693
+ const prev = shouldBatchUpdates;
694
+ shouldBatchUpdates = true;
695
+ try {
696
+ fn();
697
+ } finally {
698
+ shouldBatchUpdates = prev;
699
+ }
700
+ }
701
+
702
return {
703
scheduleWork: scheduleWork,
704
scheduleDeferredWork: scheduleDeferredWork,
705
performWithPriority: performWithPriority,
706
+ batchedUpdates: batchedUpdates,
707
};
708
};