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