creates window event when store is updated
This commit is contained in:
parent
d43f5e71db
commit
e03b86f007
13
store.ts
13
store.ts
@ -1,11 +1,18 @@
|
|||||||
|
const REFRESH_EVENT = "bm:refresh-store";
|
||||||
|
|
||||||
export class BearMetalStore {
|
export class BearMetalStore {
|
||||||
private store: Record<string, string | number | boolean> = {};
|
private store: Record<string, string | number | boolean> = {};
|
||||||
private storePath: string;
|
private storePath: string;
|
||||||
|
|
||||||
|
public readonly EVENT_NAME: string;
|
||||||
|
|
||||||
constructor(storePath?: string) {
|
constructor(storePath?: string) {
|
||||||
this.storePath = storePath || Deno.env.get("BEAR_METAL_STORE_PATH") ||
|
this.storePath = storePath || Deno.env.get("BEAR_METAL_STORE_PATH") ||
|
||||||
"./BearMetal/store.json";
|
"./BearMetal/store.json";
|
||||||
|
this.EVENT_NAME = REFRESH_EVENT + this.storePath;
|
||||||
this.readIn();
|
this.readIn();
|
||||||
|
|
||||||
|
globalThis.addEventListener(this.EVENT_NAME, this.readIn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public get(key: string): string | number | boolean {
|
public get(key: string): string | number | boolean {
|
||||||
@ -28,5 +35,11 @@ export class BearMetalStore {
|
|||||||
|
|
||||||
private writeOut() {
|
private writeOut() {
|
||||||
Deno.writeTextFileSync(this.storePath, JSON.stringify(this.store));
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user