Rewrite listener

This commit is contained in:
Talia 2022-03-16 16:45:48 +01:00
parent 357af9342d
commit 07304c8cbd

View file

@ -9,28 +9,33 @@ Example:
*/ */
const registry = new Map() const registry = new Map()
export const listener = (target={}) => { export const listener = (target={}) => {
let callbacks = new Map() const callbacks = new Map()
function listen(prop, callback) { const methods = Object.create(null)
if ("object" == typeof prop && "forEach" in prop) { methods.listen = function(name, fn, {once=false}={}) {
prop.forEach(prop => this.listen(prop, callback)) const callback = once
} else if (callback) { ? (...args) => { this.forget(name, callback); return fn(...args) }
let list = callbacks.get(prop) : fn
if (!list) callbacks.set(prop, list=[]) let set = callbacks.get(name) ?? new Set()
list.push(callback) callbacks.set(name, set)
set.add(callback)
}
methods.forget = function(name, callback) {
if (callback) {
const set = callbacks.get(name)
if (set) set.delete(callback)
} else { } else {
callbacks.delete(prop) callbacks.delete(name)
} }
} }
let proxy = new Proxy(target, { let proxy = new Proxy(target, {
set: (target, prop, value) => { set: (target, prop, value) => {
if (callbacks.has("*")) callbacks.get("*").forEach(callback => callback(value, prop, target[prop])) if (callbacks.has(null)) callbacks.get(null).forEach(callback => callback(value, target[prop], prop))
if (callbacks.has(prop)) callbacks.get(prop).forEach(callback => callback(value, prop, target[prop])) if (callbacks.has(prop)) callbacks.get(prop).forEach(callback => callback(value, target[prop], prop))
return Reflect.set(target, prop, value) return Reflect.set(target, prop, value)
}, },
get: (target, prop, value) => prop=="listen" get: (target, prop, value) => prop in methods
? listen ? methods[prop]
: target[prop] : target[prop]
}) })
registry.set(proxy, target) registry.set(proxy, target)
@ -39,16 +44,4 @@ export const listener = (target={}) => {
listener.raw = proxy => registry.get(proxy) listener.raw = proxy => registry.get(proxy)
export const text = (listener, prop) => {
if (prop) {
const node = document.createTextNode(listener[prop])
listener.listen(prop, data => node.data = data)
return node
} else {
return new Proxy(listener, {
get: (target, prop, receiver) => text(listener, prop)
})
}
}
export default listener export default listener