From ebf679b7ed56a93a6b2aa88d028e5c648da2afbe Mon Sep 17 00:00:00 2001 From: BluJ Date: Tue, 2 May 2023 15:58:28 -0600 Subject: [PATCH] feat: get system smtp in utils --- lib/util/GetSystemSmtp.ts | 40 +++++++++++++++++++++++++++++++++++++++ lib/util/index.ts | 4 ++++ 2 files changed, 44 insertions(+) create mode 100644 lib/util/GetSystemSmtp.ts diff --git a/lib/util/GetSystemSmtp.ts b/lib/util/GetSystemSmtp.ts new file mode 100644 index 0000000..cf5b57b --- /dev/null +++ b/lib/util/GetSystemSmtp.ts @@ -0,0 +1,40 @@ +import { Parser } from "ts-matches" +import { Effects, EnsureWrapperDataPath, ExtractWrapperData } from "../types" +import { NoAny } from "." + +export class GetSystemSmtp { + constructor(readonly effects: Effects) {} + + /** 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.getSystemSmtp({ + 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.getSystemSmtp({ + 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.getSystemSmtp({ + callback: () => callback(), + }) + await waitForNext + } + } +} diff --git a/lib/util/index.ts b/lib/util/index.ts index 9ffe355..b3713ce 100644 --- a/lib/util/index.ts +++ b/lib/util/index.ts @@ -10,6 +10,7 @@ import { } from "../health/checkFns" import { LocalBinding, LocalPort, NetworkBuilder, TorHostname } from "../mainFn" import { ExtractWrapperData } from "../types" +import { GetSystemSmtp } from "./GetSystemSmtp" export { default as nullIfEmpty } from "./nullIfEmpty" export { FileHelper } from "./fileHelper" @@ -59,6 +60,7 @@ export type Utils = { fileHelper: FileHelper, data: A, ) => ReturnType["write"]> + getSystemSmtp: () => GetSystemSmtp & WrapperOverWrite getWrapperData: ( packageId: string, path: T.EnsureWrapperDataPath, @@ -96,6 +98,8 @@ export type Utils = { export const utils = ( effects: T.Effects, ): Utils => ({ + getSystemSmtp: () => + new GetSystemSmtp(effects) as GetSystemSmtp & WrapperOverWrite, readFile: (fileHelper: FileHelper) => fileHelper.read(effects), writeFile: (fileHelper: FileHelper, data: A) => fileHelper.write(data, effects),