From d457c386e0b509ce50d7b22e7a667471daf52ec0 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Thu, 20 Feb 2025 23:01:12 +0100 Subject: [PATCH] Fix double dispatch bug in observable compositions --- observable.js | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/observable.js b/observable.js index cd73e02..f77e610 100644 --- a/observable.js +++ b/observable.js @@ -134,6 +134,7 @@ export class Observable extends EventTarget { enqueue(property, from, to, mutation=false) { const change = {property, from, to, mutation} if (!this.dispatchEvent(new ChangeEvent(change))) return false + if (!this.synchronous) { if (!this.#queue) { this.#queue = [] @@ -260,9 +261,14 @@ export class ObservableValue extends Observable { } } - /** @param {Change[]} changes */ - emit(...changes) { - this.dispatchEvent(new ChangedEvent(...changes)) + /** + * @param {(value: any) => any} func + */ + compose(func) { + return new Composition(func, {}, this) + } + + proxy(methods) { } } @@ -365,34 +371,17 @@ class Composition extends ObservableValue { const ref = new WeakRef(this) obesrvables.forEach(state => { - state.addEventListener("change", () => { - ref.deref()?.scheduleUpdate() + state.addEventListener("changed", () => { + ref.deref()?.update() }, {signal: abortController.signal}) }) this.update() } - #microtaskQueued = false - scheduleUpdate() { - if (this.synchronous) { - this.update() - } else { - if (!this.#microtaskQueued) { - queueMicrotask(() => { - this.#microtaskQueued = false - this.update() - }) - this.#microtaskQueued = true - } - } - } - update() { const value = this.#func(...this.#states.map(state => state.value)) - const change = {property: "value", from: this.value, to: value} this.value = value - this.emit(change) } }