Skip to content

Commit

Permalink
[crud] Basic implementation
Browse files Browse the repository at this point in the history
This PR introduces a new experimental hook `useResourceEffect`, which is something that we're doing some very early initial tests on.

This may likely not pan out and will be removed or modified if so. Please do not rely on it as it will break.
  • Loading branch information
poteto committed Nov 8, 2024
1 parent 5c56b87 commit 1a95853
Show file tree
Hide file tree
Showing 19 changed files with 991 additions and 27 deletions.
23 changes: 18 additions & 5 deletions packages/react-reconciler/src/ReactFiberCallUserSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type {CapturedValue} from './ReactCapturedValue';

import {isRendering, setIsRendering} from './ReactCurrentFiber';
import {captureCommitPhaseError} from './ReactFiberWorkLoop';
import {SimpleEffectKind} from './ReactFiberHooks';
import {enableUseResourceEffectHook} 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 @@ -177,11 +179,22 @@ export const callComponentWillUnmountInDEV: (

const callCreate = {
'react-stack-bottom-frame': function (effect: Effect): (() => void) | void {
const create = effect.create;
const inst = effect.inst;
const destroy = create();
inst.destroy = destroy;
return destroy;
if (enableUseResourceEffectHook && typeof effect.destroy === 'function') {
const inst = effect.inst;
const destroy_ = effect.destroy;
const destroy = () => {
destroy_(effect.resource);
effect.resource = null;
};
inst.destroy = destroy;
return destroy;
} else if (effect.kind === SimpleEffectKind) {
const create = effect.create;
const inst = effect.inst;
const destroy = create();
inst.destroy = destroy;
return destroy;
}
},
};

Expand Down
76 changes: 67 additions & 9 deletions packages/react-reconciler/src/ReactFiberCommitEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
enableProfilerNestedUpdatePhase,
enableSchedulingProfiler,
enableScopeAPI,
enableUseResourceEffectHook,
} from 'shared/ReactFeatureFlags';
import {
ClassComponent,
Expand Down Expand Up @@ -70,6 +71,7 @@ import {
} from './ReactFiberCallUserSpace';

import {runWithFiberInDEV} from './ReactCurrentFiber';
import {ResourceEffectKind, SimpleEffectKind} from './ReactFiberHooks';

function shouldProfile(current: Fiber): boolean {
return (
Expand Down Expand Up @@ -146,19 +148,51 @@ export function commitHookEffectListMount(

// Mount
let destroy;
if (
enableUseResourceEffectHook &&
effect.kind === ResourceEffectKind
) {
if (typeof effect.create === 'function') {
effect.resource = effect.create();
if (__DEV__) {
if (effect.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',
effect.resource,
);
}
}
} else if (
typeof effect.update === 'function' &&
effect.resource != null
) {
// TODO(@poteto) what about multiple updates?
effect.update(effect.resource);
}
destroy = effect.destroy;
}
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
}
destroy = runWithFiberInDEV(finishedWork, callCreateInDEV, effect);
if (effect.kind === SimpleEffectKind) {
destroy = runWithFiberInDEV(
finishedWork,
callCreateInDEV,
effect,
);
}
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(false);
}
} else {
const create = effect.create;
const inst = effect.inst;
destroy = create();
inst.destroy = destroy;
if (effect.kind === SimpleEffectKind) {
const create = effect.create;
const inst = effect.inst;
destroy = create();
inst.destroy = destroy;
}
}

if (enableSchedulingProfiler) {
Expand All @@ -176,6 +210,11 @@ export function commitHookEffectListMount(
hookName = 'useLayoutEffect';
} else if ((effect.tag & HookInsertion) !== NoFlags) {
hookName = 'useInsertionEffect';
} else if (
enableUseResourceEffectHook &&
effect.kind === ResourceEffectKind
) {
hookName = 'useResourceEffect';
} else {
hookName = 'useEffect';
}
Expand Down Expand Up @@ -244,9 +283,21 @@ export function commitHookEffectListUnmount(
if ((effect.tag & flags) === flags) {
// Unmount
const inst = effect.inst;
if (
enableUseResourceEffectHook &&
effect.kind === ResourceEffectKind &&
effect.resource != null &&
(effect.create != null ||
// TODO(@poteto) this feels gross
finishedWork.return == null)
) {
inst.destroy = effect.destroy;
}
const destroy = inst.destroy;
if (destroy !== undefined) {
inst.destroy = undefined;
const resource = effect.resource;
effect.resource = null;
if (enableSchedulingProfiler) {
if ((flags & HookPassive) !== NoHookEffect) {
markComponentPassiveEffectUnmountStarted(finishedWork);
Expand All @@ -260,7 +311,12 @@ export function commitHookEffectListUnmount(
setIsRunningInsertionEffect(true);
}
}
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
safelyCallDestroy(
finishedWork,
nearestMountedAncestor,
destroy,
resource,
);
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(false);
Expand Down Expand Up @@ -876,19 +932,21 @@ export function safelyDetachRef(
function safelyCallDestroy(
current: Fiber,
nearestMountedAncestor: Fiber | null,
destroy: () => void,
destroy: mixed => void,
resource: mixed,
) {
const destroy_ = resource == null ? destroy : destroy.bind(null, resource);
if (__DEV__) {
runWithFiberInDEV(
current,
callDestroyInDEV,
current,
nearestMountedAncestor,
destroy,
destroy_,
);
} else {
try {
destroy();
destroy_();
} catch (error) {
captureCommitPhaseError(current, nearestMountedAncestor, error);
}
Expand Down
Loading

0 comments on commit 1a95853

Please sign in to comment.