import { Effects, EnsureStorePath } from "../types" export class GetStore { constructor( readonly effects: Effects, readonly path: Path & EnsureStorePath, readonly options: { /** Defaults to what ever the package currently in */ packageId?: string | undefined } = {}, ) {} /** * Returns the value of Store at the provided path. Restart the service if the value changes */ const() { return this.effects.store.get({ ...this.options, path: this.path as any, callback: this.effects.restart, }) } /** * Returns the value of Store at the provided path. Does nothing if the value changes */ once() { return this.effects.store.get({ ...this.options, path: this.path as any, 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.store.get({ ...this.options, path: this.path as any, callback: () => callback(), }) await waitForNext } } } export function getStore( effects: Effects, path: Path & EnsureStorePath, options: { /** Defaults to what ever the package currently in */ packageId?: string | undefined } = {}, ) { return new GetStore(effects, path as any, options) }