Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Nov 25, 2024
1 parent 3c8a603 commit 53d4f0a
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 63 deletions.
37 changes: 25 additions & 12 deletions packages/myreact-reconciler/src/processState/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { isErrorBoundariesComponent } from "../dispatchErrorBoundaries";
import { listenerMap } from "../renderDispatch";
import { prepareUpdateOnFiber, type MyReactFiberNode } from "../runtimeFiber";
import { getInstanceOwnerFiber } from "../runtimeGenerate";
import { getCurrentDispatchFromFiber, getElementName, onceWarnWithKeyAndFiber, safeCallWithCurrentFiber, syncFlush } from "../share";
import {
enableLogForCurrentFlowIsRunning,
getCurrentDispatchFromFiber,
getElementName,
onceWarnWithKeyAndFiber,
safeCallWithCurrentFiber,
syncFlush,
} from "../share";

import type { MyReactHookNode } from "../runtimeHook";
import type { MyReactComponent, MyReactInternalInstance, RenderFiber, UpdateQueue } from "@my-react/react";
Expand Down Expand Up @@ -95,14 +102,16 @@ export const processState = (_params: UpdateQueue) => {
);
} else if (!isErrorBoundariesComponent(ownerFiber)) {
const triggeredElementName = getElementName(ownerFiber);

const currentElementName = getElementName(currentCFiber);

onceWarnWithKeyAndFiber(
currentRFiber,
`updateWhenCurrentFlowIsRunning-${triggeredElementName}`,
`[@my-react/react] trigger an update for ${triggeredElementName} when current update flow is running, this is a unexpected behavior, please make sure current render function for ${currentElementName} is a pure function`
);
if (enableLogForCurrentFlowIsRunning.current) {
onceWarnWithKeyAndFiber(
currentRFiber,
`updateWhenCurrentFlowIsRunning-${triggeredElementName}`,
`[@my-react/react] trigger an update for ${triggeredElementName} when current update flow is running, this is a unexpected behavior, please make sure current render function for ${currentElementName} is a pure function`
);
}
}

lastRenderComponentFiber = currentCFiber;
Expand Down Expand Up @@ -156,12 +165,16 @@ export const processState = (_params: UpdateQueue) => {
);
} else if (!isErrorBoundariesComponent(ownerFiber)) {
const triggeredElementName = getElementName(ownerFiber);

const currentElementName = getElementName(currentCFiber);
onceWarnWithKeyAndFiber(
currentRFiber,
`updateWhenCurrentFlowIsRunning-${triggeredElementName}`,
`[@my-react/react] trigger an update for ${triggeredElementName} when current update flow is running, this is a unexpected behavior, please make sure current render function for ${currentElementName} is a pure function`
);

if (enableLogForCurrentFlowIsRunning.current) {
onceWarnWithKeyAndFiber(
currentRFiber,
`updateWhenCurrentFlowIsRunning-${triggeredElementName}`,
`[@my-react/react] trigger an update for ${triggeredElementName} when current update flow is running, this is a unexpected behavior, please make sure current render function for ${currentElementName} is a pure function`
);
}
}
lastRenderComponentFiber = currentCFiber;
lastRenderComponentTimeStep = now;
Expand Down
13 changes: 13 additions & 0 deletions packages/myreact-reconciler/src/share/elementType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ type ReturnTypeFromElement = {

const emptyProps = {};

const checkIsMyReactElement = (element: MyReactElementNode) => {
if (isValidElement(element)) {
const isMyReactElement = element._jsx || element._legacy;
if (!isMyReactElement) {
devWarn(`[@my-react/react] look like current element is not a valid @my-react element %o`, element);
}
}
}

export const getElementTypeFromType = (type: MyReactComponentType): MyReactComponentType => {
if (typeof type === "object") {
switch (type[TYPEKEY]) {
Expand All @@ -51,6 +60,10 @@ export const getElementTypeFromType = (type: MyReactComponentType): MyReactCompo
export const getTypeFromElementNode = (element: MyReactElementNode): ReturnTypeFromElement => {
let nodeType = NODE_TYPE.__initial__;

if (__DEV__) {
checkIsMyReactElement(element);
}

if (isValidElement(element)) {
return getTypeFromElement(element);
} else {
Expand Down
2 changes: 2 additions & 0 deletions packages/myreact-reconciler/src/share/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export const currentCallingFiber = createRef<MyReactFiberNode | null>(null);
export const fiberToDispatchMap = new MyWeakMap() as WeakMap<MyReactFiberNode, CustomRenderDispatch>;

export const enableFiberForLog = createRef(false);

export const enableLogForCurrentFlowIsRunning = createRef(false);
Loading

0 comments on commit 53d4f0a

Please sign in to comment.