From e03b86f00707bb4d50fb78882c6bb24f32d1def0 Mon Sep 17 00:00:00 2001 From: Emma Date: Fri, 18 Oct 2024 17:46:11 -0600 Subject: [PATCH] creates window event when store is updated --- store.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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); } }