From cc057ea222624ee1206eb60d4e9c93c557b50872 Mon Sep 17 00:00:00 2001 From: BluJ Date: Tue, 9 May 2023 16:08:26 -0600 Subject: [PATCH] chore: Add in the vault --- lib/util/getVault.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/util/getVault.ts diff --git a/lib/util/getVault.ts b/lib/util/getVault.ts new file mode 100644 index 0000000..b88a08d --- /dev/null +++ b/lib/util/getVault.ts @@ -0,0 +1,44 @@ +import { Effects, EnsureStorePath } from "../types" + +export class GetVault { + constructor(readonly effects: Effects, readonly key: keyof Vault & string) {} + + /** + * Returns the value of Store at the provided path. Restart the service if the value changes + */ + const() { + return this.effects.vault.get({ + key: this.key, + callback: this.effects.restart, + }) + } + /** + * Returns the value of Store at the provided path. Does nothing if the value changes + */ + once() { + return this.effects.vault.get({ + key: this.key, + callback: () => {}, + }) + } + + /** + * Watches the value of Store at the provided path. Takes a custom callback function to run whenever the value changes + */ + async *watch() { + while (true) { + let callback: () => void + const waitForNext = new Promise((resolve) => { + callback = resolve + }) + yield await this.effects.vault.get({ + key: this.key, + callback: () => callback(), + }) + await waitForNext + } + } +} +export function getVault(effects: Effects, key: keyof Vault & string) { + return new GetVault(effects, key) +}