diff --git a/store.ts b/store.ts index da824d8..6a2e402 100644 --- a/store.ts +++ b/store.ts @@ -1,11 +1,18 @@ +const REFRESH_EVENT = "bm:refresh-store"; + export class BearMetalStore { private store: Record = {}; 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); } }