refactor(stage.ts): rename class 'watchable' to 'Watchable' for consistency and clarity

feat(utils.ts): add Watchable class to provide a generic watchable object with getter, setter, and watcher functionality
This commit is contained in:
Sped0n
2023-10-29 12:31:48 +08:00
parent d7cabdb894
commit bb9870176f
2 changed files with 32 additions and 23 deletions

View File

@@ -13,3 +13,21 @@ export function expand(num: number): string {
export function isMobile(): boolean {
return window.matchMedia('(hover: none)').matches
}
export class Watchable<T> {
constructor(private obj: T) {}
private watchers: (() => void)[] = []
get(): T {
return this.obj
}
set(e: T): void {
this.obj = e
this.watchers.forEach((watcher) => watcher())
}
addWatcher(watcher: () => void): void {
this.watchers.push(watcher)
}
}