Skip to content

Commit

Permalink
Merge pull request #18 from CrimsonChi/feat/watch
Browse files Browse the repository at this point in the history
feat: Signal watch
  • Loading branch information
LankyMoose authored Oct 13, 2024
2 parents acaa414 + 6503a8a commit 5ff0f5c
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 46 deletions.
9 changes: 1 addition & 8 deletions packages/devtools-host/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import { twMerge } from "tailwind-merge"
import { Flame } from "./icon/Flame"
import { useAnchorPos } from "./hooks/useAnchorPos"
import { useEffectDeep, useSpring } from "@kaioken-core/hooks"
import {
signal,
Transition,
useCallback,
useEffect,
useLayoutEffect,
useRef,
} from "kaioken"
import { signal, Transition, useEffect, useLayoutEffect, useRef } from "kaioken"
import { useDevTools } from "./hooks/useDevtools"
import { InspectComponent } from "./components/InspectComponent"
import { PageInfo } from "./icon/PageInfo"
Expand Down
16 changes: 12 additions & 4 deletions packages/lib/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@ function updateDom(vNode: VNode) {
(vNode.cleanups[key](), delete vNode.cleanups[key])
}
if (Signal.isSignal(nextProps[key])) {
const unsub = nextProps[key].subscribe((v) => {
const cb: ((v: any) => void) & { vNodeFunc?: boolean } = (v: any) => {
setProp(vNode, dom, key, v, unwrap(vNode.prev?.props[key]))
emitGranularSignalChange(nextProps[key])
})
}
if (__DEV__) {
cb.vNodeFunc = true
}
const unsub = nextProps[key].subscribe(cb)
;(vNode.cleanups ??= {})[key] = unsub
return setProp(
vNode,
Expand Down Expand Up @@ -214,10 +218,14 @@ function emitGranularSignalChange(signal: Signal<any>) {
}

function subTextNode(vNode: VNode, textNode: Text, signal: Signal<string>) {
const unsub = signal.subscribe((v) => {
const cb: ((v: any) => void) & { vNodeFunc?: boolean } = (v) => {
textNode.nodeValue = v
emitGranularSignalChange(signal)
})
}
if (__DEV__) {
cb.vNodeFunc = true
}
const unsub = signal.subscribe(cb)
;(vNode.cleanups ??= {})["nodeValue"] = unsub
}

Expand Down
Loading

0 comments on commit 5ff0f5c

Please sign in to comment.