Refactor state module
This commit is contained in:
parent
1c03da8815
commit
30f52a05e5
1 changed files with 14 additions and 12 deletions
26
state.js
26
state.js
|
@ -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 {
|
||||||
|
@ -338,7 +340,9 @@ class ComposedState extends SimpleState {
|
||||||
|
|
||||||
#microtaskQueued
|
#microtaskQueued
|
||||||
scheduleUpdate() {
|
scheduleUpdate() {
|
||||||
if (this.defer) {
|
if (this.synchronous) {
|
||||||
|
this.update()
|
||||||
|
} else {
|
||||||
if (!this.#microtaskQueued) {
|
if (!this.#microtaskQueued) {
|
||||||
queueMicrotask(() => {
|
queueMicrotask(() => {
|
||||||
this.#microtaskQueued = false
|
this.#microtaskQueued = false
|
||||||
|
@ -346,8 +350,6 @@ class ComposedState extends SimpleState {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
this.#microtaskQueued = true
|
this.#microtaskQueued = true
|
||||||
} else {
|
|
||||||
this.update()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -359,7 +361,7 @@ class ComposedState extends SimpleState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const compose = func => (...states) => new ComposedState(func, {defer: true}, ...states)
|
export const compose = func => (...states) => new ComposedState(func, {synchronous: false}, ...states)
|
||||||
|
|
||||||
const eventName = "mutation"
|
const eventName = "mutation"
|
||||||
|
|
||||||
|
@ -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]))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue