Skip to content

Commit 3101872

Browse files
authored
Clean up flushSync flow types (#21887)
1 parent d0ec283 commit 3101872

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('ReactDOMFiberAsync', () => {
132132
it('flushSync logs an error if already performing work', () => {
133133
class Component extends React.Component {
134134
componentDidUpdate() {
135-
ReactDOM.flushSync(() => {});
135+
ReactDOM.flushSync();
136136
}
137137
render() {
138138
return null;

packages/react-noop-renderer/src/createReactNoop.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
913913
}
914914
},
915915

916-
flushSync(fn: () => mixed) {
917-
NoopRenderer.flushSync(fn);
918-
},
919-
916+
flushSync: NoopRenderer.flushSync,
920917
flushPassiveEffects: NoopRenderer.flushPassiveEffects,
921918

922919
// Logs the current state of the tree.

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -1093,10 +1093,13 @@ export function discreteUpdates<A, B, C, D, R>(
10931093
}
10941094
}
10951095

1096-
export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
1097-
fn: A => R,
1098-
a: A,
1099-
): R {
1096+
// Overload the definition to the two valid signatures.
1097+
// Warning, this opts-out of checking the function body.
1098+
declare function flushSyncWithoutWarningIfAlreadyRendering<R>(fn: () => R): R;
1099+
// eslint-disable-next-line no-redeclare
1100+
declare function flushSyncWithoutWarningIfAlreadyRendering(): void;
1101+
// eslint-disable-next-line no-redeclare
1102+
export function flushSyncWithoutWarningIfAlreadyRendering(fn) {
11001103
// In legacy mode, we flush pending passive effects at the beginning of the
11011104
// next event, not at the end of the previous one.
11021105
if (
@@ -1116,9 +1119,9 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
11161119
ReactCurrentBatchConfig.transition = 0;
11171120
setCurrentUpdatePriority(DiscreteEventPriority);
11181121
if (fn) {
1119-
return fn(a);
1122+
return fn();
11201123
} else {
1121-
return (undefined: $FlowFixMe);
1124+
return undefined;
11221125
}
11231126
} finally {
11241127
setCurrentUpdatePriority(previousPriority);
@@ -1133,7 +1136,13 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
11331136
}
11341137
}
11351138

1136-
export function flushSync<A, R>(fn: A => R, a: A): R {
1139+
// Overload the definition to the two valid signatures.
1140+
// Warning, this opts-out of checking the function body.
1141+
declare function flushSync<R>(fn: () => R): R;
1142+
// eslint-disable-next-line no-redeclare
1143+
declare function flushSync(): void;
1144+
// eslint-disable-next-line no-redeclare
1145+
export function flushSync(fn) {
11371146
if (__DEV__) {
11381147
if ((executionContext & (RenderContext | CommitContext)) !== NoContext) {
11391148
console.error(
@@ -1143,7 +1152,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
11431152
);
11441153
}
11451154
}
1146-
return flushSyncWithoutWarningIfAlreadyRendering(fn, a);
1155+
return flushSyncWithoutWarningIfAlreadyRendering(fn);
11471156
}
11481157

11491158
export function flushControlled(fn: () => mixed): void {

packages/react-reconciler/src/ReactFiberWorkLoop.old.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -1093,10 +1093,13 @@ export function discreteUpdates<A, B, C, D, R>(
10931093
}
10941094
}
10951095

1096-
export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
1097-
fn: A => R,
1098-
a: A,
1099-
): R {
1096+
// Overload the definition to the two valid signatures.
1097+
// Warning, this opts-out of checking the function body.
1098+
declare function flushSyncWithoutWarningIfAlreadyRendering<R>(fn: () => R): R;
1099+
// eslint-disable-next-line no-redeclare
1100+
declare function flushSyncWithoutWarningIfAlreadyRendering(): void;
1101+
// eslint-disable-next-line no-redeclare
1102+
export function flushSyncWithoutWarningIfAlreadyRendering(fn) {
11001103
// In legacy mode, we flush pending passive effects at the beginning of the
11011104
// next event, not at the end of the previous one.
11021105
if (
@@ -1116,9 +1119,9 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
11161119
ReactCurrentBatchConfig.transition = 0;
11171120
setCurrentUpdatePriority(DiscreteEventPriority);
11181121
if (fn) {
1119-
return fn(a);
1122+
return fn();
11201123
} else {
1121-
return (undefined: $FlowFixMe);
1124+
return undefined;
11221125
}
11231126
} finally {
11241127
setCurrentUpdatePriority(previousPriority);
@@ -1133,7 +1136,13 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
11331136
}
11341137
}
11351138

1136-
export function flushSync<A, R>(fn: A => R, a: A): R {
1139+
// Overload the definition to the two valid signatures.
1140+
// Warning, this opts-out of checking the function body.
1141+
declare function flushSync<R>(fn: () => R): R;
1142+
// eslint-disable-next-line no-redeclare
1143+
declare function flushSync(): void;
1144+
// eslint-disable-next-line no-redeclare
1145+
export function flushSync(fn) {
11371146
if (__DEV__) {
11381147
if ((executionContext & (RenderContext | CommitContext)) !== NoContext) {
11391148
console.error(
@@ -1143,7 +1152,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
11431152
);
11441153
}
11451154
}
1146-
return flushSyncWithoutWarningIfAlreadyRendering(fn, a);
1155+
return flushSyncWithoutWarningIfAlreadyRendering(fn);
11471156
}
11481157

11491158
export function flushControlled(fn: () => mixed): void {

packages/react-test-renderer/src/ReactTestRenderer.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,7 @@ function create(element: React$Element<any>, options: TestRendererOptions) {
536536
return getPublicRootInstance(root);
537537
},
538538

539-
unstable_flushSync<T>(fn: () => T): T {
540-
return flushSync(fn);
541-
},
539+
unstable_flushSync: flushSync,
542540
};
543541

544542
Object.defineProperty(

0 commit comments

Comments
 (0)