Clean up flushSync flow types (#21887)
Ricky committed
Jul 16, 2021 at 10:37 UTC
310187264d01a31bc3079358f13662d31a079d9e
5 files changed
+37
-24
packages/react-dom/src/__tests__/ReactDOMFiberAsync-test.js
+1
-1
@@ -132,7 +132,7 @@ describe('ReactDOMFiberAsync', () => {
132
it('flushSync logs an error if already performing work', () => {
133
class Component extends React.Component {
134
componentDidUpdate() {
135
- ReactDOM.flushSync(() => {});
135
+ ReactDOM.flushSync();
136
}
137
render() {
138
return null;
packages/react-noop-renderer/src/createReactNoop.js
+1
-4
@@ -913,10 +913,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
913
}
914
},
915
916
- flushSync(fn: () => mixed) {
917
- NoopRenderer.flushSync(fn);
918
- },
919
-
916
+ flushSync: NoopRenderer.flushSync,
917
flushPassiveEffects: NoopRenderer.flushPassiveEffects,
918
919
// Logs the current state of the tree.
packages/react-reconciler/src/ReactFiberWorkLoop.new.js
+17
-8
@@ -1093,10 +1093,13 @@ export function discreteUpdates<A, B, C, D, R>(
1093
}
1094
}
1095
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) {
1103
// In legacy mode, we flush pending passive effects at the beginning of the
1104
// next event, not at the end of the previous one.
1105
if (
@@ -1116,9 +1119,9 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
1119
ReactCurrentBatchConfig.transition = 0;
1120
setCurrentUpdatePriority(DiscreteEventPriority);
1121
if (fn) {
1119
- return fn(a);
1122
+ return fn();
1123
} else {
1121
- return (undefined: $FlowFixMe);
1124
+ return undefined;
1125
}
1126
} finally {
1127
setCurrentUpdatePriority(previousPriority);
@@ -1133,7 +1136,13 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
1136
}
1137
}
1138
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) {
1146
if (__DEV__) {
1147
if ((executionContext & (RenderContext | CommitContext)) !== NoContext) {
1148
console.error(
@@ -1143,7 +1152,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
1152
);
1153
}
1154
}
1146
- return flushSyncWithoutWarningIfAlreadyRendering(fn, a);
1155
+ return flushSyncWithoutWarningIfAlreadyRendering(fn);
1156
}
1157
1158
export function flushControlled(fn: () => mixed): void {
packages/react-reconciler/src/ReactFiberWorkLoop.old.js
+17
-8
@@ -1093,10 +1093,13 @@ export function discreteUpdates<A, B, C, D, R>(
1093
}
1094
}
1095
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) {
1103
// In legacy mode, we flush pending passive effects at the beginning of the
1104
// next event, not at the end of the previous one.
1105
if (
@@ -1116,9 +1119,9 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
1119
ReactCurrentBatchConfig.transition = 0;
1120
setCurrentUpdatePriority(DiscreteEventPriority);
1121
if (fn) {
1119
- return fn(a);
1122
+ return fn();
1123
} else {
1121
- return (undefined: $FlowFixMe);
1124
+ return undefined;
1125
}
1126
} finally {
1127
setCurrentUpdatePriority(previousPriority);
@@ -1133,7 +1136,13 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
1136
}
1137
}
1138
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) {
1146
if (__DEV__) {
1147
if ((executionContext & (RenderContext | CommitContext)) !== NoContext) {
1148
console.error(
@@ -1143,7 +1152,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
1152
);
1153
}
1154
}
1146
- return flushSyncWithoutWarningIfAlreadyRendering(fn, a);
1155
+ return flushSyncWithoutWarningIfAlreadyRendering(fn);
1156
}
1157
1158
export function flushControlled(fn: () => mixed): void {
packages/react-test-renderer/src/ReactTestRenderer.js
+1
-3
@@ -536,9 +536,7 @@ function create(element: React$Element<any>, options: TestRendererOptions) {
536
return getPublicRootInstance(root);
537
},
538
539
- unstable_flushSync<T>(fn: () => T): T {
540
- return flushSync(fn);
541
- },
539
+ unstable_flushSync: flushSync,
540
};
541
542
Object.defineProperty(