From 6542047b7c116539bdedb17345aae31a52f7d56c Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Wed, 20 Sep 2023 13:26:14 +0200 Subject: [PATCH] Extract map of final values into change event Cached on first use for performance and more DRY --- state.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/state.js b/state.js index 8642007..979fc6c 100644 --- a/state.js +++ b/state.js @@ -1,8 +1,15 @@ export class ChangeEvent extends Event { + #final constructor(...changes) { super('change') this.changes = changes } + get final() { + if (!this.#final) { + this.#final = new Map(changes) + } + return this.#final + } } export class MapStorage extends Storage { @@ -50,8 +57,8 @@ export class State extends EventTarget { // Try running a "Changed" method for every changed property // Can be disabled to maybe squeeze out some performance if (options.methods ?? true) { - this.addEventListener("change", ({changes}) => { - new Map(changes).forEach((value, prop) => { + this.addEventListener("change", ({final}) => { + final.forEach((value, prop) => { if (`${prop}Changed` in this) this[`${prop}Changed`](value) }) })