Minor refactor & documentation

This commit is contained in:
Talia 2024-06-24 11:29:05 +02:00
parent 74de53874b
commit 8de5303550

View file

@ -361,7 +361,11 @@ export class ObservableElement extends Observable {
#value #value
#changedValue = false #changedValue = false
constructor(target, {get, equal, ...options}={}) { /**
* @param {HTMLElement} target
*/
constructor(target, {get=undefined, equal=undefined, ...options}={}) {
// @ts-ignore
super(options) super(options)
this[target] = target this[target] = target
@ -385,19 +389,19 @@ export class ObservableElement extends Observable {
get value() { return this.#value } get value() { return this.#value }
update() { update() {
const current = this.#getValue(this[target]) const current = this.#getValue(this.target)
if (this.#equal(this.#value, current)) return if (this.#equal(this.#value, current)) return
this.#value = current this.#value = current
if (this.synchronous) { if (this.synchronous) {
this.dispatchEvent(new MultiChangeEvent(["value", current])) this.dispatchEvent(new ValueChangeEvent(["value", current]))
} else { } else {
if (!this.#changedValue) { if (!this.#changedValue) {
queueMicrotask(() => { queueMicrotask(() => {
this.#changedValue = false this.#changedValue = false
this.dispatchEvent(new MultiChangeEvent(["value", this.#changedValue])) this.dispatchEvent(new ValueChangeEvent(["value", this.#changedValue]))
}) })
this.#changedValue = current this.#changedValue = current
} }
@ -407,18 +411,31 @@ export class ObservableElement extends Observable {
export class MapStorage extends Storage { export class MapStorage extends Storage {
#map = new Map() #map = new Map()
/**
* @param {number} index
* @return {string}
*/
key(index) { key(index) {
return [...this.#map.keys()][index] return [...this.#map.keys()][index]
} }
/**
* @param {string} keyName
* @return {any}
*/
getItem(keyName) { getItem(keyName) {
if (this.#map.has(keyName)) if (this.#map.has(keyName))
return this.#map.get(keyName) return this.#map.get(keyName)
else else
return null return null
} }
/**
* @param {string} keyName
* @param {any} keyValue
*/
setItem(keyName, keyValue) { setItem(keyName, keyValue) {
this.#map.set(keyName, String(keyValue)) this.#map.set(keyName, String(keyValue))
} }
/** @param {string} keyName */
removeItem(keyName) { removeItem(keyName) {
this.#map.delete(keyName) this.#map.delete(keyName)
} }