Skip to content

Commit 899e3d1

Browse files
authored
[crud] Narrow resource type (#32203)
Small refactor to the `resource` type to narrow it to an arbitrary object or void/null instead of the top type. This makes the overload on useEffect simpler since the return type of create is no longer widened to the top type when we merge their definitions. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/32203). * #32206 * #32205 * #32204 * __->__ #32203
1 parent 0a7dc1b commit 899e3d1

File tree

6 files changed

+54
-52
lines changed

6 files changed

+54
-52
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,11 +739,11 @@ function useHostTransitionStatus(): TransitionStatus {
739739
}
740740

741741
function useResourceEffect(
742-
create: () => mixed,
742+
create: () => {...} | void | null,
743743
createDeps: Array<mixed> | void | null,
744-
update: ((resource: mixed) => void) | void,
744+
update: ((resource: {...} | void | null) => void) | void,
745745
updateDeps: Array<mixed> | void | null,
746-
destroy: ((resource: mixed) => void) | void,
746+
destroy: ((resource: {...} | void | null) => void) | void,
747747
) {
748748
nextHook();
749749
hookLog.push({

packages/react-reconciler/src/ReactFiberCallUserSpace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export const callComponentWillUnmountInDEV: (
183183
const callCreate = {
184184
'react-stack-bottom-frame': function (
185185
effect: Effect,
186-
): (() => void) | mixed | void {
186+
): (() => void) | {...} | void | null {
187187
if (!enableUseResourceEffectHook) {
188188
if (effect.resourceKind != null) {
189189
if (__DEV__) {

packages/react-reconciler/src/ReactFiberCommitEffects.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ export function commitHookEffectListMount(
274274
addendum =
275275
' You returned null. If your effect does not require clean ' +
276276
'up, return undefined (or nothing).';
277+
// $FlowFixMe (@poteto) this check is safe on arbitrary non-null/void objects
277278
} else if (typeof destroy.then === 'function') {
278279
addendum =
279280
'\n\nIt looks like you wrote ' +
@@ -1036,10 +1037,10 @@ function safelyCallDestroy(
10361037
function safelyCallDestroyWithResource(
10371038
current: Fiber,
10381039
nearestMountedAncestor: Fiber | null,
1039-
destroy: mixed => void,
1040-
resource: mixed,
1040+
destroy: ({...}) => void,
1041+
resource: {...},
10411042
) {
1042-
const destroy_ = resource == null ? destroy : destroy.bind(null, resource);
1043+
const destroy_ = destroy.bind(null, resource);
10431044
if (__DEV__) {
10441045
runWithFiberInDEV(
10451046
current,
@@ -1050,6 +1051,7 @@ function safelyCallDestroyWithResource(
10501051
);
10511052
} else {
10521053
try {
1054+
// $FlowFixMe(incompatible-call) Already bound to resource
10531055
destroy_();
10541056
} catch (error) {
10551057
captureCommitPhaseError(current, nearestMountedAncestor, error);

packages/react-reconciler/src/ReactFiberHooks.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ export type Hook = {
205205
// the additional memory and we can follow up with performance
206206
// optimizations later.
207207
type EffectInstance = {
208-
resource: mixed,
209-
destroy: void | (() => void) | ((resource: mixed) => void),
208+
resource: {...} | void | null,
209+
destroy: void | (() => void) | ((resource: {...} | void | null) => void),
210210
};
211211

212212
export const ResourceEffectIdentityKind: 0 = 0;
@@ -229,15 +229,15 @@ export type ResourceEffectIdentity = {
229229
resourceKind: typeof ResourceEffectIdentityKind,
230230
tag: HookFlags,
231231
inst: EffectInstance,
232-
create: () => mixed,
232+
create: () => {...} | void | null,
233233
deps: Array<mixed> | void | null,
234234
next: Effect,
235235
};
236236
export type ResourceEffectUpdate = {
237237
resourceKind: typeof ResourceEffectUpdateKind,
238238
tag: HookFlags,
239239
inst: EffectInstance,
240-
update: ((resource: mixed) => void) | void,
240+
update: ((resource: {...} | void | null) => void) | void,
241241
deps: Array<mixed> | void | null,
242242
next: Effect,
243243
identity: ResourceEffectIdentity,
@@ -2540,9 +2540,9 @@ function pushResourceEffect(
25402540
identityTag: HookFlags,
25412541
updateTag: HookFlags,
25422542
inst: EffectInstance,
2543-
create: () => mixed,
2543+
create: () => {...} | void | null,
25442544
createDeps: Array<mixed> | void | null,
2545-
update: ((resource: mixed) => void) | void,
2545+
update: ((resource: {...} | void | null) => void) | void,
25462546
updateDeps: Array<mixed> | void | null,
25472547
): Effect {
25482548
const effectIdentity: ResourceEffectIdentity = {
@@ -2694,11 +2694,11 @@ function updateEffect(
26942694
}
26952695

26962696
function mountResourceEffect(
2697-
create: () => mixed,
2697+
create: () => {...} | void | null,
26982698
createDeps: Array<mixed> | void | null,
2699-
update: ((resource: mixed) => void) | void,
2699+
update: ((resource: {...} | void | null) => void) | void,
27002700
updateDeps: Array<mixed> | void | null,
2701-
destroy: ((resource: mixed) => void) | void,
2701+
destroy: ((resource: {...} | void | null) => void) | void,
27022702
) {
27032703
if (
27042704
__DEV__ &&
@@ -2730,11 +2730,11 @@ function mountResourceEffect(
27302730
function mountResourceEffectImpl(
27312731
fiberFlags: Flags,
27322732
hookFlags: HookFlags,
2733-
create: () => mixed,
2733+
create: () => {...} | void | null,
27342734
createDeps: Array<mixed> | void | null,
2735-
update: ((resource: mixed) => void) | void,
2735+
update: ((resource: {...} | void | null) => void) | void,
27362736
updateDeps: Array<mixed> | void | null,
2737-
destroy: ((resource: mixed) => void) | void,
2737+
destroy: ((resource: {...} | void | null) => void) | void,
27382738
) {
27392739
const hook = mountWorkInProgressHook();
27402740
currentlyRenderingFiber.flags |= fiberFlags;
@@ -2752,11 +2752,11 @@ function mountResourceEffectImpl(
27522752
}
27532753

27542754
function updateResourceEffect(
2755-
create: () => mixed,
2755+
create: () => {...} | void | null,
27562756
createDeps: Array<mixed> | void | null,
2757-
update: ((resource: mixed) => void) | void,
2757+
update: ((resource: {...} | void | null) => void) | void,
27582758
updateDeps: Array<mixed> | void | null,
2759-
destroy: ((resource: mixed) => void) | void,
2759+
destroy: ((resource: {...} | void | null) => void) | void,
27602760
) {
27612761
updateResourceEffectImpl(
27622762
PassiveEffect,
@@ -2772,11 +2772,11 @@ function updateResourceEffect(
27722772
function updateResourceEffectImpl(
27732773
fiberFlags: Flags,
27742774
hookFlags: HookFlags,
2775-
create: () => mixed,
2775+
create: () => {...} | void | null,
27762776
createDeps: Array<mixed> | void | null,
2777-
update: ((resource: mixed) => void) | void,
2777+
update: ((resource: {...} | void | null) => void) | void,
27782778
updateDeps: Array<mixed> | void | null,
2779-
destroy: ((resource: mixed) => void) | void,
2779+
destroy: ((resource: {...} | void | null) => void) | void,
27802780
) {
27812781
const hook = updateWorkInProgressHook();
27822782
const effect: Effect = hook.memoizedState;
@@ -4245,11 +4245,11 @@ if (__DEV__) {
42454245
if (enableUseResourceEffectHook) {
42464246
(HooksDispatcherOnMountInDEV: Dispatcher).useResourceEffect =
42474247
function useResourceEffect(
4248-
create: () => mixed,
4248+
create: () => {...} | void | null,
42494249
createDeps: Array<mixed> | void | null,
4250-
update: ((resource: mixed) => void) | void,
4250+
update: ((resource: {...} | void | null) => void) | void,
42514251
updateDeps: Array<mixed> | void | null,
4252-
destroy: ((resource: mixed) => void) | void,
4252+
destroy: ((resource: {...} | void | null) => void) | void,
42534253
): void {
42544254
currentHookNameInDev = 'useResourceEffect';
42554255
mountHookTypesDev();
@@ -4433,11 +4433,11 @@ if (__DEV__) {
44334433
if (enableUseResourceEffectHook) {
44344434
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useResourceEffect =
44354435
function useResourceEffect(
4436-
create: () => mixed,
4436+
create: () => {...} | void | null,
44374437
createDeps: Array<mixed> | void | null,
4438-
update: ((resource: mixed) => void) | void,
4438+
update: ((resource: {...} | void | null) => void) | void,
44394439
updateDeps: Array<mixed> | void | null,
4440-
destroy: ((resource: mixed) => void) | void,
4440+
destroy: ((resource: {...} | void | null) => void) | void,
44414441
): void {
44424442
currentHookNameInDev = 'useResourceEffect';
44434443
updateHookTypesDev();
@@ -4620,11 +4620,11 @@ if (__DEV__) {
46204620
if (enableUseResourceEffectHook) {
46214621
(HooksDispatcherOnUpdateInDEV: Dispatcher).useResourceEffect =
46224622
function useResourceEffect(
4623-
create: () => mixed,
4623+
create: () => {...} | void | null,
46244624
createDeps: Array<mixed> | void | null,
4625-
update: ((resource: mixed) => void) | void,
4625+
update: ((resource: {...} | void | null) => void) | void,
46264626
updateDeps: Array<mixed> | void | null,
4627-
destroy: ((resource: mixed) => void) | void,
4627+
destroy: ((resource: {...} | void | null) => void) | void,
46284628
) {
46294629
currentHookNameInDev = 'useResourceEffect';
46304630
updateHookTypesDev();
@@ -4807,11 +4807,11 @@ if (__DEV__) {
48074807
if (enableUseResourceEffectHook) {
48084808
(HooksDispatcherOnRerenderInDEV: Dispatcher).useResourceEffect =
48094809
function useResourceEffect(
4810-
create: () => mixed,
4810+
create: () => {...} | void | null,
48114811
createDeps: Array<mixed> | void | null,
4812-
update: ((resource: mixed) => void) | void,
4812+
update: ((resource: {...} | void | null) => void) | void,
48134813
updateDeps: Array<mixed> | void | null,
4814-
destroy: ((resource: mixed) => void) | void,
4814+
destroy: ((resource: {...} | void | null) => void) | void,
48154815
) {
48164816
currentHookNameInDev = 'useResourceEffect';
48174817
updateHookTypesDev();
@@ -5019,11 +5019,11 @@ if (__DEV__) {
50195019
if (enableUseResourceEffectHook) {
50205020
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useResourceEffect =
50215021
function useResourceEffect(
5022-
create: () => mixed,
5022+
create: () => {...} | void | null,
50235023
createDeps: Array<mixed> | void | null,
5024-
update: ((resource: mixed) => void) | void,
5024+
update: ((resource: {...} | void | null) => void) | void,
50255025
updateDeps: Array<mixed> | void | null,
5026-
destroy: ((resource: mixed) => void) | void,
5026+
destroy: ((resource: {...} | void | null) => void) | void,
50275027
): void {
50285028
currentHookNameInDev = 'useResourceEffect';
50295029
warnInvalidHookAccess();
@@ -5232,11 +5232,11 @@ if (__DEV__) {
52325232
if (enableUseResourceEffectHook) {
52335233
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useResourceEffect =
52345234
function useResourceEffect(
5235-
create: () => mixed,
5235+
create: () => {...} | void | null,
52365236
createDeps: Array<mixed> | void | null,
5237-
update: ((resource: mixed) => void) | void,
5237+
update: ((resource: {...} | void | null) => void) | void,
52385238
updateDeps: Array<mixed> | void | null,
5239-
destroy: ((resource: mixed) => void) | void,
5239+
destroy: ((resource: {...} | void | null) => void) | void,
52405240
) {
52415241
currentHookNameInDev = 'useResourceEffect';
52425242
warnInvalidHookAccess();
@@ -5445,11 +5445,11 @@ if (__DEV__) {
54455445
if (enableUseResourceEffectHook) {
54465446
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useResourceEffect =
54475447
function useResourceEffect(
5448-
create: () => mixed,
5448+
create: () => {...} | void | null,
54495449
createDeps: Array<mixed> | void | null,
5450-
update: ((resource: mixed) => void) | void,
5450+
update: ((resource: {...} | void | null) => void) | void,
54515451
updateDeps: Array<mixed> | void | null,
5452-
destroy: ((resource: mixed) => void) | void,
5452+
destroy: ((resource: {...} | void | null) => void) | void,
54535453
) {
54545454
currentHookNameInDev = 'useResourceEffect';
54555455
warnInvalidHookAccess();

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,11 @@ export type Dispatcher = {
398398
useEffectEvent?: <Args, F: (...Array<Args>) => mixed>(callback: F) => F,
399399
// TODO: Non-nullable once `enableUseResourceEffectHook` is on everywhere.
400400
useResourceEffect?: (
401-
create: () => mixed,
401+
create: () => {...} | void | null,
402402
createDeps: Array<mixed> | void | null,
403-
update: ((resource: mixed) => void) | void,
403+
update: ((resource: {...} | void | null) => void) | void,
404404
updateDeps: Array<mixed> | void | null,
405-
destroy: ((resource: mixed) => void) | void,
405+
destroy: ((resource: {...} | void | null) => void) | void,
406406
) => void,
407407
useInsertionEffect(
408408
create: () => (() => void) | void,

packages/react/src/ReactHooks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ export function useEffectEvent<Args, F: (...Array<Args>) => mixed>(
202202
}
203203

204204
export function useResourceEffect(
205-
create: () => mixed,
205+
create: () => {...} | void | null,
206206
createDeps: Array<mixed> | void | null,
207-
update: ((resource: mixed) => void) | void,
207+
update: ((resource: {...} | void | null) => void) | void,
208208
updateDeps: Array<mixed> | void | null,
209-
destroy: ((resource: mixed) => void) | void,
209+
destroy: ((resource: {...} | void | null) => void) | void,
210210
): void {
211211
if (!enableUseResourceEffectHook) {
212212
throw new Error('Not implemented.');

0 commit comments

Comments
 (0)