Refactor state module

This commit is contained in:
Talia 2024-01-24 16:11:22 +01:00
parent 97a89627b9
commit 8cca0813d9

View file

@ -74,6 +74,9 @@ export class SimpleState extends EventTarget {
return () => controller.abort() return () => controller.abort()
} }
get() { return this.value }
set(value) { this.value = value }
emit(property, from, to, options={}) { emit(property, from, to, options={}) {
const change = {property, from, to, ...options} const change = {property, from, to, ...options}
if (!this.synchronous) { if (!this.synchronous) {
@ -146,9 +149,6 @@ export class State extends SimpleState {
}) })
} }
set value(value) { this.values.value = value }
get value() { return this.values.value }
forward(property="value", methods) { forward(property="value", methods) {
return new ForwardState(this, property, methods) return new ForwardState(this, property, methods)
} }
@ -158,10 +158,12 @@ export class State extends SimpleState {
this.#target[prop] = value this.#target[prop] = value
} }
get(prop) { get(prop="value") {
if (arguments.length === 0) return this.get("value")
return this.#target[prop] return this.#target[prop]
} }
set value(value) { this.set(value) }
get value() { return this.get() }
} }
export class ForwardState extends SimpleState { export class ForwardState extends SimpleState {
@ -412,7 +414,9 @@ export class DOMState extends SimpleState {
this.#old = current this.#old = current
if (this.defer) { if (this.synchronous) {
this.dispatchEvent(new ChangeEvent(["value", current]))
} else {
if (!this.#changedValue) { if (!this.#changedValue) {
queueMicrotask(() => { queueMicrotask(() => {
this.#changedValue = false this.#changedValue = false
@ -420,8 +424,6 @@ export class DOMState extends SimpleState {
}) })
this.#changedValue = current this.#changedValue = current
} }
} else {
this.dispatchEvent(new ChangeEvent(["value", current]))
} }
} }
} }