Extract map of final values into change event
Cached on first use for performance and more DRY
This commit is contained in:
parent
d62136e180
commit
6542047b7c
1 changed files with 9 additions and 2 deletions
11
state.js
11
state.js
|
@ -1,8 +1,15 @@
|
||||||
export class ChangeEvent extends Event {
|
export class ChangeEvent extends Event {
|
||||||
|
#final
|
||||||
constructor(...changes) {
|
constructor(...changes) {
|
||||||
super('change')
|
super('change')
|
||||||
this.changes = changes
|
this.changes = changes
|
||||||
}
|
}
|
||||||
|
get final() {
|
||||||
|
if (!this.#final) {
|
||||||
|
this.#final = new Map(changes)
|
||||||
|
}
|
||||||
|
return this.#final
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MapStorage extends Storage {
|
export class MapStorage extends Storage {
|
||||||
|
@ -50,8 +57,8 @@ export class State extends EventTarget {
|
||||||
// Try running a "<name>Changed" method for every changed property
|
// Try running a "<name>Changed" method for every changed property
|
||||||
// Can be disabled to maybe squeeze out some performance
|
// Can be disabled to maybe squeeze out some performance
|
||||||
if (options.methods ?? true) {
|
if (options.methods ?? true) {
|
||||||
this.addEventListener("change", ({changes}) => {
|
this.addEventListener("change", ({final}) => {
|
||||||
new Map(changes).forEach((value, prop) => {
|
final.forEach((value, prop) => {
|
||||||
if (`${prop}Changed` in this) this[`${prop}Changed`](value)
|
if (`${prop}Changed` in this) this[`${prop}Changed`](value)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue