Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[crud] Merge useResourceEffect into useEffect #32205

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,11 @@ function useInsertionEffect(
}

function useEffect(
create: () => (() => void) | void,
inputs: Array<mixed> | void | null,
create: (() => (() => void) | void) | (() => {...} | void | null),
createDeps: Array<mixed> | void | null,
update?: ((resource: {...} | void | null) => void) | void,
updateDeps?: Array<mixed> | void | null,
destroy?: ((resource: {...} | void | null) => void) | void,
): void {
nextHook();
hookLog.push({
Expand Down Expand Up @@ -739,11 +742,11 @@ function useHostTransitionStatus(): TransitionStatus {
}

function useResourceEffect(
create: () => mixed,
create: () => {...} | void | null,
createDeps: Array<mixed> | void | null,
update: ((resource: mixed) => void) | void,
update: ((resource: {...} | void | null) => void) | void,
updateDeps: Array<mixed> | void | null,
destroy: ((resource: mixed) => void) | void,
destroy: ((resource: {...} | void | null) => void) | void,
) {
nextHook();
hookLog.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ let useRef;
let useImperativeHandle;
let useInsertionEffect;
let useLayoutEffect;
let useResourceEffect;
let useDebugValue;
let forwardRef;
let yieldedValues;
Expand All @@ -52,7 +51,6 @@ function initModules() {
useImperativeHandle = React.useImperativeHandle;
useInsertionEffect = React.useInsertionEffect;
useLayoutEffect = React.useLayoutEffect;
useResourceEffect = React.experimental_useResourceEffect;
forwardRef = React.forwardRef;

yieldedValues = [];
Expand Down Expand Up @@ -655,15 +653,15 @@ describe('ReactDOMServerHooks', () => {
});
});

describe('useResourceEffect', () => {
describe('useEffect with CRUD overload', () => {
gate(flags => {
if (flags.enableUseResourceEffectHook) {
if (flags.enableUseEffectCRUDOverload) {
const yields = [];
itRenders(
'should ignore resource effects on the server',
async render => {
function Counter(props) {
useResourceEffect(
useEffect(
() => {
yieldValue('created on client');
return {resource_counter: props.count};
Expand Down
10 changes: 5 additions & 5 deletions packages/react-reconciler/src/ReactFiberCallUserSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ResourceEffectIdentityKind,
ResourceEffectUpdateKind,
} from './ReactFiberHooks';
import {enableUseResourceEffectHook} from 'shared/ReactFeatureFlags';
import {enableUseEffectCRUDOverload} from 'shared/ReactFeatureFlags';

// These indirections exists so we can exclude its stack frame in DEV (and anything below it).
// TODO: Consider marking the whole bundle instead of these boundaries.
Expand Down Expand Up @@ -183,12 +183,12 @@ export const callComponentWillUnmountInDEV: (
const callCreate = {
'react-stack-bottom-frame': function (
effect: Effect,
): (() => void) | mixed | void {
if (!enableUseResourceEffectHook) {
): (() => void) | {...} | void | null {
if (!enableUseEffectCRUDOverload) {
if (effect.resourceKind != null) {
if (__DEV__) {
console.error(
'Expected only SimpleEffects when enableUseResourceEffectHook is disabled, ' +
'Expected only SimpleEffects when enableUseEffectCRUDOverload is disabled, ' +
'got %s',
effect.resourceKind,
);
Expand Down Expand Up @@ -254,7 +254,7 @@ const callDestroy = {
export const callDestroyInDEV: (
current: Fiber,
nearestMountedAncestor: Fiber | null,
destroy: () => void,
destroy: (() => void) | (({...}) => void),
) => void = __DEV__
? // We use this technique to trick minifiers to preserve the function name.
(callDestroy['react-stack-bottom-frame'].bind(callDestroy): any)
Expand Down
55 changes: 16 additions & 39 deletions packages/react-reconciler/src/ReactFiberCommitEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
enableProfilerCommitHooks,
enableProfilerNestedUpdatePhase,
enableSchedulingProfiler,
enableUseResourceEffectHook,
enableUseEffectCRUDOverload,
enableViewTransition,
} from 'shared/ReactFeatureFlags';
import {
Expand Down Expand Up @@ -160,7 +160,7 @@ export function commitHookEffectListMount(

// Mount
let destroy;
if (enableUseResourceEffectHook) {
if (enableUseEffectCRUDOverload) {
if (effect.resourceKind === ResourceEffectIdentityKind) {
if (__DEV__) {
effect.inst.resource = runWithFiberInDEV(
Expand All @@ -170,8 +170,9 @@ export function commitHookEffectListMount(
);
if (effect.inst.resource == null) {
console.error(
'useResourceEffect must provide a callback which returns a resource. ' +
'If a managed resource is not needed here, use useEffect. Received %s',
'useEffect must provide a callback which returns a resource. ' +
'If a managed resource is not needed here, do not provide an updater or ' +
'destroy callback. Received %s',
effect.inst.resource,
);
}
Expand Down Expand Up @@ -200,7 +201,7 @@ export function commitHookEffectListMount(
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
}
if (enableUseResourceEffectHook) {
if (enableUseEffectCRUDOverload) {
if (effect.resourceKind == null) {
destroy = runWithFiberInDEV(
finishedWork,
Expand All @@ -219,7 +220,7 @@ export function commitHookEffectListMount(
setIsRunningInsertionEffect(false);
}
} else {
if (enableUseResourceEffectHook) {
if (enableUseEffectCRUDOverload) {
if (effect.resourceKind == null) {
const create = effect.create;
const inst = effect.inst;
Expand All @@ -230,7 +231,7 @@ export function commitHookEffectListMount(
if (effect.resourceKind != null) {
if (__DEV__) {
console.error(
'Expected only SimpleEffects when enableUseResourceEffectHook is disabled, ' +
'Expected only SimpleEffects when enableUseEffectCRUDOverload is disabled, ' +
'got %s',
effect.resourceKind,
);
Expand Down Expand Up @@ -261,11 +262,6 @@ export function commitHookEffectListMount(
hookName = 'useLayoutEffect';
} else if ((effect.tag & HookInsertion) !== NoFlags) {
hookName = 'useInsertionEffect';
} else if (
enableUseResourceEffectHook &&
effect.resourceKind != null
) {
hookName = 'useResourceEffect';
} else {
hookName = 'useEffect';
}
Expand All @@ -274,6 +270,7 @@ export function commitHookEffectListMount(
addendum =
' You returned null. If your effect does not require clean ' +
'up, return undefined (or nothing).';
// $FlowFixMe (@poteto) this check is safe on arbitrary non-null/void objects
} else if (typeof destroy.then === 'function') {
addendum =
'\n\nIt looks like you wrote ' +
Expand Down Expand Up @@ -337,7 +334,7 @@ export function commitHookEffectListUnmount(
const inst = effect.inst;
const destroy = inst.destroy;
if (destroy !== undefined) {
if (enableUseResourceEffectHook) {
if (enableUseEffectCRUDOverload) {
if (effect.resourceKind == null) {
inst.destroy = undefined;
}
Expand All @@ -357,12 +354,12 @@ export function commitHookEffectListUnmount(
setIsRunningInsertionEffect(true);
}
}
if (enableUseResourceEffectHook) {
if (enableUseEffectCRUDOverload) {
if (
effect.resourceKind === ResourceEffectIdentityKind &&
effect.inst.resource != null
) {
safelyCallDestroyWithResource(
safelyCallDestroy(
finishedWork,
nearestMountedAncestor,
destroy,
Expand Down Expand Up @@ -1014,31 +1011,10 @@ export function safelyDetachRef(
function safelyCallDestroy(
current: Fiber,
nearestMountedAncestor: Fiber | null,
destroy: () => void,
) {
if (__DEV__) {
runWithFiberInDEV(
current,
callDestroyInDEV,
current,
nearestMountedAncestor,
destroy,
);
} else {
try {
destroy();
} catch (error) {
captureCommitPhaseError(current, nearestMountedAncestor, error);
}
}
}

function safelyCallDestroyWithResource(
current: Fiber,
nearestMountedAncestor: Fiber | null,
destroy: mixed => void,
resource: mixed,
destroy: (() => void) | (({...}) => void),
resource?: {...} | void | null,
) {
// $FlowFixMe[extra-arg] @poteto this is safe either way because the extra arg is ignored if it's not a CRUD effect
const destroy_ = resource == null ? destroy : destroy.bind(null, resource);
if (__DEV__) {
runWithFiberInDEV(
Expand All @@ -1050,6 +1026,7 @@ function safelyCallDestroyWithResource(
);
} else {
try {
// $FlowFixMe(incompatible-call) Already bound to resource
destroy_();
} catch (error) {
captureCommitPhaseError(current, nearestMountedAncestor, error);
Expand Down
Loading
Loading