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 15, 2024
1 parent 0480cdb commit 0106aab
Show file tree
Hide file tree
Showing 19 changed files with 1,299 additions and 30 deletions.
34 changes: 28 additions & 6 deletions packages/react-reconciler/src/ReactFiberCallUserSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import type {CapturedValue} from './ReactCapturedValue';

import {isRendering, setIsRendering} from './ReactCurrentFiber';
import {captureCommitPhaseError} from './ReactFiberWorkLoop';
import {
ResourceEffectIdentityKind,
ResourceEffectUpdateKind,
SimpleEffectKind,
} from './ReactFiberHooks';

// 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 @@ -176,12 +181,29 @@ export const callComponentWillUnmountInDEV: (
: (null: any);

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;
'react-stack-bottom-frame': function (
effect: Effect,
): (() => void) | mixed | void {
switch (effect.kind) {
case SimpleEffectKind: {
const create = effect.create;
const inst = effect.inst;
const destroy = create();
inst.destroy = destroy;
return destroy;
}
case ResourceEffectIdentityKind: {
return effect.create();
}
case ResourceEffectUpdateKind:
default: {
if (__DEV__) {
console.error(
'Could not call create on an update to a ResourceEffect. This is a bug in React.',
);
}
}
}
},
};

Expand Down
131 changes: 125 additions & 6 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 @@ -49,6 +50,7 @@ import {
Layout as HookLayout,
Insertion as HookInsertion,
Passive as HookPassive,
HasEffect as HookHasEffect,
} from './ReactHookEffectTags';
import {didWarnAboutReassigningProps} from './ReactFiberBeginWork';
import {
Expand All @@ -70,6 +72,11 @@ import {
} from './ReactFiberCallUserSpace';

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

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

// Mount
let destroy;
if (enableUseResourceEffectHook) {
if (effect.kind === ResourceEffectIdentityKind) {
if (__DEV__) {
effect.resource = runWithFiberInDEV(
finishedWork,
callCreateInDEV,
effect,
);
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 {
effect.resource = effect.create();
}
if (effect.next.kind === ResourceEffectUpdateKind) {
effect.next.resource = effect.resource;
} else {
if (__DEV__) {
console.error(
'Found identity effect without an update effect. This is a bug in React.',
);
}
}
destroy = effect.destroy;
}
if (effect.kind === ResourceEffectUpdateKind) {
if (
// We don't want to fire updates on remount during Activity
(flags & HookHasEffect) > 0 &&
typeof effect.update === 'function' &&
effect.resource != null
) {
// TODO(@poteto) what about multiple updates?
effect.update(effect.resource);
}
}
}
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 +232,11 @@ export function commitHookEffectListMount(
hookName = 'useLayoutEffect';
} else if ((effect.tag & HookInsertion) !== NoFlags) {
hookName = 'useInsertionEffect';
} else if (
enableUseResourceEffectHook &&
effect.kind === ResourceEffectIdentityKind
) {
hookName = 'useResourceEffect';
} else {
hookName = 'useEffect';
}
Expand Down Expand Up @@ -244,9 +305,34 @@ export function commitHookEffectListUnmount(
if ((effect.tag & flags) === flags) {
// Unmount
const inst = effect.inst;
if (
enableUseResourceEffectHook &&
effect.kind === ResourceEffectIdentityKind &&
effect.resource != null
) {
inst.destroy = effect.destroy;
}
const destroy = inst.destroy;
if (destroy !== undefined) {
inst.destroy = undefined;
let resource;
if (enableUseResourceEffectHook) {
if (effect.kind === ResourceEffectIdentityKind) {
resource = effect.resource;
effect.resource = null;
// TODO(@poteto) very sketchy
if (effect.next.kind === ResourceEffectUpdateKind) {
effect.next.resource = null;
effect.next.update = undefined;
} else {
if (__DEV__) {
console.error(
'Found identity effect without an update effect. This is a bug in React.',
);
}
}
}
}
if (enableSchedulingProfiler) {
if ((flags & HookPassive) !== NoHookEffect) {
markComponentPassiveEffectUnmountStarted(finishedWork);
Expand All @@ -260,7 +346,16 @@ export function commitHookEffectListUnmount(
setIsRunningInsertionEffect(true);
}
}
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
if (enableUseResourceEffectHook) {
safelyCallDestroyWithResource(
finishedWork,
nearestMountedAncestor,
destroy,
resource,
);
} else {
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
}
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(false);
Expand Down Expand Up @@ -895,6 +990,30 @@ function safelyCallDestroy(
}
}

function safelyCallDestroyWithResource(
current: Fiber,
nearestMountedAncestor: Fiber | null,
destroy: mixed => void,
resource: mixed,
) {
const destroy_ = resource == null ? destroy : destroy.bind(null, resource);
if (__DEV__) {
runWithFiberInDEV(
current,
callDestroyInDEV,
current,
nearestMountedAncestor,
destroy_,
);
} else {
try {
destroy_();
} catch (error) {
captureCommitPhaseError(current, nearestMountedAncestor, error);
}
}
}

function commitProfiler(
finishedWork: Fiber,
current: Fiber | null,
Expand Down
Loading

0 comments on commit 0106aab

Please sign in to comment.