Skip to content

Commit 5a41cbb

Browse files
committed
+
1 parent b1c080c commit 5a41cbb

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/utils/dom.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
CHILD,
4848
TREE,
4949
PARENT,
50+
SEEN_TREE_NODES,
5051
} from './shared';
5152
import { isRehydrationScheduled } from './ssr/rehydration';
5253
import { createHotReload } from './hmr';
@@ -788,14 +789,26 @@ export const $_maybeHelper = (
788789
return value;
789790
};
790791

791-
let parentContext: Root | Component<any> | null = null;
792+
let parentContext: Array<number> = [];
793+
let parentContextIndex = -1;
792794

793795
export const setParentContext = (value: Root | Component<any> | null) => {
794-
parentContext = value;
795-
}
796+
if (value === null) {
797+
parentContextIndex--;
798+
parentContext.pop();
799+
} else {
800+
parentContextIndex++;
801+
parentContext.push(value[COMPONENT_ID_PROPERTY]!);
802+
}
803+
};
796804
export const getParentContext = () => {
797-
return parentContext;
798-
}
805+
if (IS_DEV_MODE) {
806+
if (!TREE.get(parentContext[parentContextIndex]!)) {
807+
throw new Error('unable to get parent context before set');
808+
}
809+
}
810+
return TREE.get(parentContext[parentContextIndex]!);
811+
};
799812

800813
function component(
801814
comp: ComponentReturnType | Component | typeof Component,
@@ -1177,7 +1190,9 @@ export function $_GET_ARGS(ctx: Component<any>, args: IArguments) {
11771190
ctx[$args] = ctx[$args] || args[0] || {};
11781191
ctx[RENDERED_NODES_PROPERTY] = ctx[RENDERED_NODES_PROPERTY] ?? [];
11791192
ctx[COMPONENT_ID_PROPERTY] = ctx[COMPONENT_ID_PROPERTY] ?? cId();
1180-
addToTree(getParentContext()!, ctx);
1193+
if (!SEEN_TREE_NODES.has(ctx)) {
1194+
addToTree(getParentContext()!, ctx);
1195+
}
11811196
}
11821197
export function $_GET_SLOTS(ctx: any, args: any) {
11831198
return (args[0] || {})[$SLOTS_SYMBOL] || ctx[$args]?.[$SLOTS_SYMBOL] || {};

src/utils/shared.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export function setBounds(component: ComponentReturnType) {
118118
BOUNDS.delete(ctx);
119119
});
120120
}
121-
const SEEN_TREE_NODES = new WeakSet();
121+
export const SEEN_TREE_NODES = new WeakSet();
122122
export const TREE: Map<number, Component<any>> = new Map();
123123
export const CHILD: Map<number, Array<number> | undefined> = new Map();
124124
export const PARENT: Map<number, number> = new Map();

0 commit comments

Comments
 (0)