From c92143c84e2139529201d05757a57fa4f2b18fc5 Mon Sep 17 00:00:00 2001 From: Rick Hanlon Date: Thu, 15 Jul 2021 12:36:31 -0400 Subject: [PATCH] Clean up flushSync flow types --- .../src/__tests__/ReactDOMFiberAsync-test.js | 2 +- .../src/createReactNoop.js | 5 +--- .../src/ReactFiberWorkLoop.new.js | 25 +++++++++++++------ .../src/ReactFiberWorkLoop.old.js | 25 +++++++++++++------ .../src/ReactTestRenderer.js | 4 +-- 5 files changed, 37 insertions(+), 24 deletions(-) diff --git a/packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js b/packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js index ef2873a80bd7f..79a7351f57f37 100644 --- a/packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js +++ b/packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js @@ -132,7 +132,7 @@ describe('ReactDOMFiberAsync', () => { it('flushSync logs an error if already performing work', () => { class Component extends React.Component { componentDidUpdate() { - ReactDOM.flushSync(() => {}); + ReactDOM.flushSync(); } render() { return null; diff --git a/packages/react-noop-renderer/src/createReactNoop.js b/packages/react-noop-renderer/src/createReactNoop.js index 5ea510d73d6ea..652f32521c249 100644 --- a/packages/react-noop-renderer/src/createReactNoop.js +++ b/packages/react-noop-renderer/src/createReactNoop.js @@ -913,10 +913,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) { } }, - flushSync(fn: () => mixed) { - NoopRenderer.flushSync(fn); - }, - + flushSync: NoopRenderer.flushSync, flushPassiveEffects: NoopRenderer.flushPassiveEffects, // Logs the current state of the tree. diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js index cede27655a52a..065d17fcc7533 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.new.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.new.js @@ -1093,10 +1093,13 @@ export function discreteUpdates( } } -export function flushSyncWithoutWarningIfAlreadyRendering( - fn: A => R, - a: A, -): R { +// Overload the definition to the two valid signatures. +// Warning, this opts-out of checking the function body. +declare function flushSyncWithoutWarningIfAlreadyRendering(fn: () => R): R; +// eslint-disable-next-line no-redeclare +declare function flushSyncWithoutWarningIfAlreadyRendering(): void; +// eslint-disable-next-line no-redeclare +export function flushSyncWithoutWarningIfAlreadyRendering(fn) { // In legacy mode, we flush pending passive effects at the beginning of the // next event, not at the end of the previous one. if ( @@ -1116,9 +1119,9 @@ export function flushSyncWithoutWarningIfAlreadyRendering( ReactCurrentBatchConfig.transition = 0; setCurrentUpdatePriority(DiscreteEventPriority); if (fn) { - return fn(a); + return fn(); } else { - return (undefined: $FlowFixMe); + return undefined; } } finally { setCurrentUpdatePriority(previousPriority); @@ -1133,7 +1136,13 @@ export function flushSyncWithoutWarningIfAlreadyRendering( } } -export function flushSync(fn: A => R, a: A): R { +// Overload the definition to the two valid signatures. +// Warning, this opts-out of checking the function body. +declare function flushSync(fn: () => R): R; +// eslint-disable-next-line no-redeclare +declare function flushSync(): void; +// eslint-disable-next-line no-redeclare +export function flushSync(fn) { if (__DEV__) { if ((executionContext & (RenderContext | CommitContext)) !== NoContext) { console.error( @@ -1143,7 +1152,7 @@ export function flushSync(fn: A => R, a: A): R { ); } } - return flushSyncWithoutWarningIfAlreadyRendering(fn, a); + return flushSyncWithoutWarningIfAlreadyRendering(fn); } export function flushControlled(fn: () => mixed): void { diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js index 0bba7217b1fdb..710e853ce4fd7 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.old.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.old.js @@ -1093,10 +1093,13 @@ export function discreteUpdates( } } -export function flushSyncWithoutWarningIfAlreadyRendering( - fn: A => R, - a: A, -): R { +// Overload the definition to the two valid signatures. +// Warning, this opts-out of checking the function body. +declare function flushSyncWithoutWarningIfAlreadyRendering(fn: () => R): R; +// eslint-disable-next-line no-redeclare +declare function flushSyncWithoutWarningIfAlreadyRendering(): void; +// eslint-disable-next-line no-redeclare +export function flushSyncWithoutWarningIfAlreadyRendering(fn) { // In legacy mode, we flush pending passive effects at the beginning of the // next event, not at the end of the previous one. if ( @@ -1116,9 +1119,9 @@ export function flushSyncWithoutWarningIfAlreadyRendering( ReactCurrentBatchConfig.transition = 0; setCurrentUpdatePriority(DiscreteEventPriority); if (fn) { - return fn(a); + return fn(); } else { - return (undefined: $FlowFixMe); + return undefined; } } finally { setCurrentUpdatePriority(previousPriority); @@ -1133,7 +1136,13 @@ export function flushSyncWithoutWarningIfAlreadyRendering( } } -export function flushSync(fn: A => R, a: A): R { +// Overload the definition to the two valid signatures. +// Warning, this opts-out of checking the function body. +declare function flushSync(fn: () => R): R; +// eslint-disable-next-line no-redeclare +declare function flushSync(): void; +// eslint-disable-next-line no-redeclare +export function flushSync(fn) { if (__DEV__) { if ((executionContext & (RenderContext | CommitContext)) !== NoContext) { console.error( @@ -1143,7 +1152,7 @@ export function flushSync(fn: A => R, a: A): R { ); } } - return flushSyncWithoutWarningIfAlreadyRendering(fn, a); + return flushSyncWithoutWarningIfAlreadyRendering(fn); } export function flushControlled(fn: () => mixed): void { diff --git a/packages/react-test-renderer/src/ReactTestRenderer.js b/packages/react-test-renderer/src/ReactTestRenderer.js index 60e02766dc9dd..39c288014884d 100644 --- a/packages/react-test-renderer/src/ReactTestRenderer.js +++ b/packages/react-test-renderer/src/ReactTestRenderer.js @@ -536,9 +536,7 @@ function create(element: React$Element, options: TestRendererOptions) { return getPublicRootInstance(root); }, - unstable_flushSync(fn: () => T): T { - return flushSync(fn); - }, + unstable_flushSync: flushSync, }; Object.defineProperty(