Fix and refactor skooma.js

This commit is contained in:
Talia 2024-01-24 16:13:01 +01:00
parent 5488f2a49a
commit 6248593570

View file

@ -54,17 +54,15 @@ const toChild = arg => {
return arg return arg
else if (isReactive(arg)) else if (isReactive(arg))
return reactiveChild(arg) return reactiveChild(arg)
else
return document.createComment("Placeholder for reactive content")
} }
const reactiveChild = reactive => { const reactiveChild = reactive => {
let ref let ref
const abort = reactive.subscribe(value => { const abort = reactive.subscribe(value => {
if (ref && !ref.deref()) return abort() if (ref && !ref.deref()) return abort()
const child = toChild(value) const child = toChild(value) ?? document.createComment("Placeholder for reactive content")
if (ref) ref.deref().replaceWith(child)
untilDeathDoThemPart(child, reactive) untilDeathDoThemPart(child, reactive)
if (ref) ref.deref().replaceWith(child)
ref = new WeakRef(child) ref = new WeakRef(child)
}) })
return ref.deref() return ref.deref()
@ -102,9 +100,8 @@ const setAttribute = (element, attribute, value, cleanupSignal) => {
setReactiveAttribute(element, attribute, value) setReactiveAttribute(element, attribute, value)
else if (typeof value === "function") else if (typeof value === "function")
element.addEventListener(attribute, value, {signal: cleanupSignal}) element.addEventListener(attribute, value, {signal: cleanupSignal})
else if (special) { else if (special?.set)
special.set.call(element, value) special.set.call(element, value)
}
else if (value === true) else if (value === true)
{if (!element.hasAttribute(attribute)) element.setAttribute(attribute, '')} {if (!element.hasAttribute(attribute)) element.setAttribute(attribute, '')}
else if (value === false) else if (value === false)
@ -123,10 +120,11 @@ const setReactiveAttribute = (element, attribute, reactive) => {
multiAbort.abort() multiAbort.abort()
setAttribute(element, attribute, value, multiAbort.signal) setAttribute(element, attribute, value, multiAbort.signal)
}) })
const special = specialAttributes[attribute]
if (special?.hook && reactive.set) { if (special?.hook && reactive.set) {
special.hook.call(element, () => { special.hook.call(element, () => {
const value = special.get.call(element, attribute) const value = special.get.call(element, attribute)
if (value != old) reactive.set() = value if (value != old) reactive.set(value)
}) })
} }
} }