From 8c243adb8dfa879eb531fd6d88e1ae5c533a6b24 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Sat, 9 Aug 2025 20:37:55 +0200 Subject: [PATCH] Update example and fix bug with OO controllers --- example.html | 74 ++++++++++++++++++++++++++++---------- src/controller-registry.js | 32 ++++++++++++++--- 2 files changed, 84 insertions(+), 22 deletions(-) diff --git a/example.html b/example.html index 37e20a0..fd3b6d9 100644 --- a/example.html +++ b/example.html @@ -1,25 +1,63 @@ + + -

Hello, World!

+ + +
+
+ +
+
diff --git a/src/controller-registry.js b/src/controller-registry.js index 2c45fdf..8945dc3 100644 --- a/src/controller-registry.js +++ b/src/controller-registry.js @@ -22,18 +22,25 @@ export class ControllerList { this.#element.setAttribute(this.#attribute, [...set].join(" ")) } + /** @param {String} name */ contains(name) { return this.#set.has(name) } + /** @param {String} name */ add(name) { this.toggle(name, true) } + /** @param {String} name */ remove(name) { this.toggle(name, false) } + /** + * @param {String} name + * @param {String} replacement + */ replace(name, replacement) { const set = this.#set if (set.has(name)) { @@ -46,6 +53,10 @@ export class ControllerList { } } + /** + * @param {String} name + * @param {Boolean} force + */ toggle(name, force) { const set = this.#set if (force === true) { @@ -83,29 +94,42 @@ export class ControllerRegistry { /** @type {WeakMap>} */ #attached = new WeakMap() - /** @type {Map} */ + /** @type {Map>} */ #waiting = new Map() - /** @type {Mapvoid>} */ + /** @type {Mapvoid>} */ #defined = new Map() #attribute + /** @typedef {HTMLElement} Root */ + + /** + * @param {Root} root + * @param {String} attribute + */ constructor(root, attribute="controller") { this.#attribute = attribute this.#observer.observe(root, {subtree: true, childList: true, attributes: true, attributeFilter: [attribute], attributeOldValue: false}) this.upgrade(root) } + /** + * @param {Root} root + */ upgrade(root) { root.querySelectorAll(`[${this.#attribute}]`).forEach(element => this.#update(element)) } + /** + * @param {String} name + * @param {Function} callback + */ define(name, callback) { if (("function" == typeof callback) && callback.prototype) { callback = async (element, disconnected) => { - const {proxy, revoke} = Proxy.reevocable(element) - const controller = new callback(proxy) + const {proxy, revoke} = Proxy.revocable(element, {}) + const controller = new callback(proxy, disconnected) await disconnected revoke() if ("detach" in controller) controller.detach(element)