diff --git a/packages/lib/src/utils.ts b/packages/lib/src/utils.ts index 9d1fd1dd..8f5c41b5 100644 --- a/packages/lib/src/utils.ts +++ b/packages/lib/src/utils.ts @@ -1,4 +1,4 @@ -import type { GlobalContext } from "./globalContext" +import { node, nodeToCtxMap } from "./globals.js" export { isVNode, @@ -8,12 +8,22 @@ export { propToHtmlAttr, propValueToHtmlAttrValue, shallowCompare, + getNodeGlobalContext, + getCurrentNode, propFilters, selfClosingTags, svgTags, booleanAttributes, } +function getNodeGlobalContext(node: Kaioken.VNode) { + return nodeToCtxMap.get(node) +} + +function getCurrentNode() { + return node.current +} + function isVNode(thing: unknown): thing is Kaioken.VNode { return typeof thing === "object" && thing !== null && "type" in thing } @@ -40,12 +50,10 @@ function vNodeContains( } function applyRecursive( - ctx: GlobalContext, + node: Kaioken.VNode, func: (node: Kaioken.VNode) => void ) { - if (!ctx.rootNode) return - - const nodes: Kaioken.VNode[] = [ctx.rootNode] + const nodes: Kaioken.VNode[] = [node] const apply = (node: Kaioken.VNode) => { func(node) node.child && nodes.push(node.child) diff --git a/packages/lib/utils.d.ts b/packages/lib/utils.d.ts new file mode 100644 index 00000000..5b0ab4d4 --- /dev/null +++ b/packages/lib/utils.d.ts @@ -0,0 +1 @@ +export * from "./dist/utils" diff --git a/packages/lib/utils.js b/packages/lib/utils.js new file mode 100644 index 00000000..84f20001 --- /dev/null +++ b/packages/lib/utils.js @@ -0,0 +1 @@ +export * from "./dist/utils.js" diff --git a/packages/vite-plugin-kaioken/src/index.ts b/packages/vite-plugin-kaioken/src/index.ts index 2680e5b5..0382d1c9 100644 --- a/packages/vite-plugin-kaioken/src/index.ts +++ b/packages/vite-plugin-kaioken/src/index.ts @@ -63,7 +63,7 @@ if (import.meta.hot) { function handleUpdate(newModule, name, funcRef) { if (newModule[name]) { contexts.forEach((ctx) => { - applyRecursive(ctx, (node) => { + applyRecursive(ctx.rootNode, (node) => { if (node.type === funcRef) { node.type = newModule[name]; if (node.prev) { diff --git a/sandbox/ssr/src/components/Counter.tsx b/sandbox/ssr/src/components/Counter.tsx index b4efc233..f3cded7c 100644 --- a/sandbox/ssr/src/components/Counter.tsx +++ b/sandbox/ssr/src/components/Counter.tsx @@ -1,7 +1,13 @@ import { useState } from "kaioken" +import { getCurrentNode, getNodeGlobalContext } from "kaioken/utils" export function Counter() { const [count, setCount] = useState(0) + + const node = getCurrentNode() + const ctx = getNodeGlobalContext(node!) + console.log(ctx, node) + return ( <> count: {count}