Skip to content

Commit b41883f

Browse files
committed
unstable_batchedUpdates
Implements batchedUpdates and exposes as unstable_batchedUpdates. All nested synchronous updates are automatically batched.
1 parent d153b1e commit b41883f

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/renderers/dom/fiber/ReactDOMFiber.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ var ReactDOM = {
165165
return DOMRenderer.findHostInstance(component);
166166
},
167167

168+
unstable_batchedUpdates(fn : Function) : void {
169+
DOMRenderer.batchedUpdates(fn);
170+
},
171+
168172
};
169173

170174
module.exports = ReactDOM;

src/renderers/shared/fiber/ReactFiberReconciler.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export type Reconciler<C, I, TI> = {
6868
updateContainer(element : ReactElement<any>, container : OpaqueNode) : void,
6969
unmountContainer(container : OpaqueNode) : void,
7070
performWithPriority(priorityLevel : PriorityLevel, fn : Function) : void,
71+
batchedUpdates(fn: Function) : void,
7172

7273
// Used to extract the return value from the initial render. Legacy API.
7374
getPublicRootInstance(container : OpaqueNode) : (ReactComponent<any, any, any> | TI | I | null),
@@ -78,7 +79,11 @@ export type Reconciler<C, I, TI> = {
7879

7980
module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) : Reconciler<C, I, TI> {
8081

81-
var { scheduleWork, performWithPriority } = ReactFiberScheduler(config);
82+
var {
83+
scheduleWork,
84+
performWithPriority,
85+
batchedUpdates,
86+
} = ReactFiberScheduler(config);
8287

8388
return {
8489

@@ -142,6 +147,8 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) :
142147

143148
performWithPriority,
144149

150+
batchedUpdates,
151+
145152
getPublicRootInstance(container : OpaqueNode) : (ReactComponent<any, any, any> | I | TI | null) {
146153
const root : FiberRoot = (container.stateNode : any);
147154
const containerFiber = root.current;

src/renderers/shared/fiber/ReactFiberScheduler.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,10 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
508508
}
509509

510510
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+
});
517515
}
518516

519517
function scheduleSynchronousWork(root : FiberRoot) {
@@ -534,6 +532,7 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
534532
lastScheduledRoot = root;
535533

536534
if (!shouldBatchUpdates) {
535+
// Unless in batched mode, perform work immediately
537536
performSynchronousWork();
538537
}
539538
}
@@ -690,9 +689,20 @@ module.exports = function<T, P, I, TI, C>(config : HostConfig<T, P, I, TI, C>) {
690689
}
691690
}
692691

692+
function batchedUpdates(fn: Function) {
693+
const prev = shouldBatchUpdates;
694+
shouldBatchUpdates = true;
695+
try {
696+
fn();
697+
} finally {
698+
shouldBatchUpdates = prev;
699+
}
700+
}
701+
693702
return {
694703
scheduleWork: scheduleWork,
695704
scheduleDeferredWork: scheduleDeferredWork,
696705
performWithPriority: performWithPriority,
706+
batchedUpdates: batchedUpdates,
697707
};
698708
};

0 commit comments

Comments
 (0)