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 } = {}, ) {} /** This should be used as the primary method in main since it allows the main to * restart if the wrapper data changes */ const() { return this.effects.getWrapperData({ ...this.options, path: this.path as any, callback: this.effects.restart, }) } /** * Returns the wrapper data once and then never again * Doesn't restart the server when the wrapper data changes */ once() { return this.effects.getWrapperData({ ...this.options, path: this.path as any, callback: () => {}, }) } /** * Keeps giving the latest wrapper data as it 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) }