@@ -1044,6 +1044,34 @@ export function getExecutionContext(): ExecutionContext {
10441044 return executionContext ;
10451045}
10461046
1047+ export function flushDiscreteUpdates ( ) {
1048+ // TODO: Should be able to flush inside batchedUpdates, but not inside `act`.
1049+ // However, `act` uses `batchedUpdates`, so there's no way to distinguish
1050+ // those two cases. Need to fix this before exposing flushDiscreteUpdates
1051+ // as a public API.
1052+ if (
1053+ ( executionContext & ( BatchedContext | RenderContext | CommitContext ) ) !==
1054+ NoContext
1055+ ) {
1056+ if ( __DEV__ ) {
1057+ if ( ( executionContext & RenderContext ) !== NoContext ) {
1058+ console . error (
1059+ 'unstable_flushDiscreteUpdates: Cannot flush updates when React is ' +
1060+ 'already rendering.' ,
1061+ ) ;
1062+ }
1063+ }
1064+ // We're already rendering, so we can't synchronously flush pending work.
1065+ // This is probably a nested event dispatch triggered by a lifecycle/effect,
1066+ // like `el.focus()`. Exit.
1067+ return ;
1068+ }
1069+ flushSyncCallbacks ( ) ;
1070+ // If the discrete updates scheduled passive effects, flush them now so that
1071+ // they fire before the next serial event.
1072+ flushPassiveEffects ( ) ;
1073+ }
1074+
10471075export function deferredUpdates < A > ( fn : ( ) = > A ) : A {
10481076 const previousPriority = getCurrentUpdatePriority ( ) ;
10491077 const prevTransition = ReactCurrentBatchConfig . transition ;
@@ -1114,10 +1142,7 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {
11141142 }
11151143}
11161144
1117- export function flushSyncWithoutWarningIfAlreadyRendering < A , R > (
1118- fn : A => R ,
1119- a : A ,
1120- ) : R {
1145+ export function flushSync < A , R > ( fn : A => R , a : A ) : R {
11211146 const prevExecutionContext = executionContext ;
11221147 executionContext |= BatchedContext ;
11231148
@@ -1140,23 +1165,18 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
11401165 // the stack.
11411166 if ( ( executionContext & ( RenderContext | CommitContext ) ) === NoContext ) {
11421167 flushSyncCallbacks ( ) ;
1168+ } else {
1169+ if ( __DEV__ ) {
1170+ console . error (
1171+ 'flushSync was called from inside a lifecycle method. React cannot ' +
1172+ 'flush when React is already rendering. Consider moving this call to ' +
1173+ 'a scheduler task or micro task.' ,
1174+ ) ;
1175+ }
11431176 }
11441177 }
11451178}
11461179
1147- export function flushSync < A , R > ( fn : A => R , a : A ) : R {
1148- if ( __DEV__ ) {
1149- if ( ( executionContext & ( RenderContext | CommitContext ) ) !== NoContext ) {
1150- console . error (
1151- 'flushSync was called from inside a lifecycle method. React cannot ' +
1152- 'flush when React is already rendering. Consider moving this call to ' +
1153- 'a scheduler task or micro task.' ,
1154- ) ;
1155- }
1156- }
1157- return flushSyncWithoutWarningIfAlreadyRendering ( fn , a ) ;
1158- }
1159-
11601180export function flushControlled ( fn : ( ) = > mixed ) : void {
11611181 const prevExecutionContext = executionContext ;
11621182 executionContext |= BatchedContext ;
0 commit comments