chore: Add the wrapperData, bound effects, and sdk

This commit is contained in:
BluJ
2023-04-13 14:44:17 -06:00
parent cff86718f8
commit c9a1c45930
11 changed files with 134 additions and 25 deletions

View File

@@ -0,0 +1,45 @@
import { Parser } from "ts-matches";
import { Effects } from "../types";
export function getWrapperData<A>(
effects: Effects,
validator: Parser<unknown, A>,
options: {
/** Defaults to what ever the package currently in */
packageId?: string | undefined;
/** JsonPath */
path?: string | undefined;
} = {}
) {
return {
const: () =>
effects
.getWrapperData({
...options,
callback: effects.restart,
})
.then(validator.unsafeCast),
first: () =>
effects
.getWrapperData({
...options,
callback: () => {},
})
.then(validator.unsafeCast),
overTime: async function* <A>() {
while (true) {
let callback: () => void;
const waitForNext = new Promise<void>((resolve) => {
callback = resolve;
});
yield await effects
.getWrapperData({
...options,
callback: () => callback(),
})
.then(validator.unsafeCast);
await waitForNext;
}
},
};
}

View File

@@ -1,8 +1,15 @@
import { Parser, string } from "ts-matches";
import * as T from "../types";
import FileHelper from "./fileHelper";
import nullIfEmpty from "./nullIfEmpty";
import { getWrapperData } from "./getWrapperData";
import { checkPortListening, checkWebUrl } from "../health/checkFns";
import { LocalPort, NetworkBuilder, TorHostname } from "../mainFn";
export { guardAll, typeFromProps } from "./propertiesMatcher";
export { default as nullIfEmpty } from "./nullIfEmpty";
export { FileHelper } from "./fileHelper";
export { getWrapperData } from "./getWrapperData";
/** Used to check if the file exists before hand */
export const exists = (
@@ -16,3 +23,31 @@ export const exists = (
export const isKnownError = (e: unknown): e is T.KnownError =>
e instanceof Object && ("error" in e || "error-code" in e);
type Cdr<A> = A extends [unknown, ...infer Cdr] ? Cdr : [];
export const utils = (effects: T.Effects) => ({
readFile: <A>(fileHelper: FileHelper<A>) => fileHelper.read(effects),
writeFile: <A>(fileHelper: FileHelper<A>, data: A) =>
fileHelper.write(data, effects),
exists: (props: { path: string; volumeId: string }) => exists(effects, props),
nullIfEmpty,
getWrapperData: <A>(
validator: Parser<unknown, A>,
options: {
/** Defaults to what ever the package currently in */
packageId?: string | undefined;
/** JsonPath */
path?: string | undefined;
} = {}
) => getWrapperData(effects, validator, options),
setWrapperData: <A>(
value: A,
options: { packageId?: string | undefined; path?: string | undefined } = {}
) => effects.setWrapperData({ ...options, value }),
checkPortListening: checkPortListening.bind(null, effects),
checkWebUrl: checkWebUrl.bind(null, effects),
localPort: LocalPort.bind(null, effects),
networkBuilder: NetworkBuilder.of.bind(null, effects),
torHostName: TorHostname.of.bind(null, effects),
});