Add $$ proxies
This commit is contained in:
61
sigpro2.js
61
sigpro2.js
@@ -10,6 +10,8 @@ let activeOwner = null
|
|||||||
let isFlushing = false
|
let isFlushing = false
|
||||||
let batchDepth = 0
|
let batchDepth = 0
|
||||||
const effectQueue = new Set()
|
const effectQueue = new Set()
|
||||||
|
const proxyCache = new WeakMap()
|
||||||
|
const ITER = Symbol('iter')
|
||||||
const MOUNTED_NODES = new WeakMap()
|
const MOUNTED_NODES = new WeakMap()
|
||||||
|
|
||||||
const dispose = eff => {
|
const dispose = eff => {
|
||||||
@@ -41,15 +43,6 @@ const onUnmount = fn => {
|
|||||||
if (activeOwner) (activeOwner._cleanups ||= new Set()).add(fn)
|
if (activeOwner) (activeOwner._cleanups ||= new Set()).add(fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
const set = (signal, path, value) => {
|
|
||||||
if (value === undefined) return signal(isFunc(path) ? path(signal()) : path)
|
|
||||||
const keys = path.split('.'), root = { ...signal() }
|
|
||||||
let acc = root, k
|
|
||||||
for (k of keys.slice(0, -1)) acc = acc[k] = { ...(acc[k] || {}) }
|
|
||||||
acc[keys.at(-1)] = value
|
|
||||||
signal(root)
|
|
||||||
}
|
|
||||||
|
|
||||||
const untrack = fn => {
|
const untrack = fn => {
|
||||||
const p = activeEffect
|
const p = activeEffect
|
||||||
activeEffect = null
|
activeEffect = null
|
||||||
@@ -95,7 +88,7 @@ const flush = () => {
|
|||||||
isFlushing = false
|
isFlushing = false
|
||||||
}
|
}
|
||||||
|
|
||||||
const batch = fn => {
|
const Batch = fn => {
|
||||||
batchDepth++
|
batchDepth++
|
||||||
try {
|
try {
|
||||||
return fn()
|
return fn()
|
||||||
@@ -179,6 +172,50 @@ const $ = (val, key = null) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const $$ = (target) => {
|
||||||
|
if (!isObj(target)) return target
|
||||||
|
|
||||||
|
if (proxyCache.has(target)) return proxyCache.get(target)
|
||||||
|
|
||||||
|
const subsMap = new Map()
|
||||||
|
const getSubs = (k) => {
|
||||||
|
let s = subsMap.get(k)
|
||||||
|
if (!s) subsMap.set(k, s = new Set())
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
const proxy = new Proxy(target, {
|
||||||
|
get(t, k) {
|
||||||
|
trackUpdate(getSubs(k))
|
||||||
|
return $$(t[k])
|
||||||
|
},
|
||||||
|
set(t, k, v) {
|
||||||
|
const isNew = !(k in t)
|
||||||
|
if (!Object.is(t[k], v)) {
|
||||||
|
t[k] = v
|
||||||
|
trackUpdate(getSubs(k), true)
|
||||||
|
if (isNew) trackUpdate(getSubs(ITER), true)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
deleteProperty(t, k) {
|
||||||
|
const res = Reflect.deleteProperty(t, k)
|
||||||
|
if (res) {
|
||||||
|
trackUpdate(getSubs(k), true)
|
||||||
|
trackUpdate(getSubs(ITER), true)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
},
|
||||||
|
ownKeys(t) {
|
||||||
|
trackUpdate(getSubs(ITER))
|
||||||
|
return Reflect.ownKeys(t)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
proxyCache.set(target, proxy)
|
||||||
|
return proxy
|
||||||
|
}
|
||||||
|
|
||||||
const Watch = (sources, cb) => {
|
const Watch = (sources, cb) => {
|
||||||
if (cb === undefined) {
|
if (cb === undefined) {
|
||||||
const effect = createEffect(sources)
|
const effect = createEffect(sources)
|
||||||
@@ -469,7 +506,7 @@ const Mount = (comp, target) => {
|
|||||||
return inst
|
return inst
|
||||||
}
|
}
|
||||||
|
|
||||||
const SigPro = Object.freeze({ $, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, batch, set })
|
const SigPro = Object.freeze({ $, $$, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, Batch })
|
||||||
|
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
Object.assign(window, SigPro)
|
Object.assign(window, SigPro)
|
||||||
@@ -477,5 +514,5 @@ if (typeof window !== "undefined") {
|
|||||||
.split(" ").forEach(t => window[t[0].toUpperCase() + t.slice(1)] = (p, c) => SigPro.Tag(t, p, c))
|
.split(" ").forEach(t => window[t[0].toUpperCase() + t.slice(1)] = (p, c) => SigPro.Tag(t, p, c))
|
||||||
}
|
}
|
||||||
|
|
||||||
export { $, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, batch, set }
|
export { $, $$, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, Batch }
|
||||||
export default SigPro
|
export default SigPro
|
||||||
Reference in New Issue
Block a user