mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 04:11:57 +00:00
chore: Wrapper Data Contract
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
import { Parser } from "ts-matches"
|
||||
import { Effects, EnsureWrapperDataPath, ExtractWrapperData } from "../types"
|
||||
import { NoAny } from "."
|
||||
|
||||
export class GetWrapperData<WrapperData, Path extends string> {
|
||||
constructor(
|
||||
readonly effects: Effects,
|
||||
readonly path: Path & EnsureWrapperDataPath<WrapperData, Path>,
|
||||
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<WrapperData, Path>({
|
||||
...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<WrapperData, Path>({
|
||||
...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<void>((resolve) => {
|
||||
callback = resolve
|
||||
})
|
||||
yield await this.effects.getWrapperData<WrapperData, Path>({
|
||||
...this.options,
|
||||
path: this.path as any,
|
||||
callback: () => callback(),
|
||||
})
|
||||
await waitForNext
|
||||
}
|
||||
}
|
||||
}
|
||||
export function getWrapperData<WrapperData, Path extends string>(
|
||||
effects: Effects,
|
||||
path: Path & EnsureWrapperDataPath<WrapperData, Path>,
|
||||
options: {
|
||||
/** Defaults to what ever the package currently in */
|
||||
packageId?: string | undefined
|
||||
} = {},
|
||||
) {
|
||||
return new GetWrapperData<WrapperData, Path>(effects, path as any, options)
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { Parser, string } from "ts-matches"
|
||||
import * as T from "../types"
|
||||
import FileHelper from "./fileHelper"
|
||||
import nullIfEmpty from "./nullIfEmpty"
|
||||
import { GetWrapperData, getWrapperData } from "./getWrapperData"
|
||||
import { GetWrapperData, getWrapperData } from "../wrapperData/getWrapperData"
|
||||
import {
|
||||
CheckResult,
|
||||
checkPortListening,
|
||||
@@ -13,7 +13,7 @@ import { GetSystemSmtp } from "./GetSystemSmtp"
|
||||
|
||||
import "./nullIfEmpty"
|
||||
import "./fileHelper"
|
||||
import "./getWrapperData"
|
||||
import "../wrapperData/getWrapperData"
|
||||
import "./deepEqual"
|
||||
import "./deepMerge"
|
||||
import "./once"
|
||||
@@ -23,6 +23,7 @@ import { NetworkBuilder } from "../mainFn/NetworkBuilder"
|
||||
import { TorHostname } from "../mainFn/TorHostname"
|
||||
import { DefaultString } from "../config/configTypes"
|
||||
import { getDefaultString } from "./getDefaultString"
|
||||
import { WrapperDataContract } from "../wrapperData/wrapperDataContract"
|
||||
|
||||
// prettier-ignore
|
||||
export type FlattenIntersection<T> =
|
||||
@@ -37,12 +38,6 @@ export const isKnownError = (e: unknown): e is T.KnownError =>
|
||||
|
||||
declare const affine: unique symbol
|
||||
|
||||
export type WrapperDataOptionals<WrapperData, Path extends string> = {
|
||||
validator?: Parser<unknown, ExtractWrapperData<WrapperData, Path>>
|
||||
/** Defaults to what ever the package currently in */
|
||||
packageId?: string | undefined
|
||||
}
|
||||
|
||||
export type Utils<WD, WrapperOverWrite = { const: never }> = {
|
||||
createOrUpdateVault: (opts: {
|
||||
key: string
|
||||
@@ -88,9 +83,10 @@ export type Utils<WD, WrapperOverWrite = { const: never }> = {
|
||||
torHostName: (id: string) => TorHostname
|
||||
nullIfEmpty: typeof nullIfEmpty
|
||||
}
|
||||
export const utils = <WrapperData = never, WrapperOverWrite = { const: never }>(
|
||||
export const utils = <WD = never, WrapperOverWrite = { const: never }>(
|
||||
_wrapperDataContract: WrapperDataContract<WD>,
|
||||
effects: T.Effects,
|
||||
): Utils<WrapperData, WrapperOverWrite> => ({
|
||||
): Utils<WD, WrapperOverWrite> => ({
|
||||
createOrUpdateVault: async ({
|
||||
key,
|
||||
value,
|
||||
@@ -125,12 +121,12 @@ export const utils = <WrapperData = never, WrapperOverWrite = { const: never }>(
|
||||
packageId,
|
||||
}) as any,
|
||||
getOwnWrapperData: <Path extends string>(
|
||||
path: T.EnsureWrapperDataPath<WrapperData, Path>,
|
||||
) => getWrapperData<WrapperData, Path>(effects, path as any) as any,
|
||||
path: T.EnsureWrapperDataPath<WD, Path>,
|
||||
) => getWrapperData<WD, Path>(effects, path as any) as any,
|
||||
setOwnWrapperData: <Path extends string | never>(
|
||||
path: T.EnsureWrapperDataPath<WrapperData, Path>,
|
||||
value: ExtractWrapperData<WrapperData, Path>,
|
||||
) => effects.setWrapperData<WrapperData, Path>({ value, path: path as any }),
|
||||
path: T.EnsureWrapperDataPath<WD, Path>,
|
||||
value: ExtractWrapperData<WD, Path>,
|
||||
) => effects.setWrapperData<WD, Path>({ value, path: path as any }),
|
||||
checkPortListening: checkPortListening.bind(null, effects),
|
||||
checkWebUrl: checkWebUrl.bind(null, effects),
|
||||
bindLan: async (port: number) => LocalPort.bindLan(effects, port),
|
||||
@@ -138,6 +134,12 @@ export const utils = <WrapperData = never, WrapperOverWrite = { const: never }>(
|
||||
torHostName: (id: string) => TorHostname.of(effects, id),
|
||||
})
|
||||
|
||||
export const createUtils = utils
|
||||
export const createMainUtils = <WD>(
|
||||
wrapperDataContract: WrapperDataContract<WD>,
|
||||
effects: T.Effects,
|
||||
) => createUtils<WD, {}>(wrapperDataContract, effects)
|
||||
|
||||
type NeverPossible = { [affine]: string }
|
||||
export type NoAny<A> = NeverPossible extends A
|
||||
? keyof NeverPossible extends keyof A
|
||||
|
||||
Reference in New Issue
Block a user