Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Aug 27, 2024
1 parent f9cf1bb commit cfba033
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 33 deletions.
7 changes: 6 additions & 1 deletion packages/myreact-reconciler/src/processHook/feature.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { __my_react_internal__ } from "@my-react/react";
import { HOOK_TYPE, ListTree, STATE_TYPE, include } from "@my-react/react-shared";

import { listenerMap } from "../renderDispatch";
import { createHookNode, effectHookNode, updateHookNode } from "../runtimeHook";
import { currentRenderDispatch } from "../share";
import { currentRenderDispatch, safeCallWithFiber } from "../share";

import type { MyReactFiberNode } from "../runtimeFiber";
import type { MyReactHookNode } from "../runtimeHook";
Expand Down Expand Up @@ -47,9 +48,13 @@ export const processHookNode = ({ type, reducer, value, deps }: RenderHookParams
// initial
if (include(fiber.state, STATE_TYPE.__create__)) {
currentHook = createHookNode({ type, reducer, value, deps }, fiber);

safeCallWithFiber({ fiber, action: () => listenerMap.get(renderDispatch)?.hookInitial?.forEach((cb) => cb(currentHook)) });
} else {
// update
currentHook = updateHookNode({ type, reducer, value, deps }, fiber, __DEV__ && Boolean(include(fiber.state, STATE_TYPE.__hmr__)));

safeCallWithFiber({ fiber, action: () => listenerMap.get(renderDispatch)?.hookUpdate?.forEach((cb) => cb(currentHook)) });
}

currentHookNodeIndex.current++;
Expand Down
15 changes: 8 additions & 7 deletions packages/myreact-reconciler/src/processState/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getInstanceOwnerFiber } from "../runtimeGenerate";
import { getCurrentDispatchFromFiber, getElementName, onceWarnWithKeyAndFiber, safeCallWithFiber, syncFlush } from "../share";

import type { MyReactFiberNode } from "../runtimeFiber";
import type { MyReactHookNode } from "../runtimeHook";
import type { MyReactInternalInstance, RenderFiber, UpdateQueue } from "@my-react/react";

export type UpdateQueueDev = UpdateQueue<{
Expand Down Expand Up @@ -44,7 +45,9 @@ export const processState = (_params: UpdateQueue) => {

const renderDispatch = getCurrentDispatchFromFiber(ownerFiber);

safeCallWithFiber({ fiber: ownerFiber, action: () => listenerMap.get(renderDispatch)?.fiberTrigger?.forEach((cb) => cb(ownerFiber, _params)) });
if (renderDispatch?.enableUpdate) {
safeCallWithFiber({ fiber: ownerFiber, action: () => listenerMap.get(renderDispatch)?.fiberTrigger?.forEach((cb) => cb(ownerFiber, _params)) });
}

const isInitial = getInstanceOwnerFiber(_params.trigger as MyReactInternalInstance)?.mode === MODE_TYPE.__initial__;

Expand All @@ -53,8 +56,6 @@ export const processState = (_params: UpdateQueue) => {

if (!ownerFiber || include(ownerFiber.state, STATE_TYPE.__unmount__)) return;

const renderDispatch = getCurrentDispatchFromFiber(ownerFiber);

// if current dispatch is a server || noop
if (!renderDispatch.enableUpdate) return;

Expand Down Expand Up @@ -108,8 +109,6 @@ export const processState = (_params: UpdateQueue) => {

if (!ownerFiber || include(ownerFiber?.state, STATE_TYPE.__unmount__)) return;

const renderDispatch = getCurrentDispatchFromFiber(ownerFiber);

if (!renderDispatch.enableUpdate) return;

if (__DEV__ && !syncFlush && currentComponentFiber.current) {
Expand Down Expand Up @@ -149,6 +148,10 @@ export const processState = (_params: UpdateQueue) => {
lastRenderComponentTimeStep = now;
}

const trigger = _params.trigger as MyReactHookNode;

safeCallWithFiber({ fiber: ownerFiber, action: () => listenerMap.get(renderDispatch)?.hookTrigger?.forEach((cb) => cb(trigger, _params)) });

ownerFiber.updateQueue = ownerFiber.updateQueue || new ListTree();

ownerFiber.updateQueue.push(_params);
Expand All @@ -159,8 +162,6 @@ export const processState = (_params: UpdateQueue) => {

if (!ownerFiber || include(ownerFiber.state, STATE_TYPE.__unmount__)) return;

const renderDispatch = getCurrentDispatchFromFiber(ownerFiber);

if (!renderDispatch.enableUpdate) return;

ownerFiber.updateQueue = ownerFiber.updateQueue || new ListTree();
Expand Down
145 changes: 141 additions & 4 deletions packages/myreact-reconciler/src/renderDispatch/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@ import { MyWeakMap, NODE_TYPE, onceWarnWithKeyAndFiber, safeCall } from "../shar

import type { fiberKey, refKey, RenderDispatch, RuntimeMap } from "./interface";
import type { MyReactFiberContainer, MyReactFiberNode } from "../runtimeFiber";
import type { MyReactHookNode } from "../runtimeHook";
import type { HMR } from "../share";
import type { createContext, MyReactElementNode, UpdateQueue } from "@my-react/react";

type Listeners = {
fiberInitial: Set<(fiber: MyReactFiberNode) => void>;
fiberUpdate: Set<(fiber: MyReactFiberNode) => void>;
fiberTrigger: Set<(fiber: MyReactFiberNode, updater: UpdateQueue) => void>;
fiberUnmount: Set<(fiber: MyReactFiberNode) => void>;
fiberHMR?: Set<(fiber: MyReactFiberNode) => void>;
fiberWarn?: Set<(fiber: MyReactFiberNode, ...args: any) => void>;
fiberError?: Set<(fiber: MyReactFiberNode, ...args: any) => void>;
fiberHasChange: Set<(list: ListTree<MyReactFiberNode>) => void>;
fiberTrigger: Set<(fiber: MyReactFiberNode, updater: UpdateQueue) => void>;
performanceWarn?: Set<(fiber: MyReactFiberNode) => void>;

hookInitial: Set<(hook: MyReactHookNode) => void>;
hookUpdate: Set<(hook: MyReactHookNode) => void>;
hookTrigger: Set<(hook: MyReactHookNode, updater: UpdateQueue) => void>;
hookUnmount: Set<(hook: MyReactHookNode) => void>;

beforeCommit: Set<() => void>;
afterCommit: Set<() => void>;
beforeUpdate: Set<() => void>;
Expand All @@ -42,8 +50,14 @@ const getInitialValue = (): Listeners => {
fiberHasChange: new Set(),
fiberUnmount: new Set(),
fiberHMR: new Set(),
fiberWarn: new Set(),
fiberError: new Set(),
fiberTrigger: new Set(),
performanceWarn: new Set(),
hookInitial: new Set(),
hookUpdate: new Set(),
hookTrigger: new Set(),
hookUnmount: new Set(),
beforeCommit: new Set(),
afterCommit: new Set(),
beforeUpdate: new Set(),
Expand All @@ -57,7 +71,10 @@ const getInitialValue = (): Listeners => {
fiberHasChange: new Set(),
fiberUnmount: new Set(),
fiberTrigger: new Set(),
performanceWarn: new Set(),
hookInitial: new Set(),
hookUpdate: new Set(),
hookTrigger: new Set(),
hookUnmount: new Set(),
beforeCommit: new Set(),
afterCommit: new Set(),
beforeUpdate: new Set(),
Expand Down Expand Up @@ -285,12 +302,52 @@ export class CustomRenderDispatch implements RenderDispatch {
set?.add?.(onceCb);
}

onFiberWarn(cb: (_fiber: MyReactFiberNode, ...args: any) => void) {
const set = listenerMap.get(this).fiberWarn;

set?.add?.(cb);

return () => set?.delete?.(cb);
}

onceFiberWarn(cb: (_fiber: MyReactFiberNode, ...args: any) => void) {
const set = listenerMap.get(this).fiberWarn;

const onceCb = (_fiber: MyReactFiberNode) => {
cb(_fiber);

set?.delete?.(onceCb);
};

set?.add?.(onceCb);
}

onFiberError(cb: (_fiber: MyReactFiberNode, ...args: any) => void) {
const set = listenerMap.get(this).fiberError;

set?.add?.(cb);

return () => set?.delete?.(cb);
}

onceFiberError(cb: (_fiber: MyReactFiberNode, ...args: any) => void) {
const set = listenerMap.get(this).fiberError;

const onceCb = (_fiber: MyReactFiberNode) => {
cb(_fiber);

set?.delete?.(onceCb);
};

set?.add?.(onceCb);
}

onPerformanceWarn(cb: (_fiber: MyReactFiberNode) => void) {
const set = listenerMap.get(this).performanceWarn;

set.add(cb);
set?.add?.(cb);

return () => set.delete(cb);
return () => set?.delete?.(cb);
}

oncePerformanceWarn(cb: (_fiber: MyReactFiberNode) => void) {
Expand All @@ -299,6 +356,86 @@ export class CustomRenderDispatch implements RenderDispatch {
const onceCb = (_fiber: MyReactFiberNode) => {
cb(_fiber);

set?.delete?.(onceCb);
};

set?.add?.(onceCb);
}

onHookInitial(cb: (_hook: MyReactHookNode) => void) {
const set = listenerMap.get(this).hookInitial;

set.add(cb);

return () => set.delete(cb);
}

onceHookInitial(cb: (_hook: MyReactHookNode) => void) {
const set = listenerMap.get(this).hookInitial;

const onceCb = (_hook: MyReactHookNode) => {
cb(_hook);

set.delete(onceCb);
};

set.add(onceCb);
}

onHookUpdate(cb: (_hook: MyReactHookNode) => void) {
const set = listenerMap.get(this).hookUpdate;

set.add(cb);

return () => set.delete(cb);
}

onceHookUpdate(cb: (_hook: MyReactHookNode) => void) {
const set = listenerMap.get(this).hookUpdate;

const onceCb = (_hook: MyReactHookNode) => {
cb(_hook);

set.delete(onceCb);
};

set.add(onceCb);
}

onHookUnmount(cb: (_hook: MyReactHookNode) => void) {
const set = listenerMap.get(this).hookUnmount;

set.add(cb);

return () => set.delete(cb);
}

onceHookUnmount(cb: (_hook: MyReactHookNode) => void) {
const set = listenerMap.get(this).hookUnmount;

const onceCb = (_hook: MyReactHookNode) => {
cb(_hook);

set.delete(onceCb);
};

set.add(onceCb);
}

onHookTrigger(cb: (_hook: MyReactHookNode, _updater: UpdateQueue) => void) {
const set = listenerMap.get(this).hookTrigger;

set.add(cb);

return () => set.delete(cb);
}

onceHookTrigger(cb: (_hook: MyReactHookNode, _updater: UpdateQueue) => void) {
const set = listenerMap.get(this).hookTrigger;

const onceCb = (_hook: MyReactHookNode, _updater: UpdateQueue) => {
cb(_hook, _updater);

set.delete(onceCb);
};

Expand Down
11 changes: 4 additions & 7 deletions packages/myreact-reconciler/src/renderUnmount/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ export const unmountList = (list: ListTree<MyReactFiberNode>, renderDispatch: Cu
// will happen when app crash
list.listToFoot((f) => unmountPending(f, renderDispatch));

list.listToFoot((f) =>
list.listToFoot((f) => {
safeCallWithFiber({
fiber: f,
action: () => {
f._unmount();
unmountFiberNode(f, renderDispatch);
},
})
);
action: () => f._unmount(() => unmountFiberNode(f, renderDispatch)),
});
});
};

// unmount current fiber
Expand Down
2 changes: 1 addition & 1 deletion packages/myreact-reconciler/src/runtimeHook/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export const createHookNode = ({ type, value, reducer, deps }: RenderHookParams,
const line = i.getEnclosingLineNumber();
const column = i.getEnclosingColumnNumber();
const fileName = i.getFileName();
const functionName = i.getFunctionName() || 'Anonymous';
const functionName = i.getFunctionName() || "Anonymous";
return { id: `${fileName}:${line}:${column}`, name: functionName };
})
.reverse();
Expand Down
11 changes: 8 additions & 3 deletions packages/myreact-reconciler/src/runtimeHook/unmount.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { listenerMap, type CustomRenderDispatch } from "../renderDispatch";
import { unmountInstance } from "../runtimeGenerate";
import { safeCallWithFiber } from "../share";
import { getCurrentDispatchFromFiber, safeCallWithFiber } from "../share";

import type { MyReactHookNode } from "./instance";
import type { MyReactFiberNode } from "../runtimeFiber";

export const hookNodeUnmount = (hookNode: MyReactHookNode) => {
export const hookNodeUnmount = (renderDispatch: CustomRenderDispatch, fiber: MyReactFiberNode, hookNode: MyReactHookNode) => {
hookNode.hasEffect = false;

hookNode.cancel && hookNode.cancel();

safeCallWithFiber({ fiber, action: () => listenerMap.get(renderDispatch)?.hookUnmount?.forEach((cb) => cb(hookNode)) });

unmountInstance(hookNode);
};

export const hookListUnmount = (fiber: MyReactFiberNode) => {
fiber.hookList?.listToFoot?.((hookNode) => safeCallWithFiber({ fiber, action: () => hookNodeUnmount(hookNode as MyReactHookNode) }));
const renderDispatch = getCurrentDispatchFromFiber(fiber);

fiber.hookList?.listToFoot?.((hookNode) => safeCallWithFiber({ fiber, action: () => hookNodeUnmount(renderDispatch, fiber, hookNode as MyReactHookNode) }));
};
Loading

0 comments on commit cfba033

Please sign in to comment.