creates window event when store is updated

This commit is contained in:
Emmaline Autumn 2024-10-18 17:46:11 -06:00
parent d43f5e71db
commit e03b86f007

View File

@ -1,11 +1,18 @@
const REFRESH_EVENT = "bm:refresh-store";
export class BearMetalStore {
private store: Record<string, string | number | boolean> = {};
private storePath: string;
public readonly EVENT_NAME: string;
constructor(storePath?: string) {
this.storePath = storePath || Deno.env.get("BEAR_METAL_STORE_PATH") ||
"./BearMetal/store.json";
this.EVENT_NAME = REFRESH_EVENT + this.storePath;
this.readIn();
globalThis.addEventListener(this.EVENT_NAME, this.readIn);
}
public get(key: string): string | number | boolean {
@ -28,5 +35,11 @@ export class BearMetalStore {
private writeOut() {
Deno.writeTextFileSync(this.storePath, JSON.stringify(this.store));
globalThis.dispatchEvent(new CustomEvent(this.EVENT_NAME));
}
[Symbol.dispose]() {
this.writeOut();
globalThis.removeEventListener(this.EVENT_NAME, this.readIn);
}
}