@@ -17,15 +17,33 @@ import {
1717// scheduled work and instead do synchronous work.
1818
1919// Defaults
20- let _batchedUpdatesImpl = function ( fn , bookkeeping ) {
20+ let batchedUpdatesImpl = function ( fn , bookkeeping ) {
2121 return fn ( bookkeeping ) ;
2222} ;
23- let _interactiveUpdatesImpl = function ( fn , a , b , c ) {
23+ let discreteUpdatesImpl = function ( fn , a , b , c ) {
2424 return fn ( a , b , c ) ;
2525} ;
26- let _flushInteractiveUpdatesImpl = function ( ) { } ;
26+ let flushDiscreteUpdatesImpl = function ( ) { } ;
27+ let batchedEventUpdatesImpl = batchedUpdatesImpl ;
2728
2829let isBatching = false ;
30+
31+ function batchedUpdatesFinally ( ) {
32+ // Here we wait until all updates have propagated, which is important
33+ // when using controlled components within layers:
34+ // https://github.com/facebook/react/issues/1698
35+ // Then we restore state of any controlled component.
36+ isBatching = false ;
37+ const controlledComponentsHavePendingUpdates = needsStateRestore ( ) ;
38+ if ( controlledComponentsHavePendingUpdates ) {
39+ // If a controlled event was fired, we may need to restore the state of
40+ // the DOM node back to the controlled value. This is necessary when React
41+ // bails out of the update without touching the DOM.
42+ flushDiscreteUpdatesImpl ( ) ;
43+ restoreStateIfNeeded ( ) ;
44+ }
45+ }
46+
2947export function batchedUpdates ( fn , bookkeeping ) {
3048 if ( isBatching ) {
3149 // If we are currently inside another batch, we need to wait until it
@@ -34,38 +52,42 @@ export function batchedUpdates(fn, bookkeeping) {
3452 }
3553 isBatching = true ;
3654 try {
37- return _batchedUpdatesImpl ( fn , bookkeeping ) ;
55+ return batchedUpdatesImpl ( fn , bookkeeping ) ;
56+ } finally {
57+ batchedUpdatesFinally ( ) ;
58+ }
59+ }
60+
61+ export function batchedEventUpdates ( fn , bookkeeping ) {
62+ if ( isBatching ) {
63+ // If we are currently inside another batch, we need to wait until it
64+ // fully completes before restoring state.
65+ return fn ( bookkeeping ) ;
66+ }
67+ isBatching = true ;
68+ try {
69+ return batchedEventUpdatesImpl ( fn , bookkeeping ) ;
3870 } finally {
39- // Here we wait until all updates have propagated, which is important
40- // when using controlled components within layers:
41- // https://github.com/facebook/react/issues/1698
42- // Then we restore state of any controlled component.
43- isBatching = false ;
44- const controlledComponentsHavePendingUpdates = needsStateRestore ( ) ;
45- if ( controlledComponentsHavePendingUpdates ) {
46- // If a controlled event was fired, we may need to restore the state of
47- // the DOM node back to the controlled value. This is necessary when React
48- // bails out of the update without touching the DOM.
49- _flushInteractiveUpdatesImpl ( ) ;
50- restoreStateIfNeeded ( ) ;
51- }
71+ batchedUpdatesFinally ( ) ;
5272 }
5373}
5474
55- export function interactiveUpdates ( fn , a , b , c ) {
56- return _interactiveUpdatesImpl ( fn , a , b , c ) ;
75+ export function discreteUpdates ( fn , a , b , c ) {
76+ return discreteUpdatesImpl ( fn , a , b , c ) ;
5777}
5878
59- export function flushInteractiveUpdates ( ) {
60- return _flushInteractiveUpdatesImpl ( ) ;
79+ export function flushDiscreteUpdates ( ) {
80+ return flushDiscreteUpdatesImpl ( ) ;
6181}
6282
6383export function setBatchingImplementation (
64- batchedUpdatesImpl ,
65- interactiveUpdatesImpl ,
66- flushInteractiveUpdatesImpl ,
84+ _batchedUpdatesImpl ,
85+ _discreteUpdatesImpl ,
86+ _flushDiscreteUpdatesImpl ,
87+ _batchedEventUpdatesImpl ,
6788) {
68- _batchedUpdatesImpl = batchedUpdatesImpl ;
69- _interactiveUpdatesImpl = interactiveUpdatesImpl ;
70- _flushInteractiveUpdatesImpl = flushInteractiveUpdatesImpl ;
89+ batchedUpdatesImpl = _batchedUpdatesImpl ;
90+ discreteUpdatesImpl = _discreteUpdatesImpl ;
91+ flushDiscreteUpdatesImpl = _flushDiscreteUpdatesImpl ;
92+ batchedEventUpdatesImpl = _batchedEventUpdatesImpl ;
7193}
0 commit comments