Add return values to listen and forget methods

This commit is contained in:
Talia 2022-03-17 09:49:25 +01:00
parent 07304c8cbd
commit 5e19b2d70f

View file

@ -9,7 +9,7 @@ Example:
*/ */
const registry = new Map() const registry = new Map()
export const listener = (target={}) => { const listener = (target={}) => {
const callbacks = new Map() const callbacks = new Map()
const methods = Object.create(null) const methods = Object.create(null)
methods.listen = function(name, fn, {once=false}={}) { methods.listen = function(name, fn, {once=false}={}) {
@ -19,13 +19,14 @@ export const listener = (target={}) => {
let set = callbacks.get(name) ?? new Set() let set = callbacks.get(name) ?? new Set()
callbacks.set(name, set) callbacks.set(name, set)
set.add(callback) set.add(callback)
return this
} }
methods.forget = function(name, callback) { methods.forget = function(name, callback) {
if (callback) { if (callback) {
const set = callbacks.get(name) const set = callbacks.get(name)
if (set) set.delete(callback) if (set) return set.delete(callback)
} else { } else {
callbacks.delete(name) return callbacks.delete(name)
} }
} }
let proxy = new Proxy(target, { let proxy = new Proxy(target, {