elexis/lib/$State.ts
defaultkavy 658df2d8e6 v0.0.2
new: $Image
enhance: $ parameter accept null and undefined
new: $Dialog
new: $Canvas
add: $.rem()
fix: Router resolve path error with param resolve
fix: $Container.insert() render error with undefined child
change: Route builder function parameter with one object
enhance: $Node event listener with event type
add: $Node.inDOM()
2024-02-13 19:38:46 +08:00

28 lines
867 B
TypeScript

import { $Node } from "./$Node";
export class $State<T> {
readonly value: T;
readonly attributes = new Map<$Node, Set<string | number | symbol>>();
constructor(value: T) {
this.value = value;
}
set(value: T) {
(this as Mutable<$State<T>>).value = value;
for (const [node, attrList] of this.attributes.entries()) {
for (const attr of attrList) {
//@ts-expect-error
if (node[attr] instanceof Function) node[attr](value)
}
}
}
toString(): string {
return `${this.value}`
}
use<T extends $Node, K extends keyof T>($node: T, attrName: K) {
const attrList = this.attributes.get($node)
if (attrList) attrList.add(attrName);
else this.attributes.set($node, new Set<string | number | symbol>().add(attrName))
}
};