Skip to content

Commit

Permalink
expose kaioken/utils, add new util methods
Browse files Browse the repository at this point in the history
  • Loading branch information
LankyMoose committed Mar 26, 2024
1 parent 3e7415e commit 3bdbb3f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
18 changes: 13 additions & 5 deletions packages/lib/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { GlobalContext } from "./globalContext"
import { node, nodeToCtxMap } from "./globals.js"

export {
isVNode,
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/lib/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/utils"
1 change: 1 addition & 0 deletions packages/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/utils.js"
2 changes: 1 addition & 1 deletion packages/vite-plugin-kaioken/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions sandbox/ssr/src/components/Counter.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<span>count: {count}</span>
Expand Down

0 comments on commit 3bdbb3f

Please sign in to comment.