chore: Update types

This commit is contained in:
BluJ
2023-04-25 10:32:02 -06:00
parent c355a9d2d9
commit 5e1d77b95d
12 changed files with 106 additions and 95 deletions

View File

@@ -8,13 +8,9 @@ export function setupActions(...createdActions: CreatedAction<any, any>[]) {
actions[action.metaData.id] = action.exportedAction;
}
const initializeActions = async (effects: Effects) => {
for (const action of createdActions) {
action.exportAction(effects);
}
};
const actionsMetadata = createdActions.map((x) => x.metaData);
return {
actions,
initializeActions,
actionsMetadata,
};
}

View File

@@ -1,18 +1,18 @@
import { AutoConfigure, Effects, ExpectedExports } from "../types";
import { AutoConfigure, DeepPartial, Effects, ExpectedExports } from "../types";
import { Utils, deepEqual, deepMerge, utils } from "../util";
export type AutoConfigFrom<WD, Input> = {
[key: string]: (options: {
export type AutoConfigFrom<WD, Input, NestedConfigs> = {
[key in keyof NestedConfigs & string]: (options: {
effects: Effects;
localConfig: Input;
remoteConfig: unknown;
remoteConfig: NestedConfigs[key];
utils: Utils<WD>;
}) => Promise<void | Record<string, unknown>>;
}) => Promise<void | DeepPartial<NestedConfigs[key]>>;
};
export class AutoConfig<WD, Input> {
export class AutoConfig<WD, Input, NestedConfigs> {
constructor(
readonly configs: AutoConfigFrom<WD, Input>,
readonly path: keyof AutoConfigFrom<WD, Input>,
readonly configs: AutoConfigFrom<WD, Input, NestedConfigs>,
readonly path: keyof AutoConfigFrom<WD, Input, NestedConfigs>,
) {}
async check(
@@ -23,6 +23,7 @@ export class AutoConfig<WD, Input> {
...options,
utils: utils<WD>(options.effects),
localConfig: options.localConfig as Input,
remoteConfig: options.remoteConfig as any,
};
if (
!deepEqual(
@@ -43,6 +44,7 @@ export class AutoConfig<WD, Input> {
...options,
utils: utils<WD>(options.effects),
localConfig: options.localConfig as Input,
remoteConfig: options.remoteConfig as any,
};
return deepMerge(
{},

View File

@@ -1,12 +1,24 @@
import { GenericManifest } from "../manifest/ManifestTypes";
import { AutoConfig, AutoConfigFrom } from "./AutoConfig";
export function setupAutoConfig<WD, Input>(configs: AutoConfigFrom<WD, Input>) {
export function setupAutoConfig<
WD,
Input,
Manifest extends GenericManifest,
NestedConfigs extends {
[key in keyof Manifest["dependencies"]]: unknown;
},
>(configs: AutoConfigFrom<WD, Input, NestedConfigs>) {
type C = typeof configs;
const answer = { ...configs } as unknown as {
[k in keyof C]: AutoConfig<WD, Input>;
[k in keyof C]: AutoConfig<WD, Input, NestedConfigs>;
};
for (const key in configs) {
answer[key] = new AutoConfig<WD, Input>(configs, key);
answer[key as keyof typeof configs] = new AutoConfig<
WD,
Input,
NestedConfigs
>(configs, key as keyof typeof configs);
}
return answer;
}

View File

@@ -1,55 +1,43 @@
// import { InterfaceReceipt } from "../mainFn/interfaceReceipt";
// import { Daemon, Effects } from "../types";
// import { CheckResult } from "./checkFns/CheckResult";
// import { ReadyReceipt } from "./ReadyProof";
// import { HealthReceipt } from "./HealthReceipt";
// import { Trigger } from "./trigger";
// import { TriggerInput } from "./trigger/TriggerInput";
// import { defaultTrigger } from "./trigger/defaultTrigger";
import { InterfaceReceipt } from "../mainFn/interfaceReceipt";
import { Daemon, Effects } from "../types";
import { CheckResult } from "./checkFns/CheckResult";
import { HealthReceipt } from "./HealthReceipt";
import { Trigger } from "./trigger";
import { TriggerInput } from "./trigger/TriggerInput";
import { defaultTrigger } from "./trigger/defaultTrigger";
// function readReciptOf<A extends { daemon: Daemon }>(a: A) {
// return a as A & ReadyReceipt;
// }
// export function readyCheck(o: {
// effects: Effects;
// started(onTerm: () => void): null;
// interfaceReceipt: InterfaceReceipt;
// healthReceipts: Iterable<HealthReceipt>;
// daemonReceipt: Daemon;
// name: string;
// trigger?: Trigger;
// fn(): Promise<CheckResult> | CheckResult;
// onFirstSuccess?: () => () => Promise<unknown> | unknown;
// }) {
// new Promise(async () => {
// const trigger = (o.trigger ?? defaultTrigger)();
// let currentValue: TriggerInput = {
// lastResult: null,
// hadSuccess: false,
// };
// for (
// let res = await trigger.next(currentValue);
// !res.done;
// res = await trigger.next(currentValue)
// ) {
// try {
// const { status, message } = await o.fn();
// if (!currentValue.hadSuccess) {
// await o.started(o?.onFirstSuccess ?? (() => o.daemonReceipt.term()));
// }
// await o.effects.setHealth({
// name: o.name,
// status,
// message,
// });
// currentValue.hadSuccess = true;
// currentValue.lastResult = "success";
// } catch (_) {
// currentValue.lastResult = "failure";
// }
// }
// });
// return readReciptOf({
// daemon: o.daemonReceipt,
// });
// }
export function healthCheck(o: {
effects: Effects;
name: string;
trigger?: Trigger;
fn(): Promise<CheckResult> | CheckResult;
onFirstSuccess?: () => () => Promise<unknown> | unknown;
}) {
new Promise(async () => {
let currentValue: TriggerInput = {
lastResult: null,
hadSuccess: false,
};
const getCurrentValue = () => currentValue;
const trigger = (o.trigger ?? defaultTrigger)(getCurrentValue);
for (
let res = await trigger.next();
!res.done;
res = await trigger.next()
) {
try {
const { status, message } = await o.fn();
await o.effects.setHealth({
name: o.name,
status,
message,
});
currentValue.hadSuccess = true;
currentValue.lastResult = "success";
} catch (_) {
currentValue.lastResult = "failure";
}
}
});
return {} as HealthReceipt;
}

View File

@@ -4,25 +4,28 @@ import { Trigger } from "./index";
export function changeOnFirstSuccess(o: {
beforeFirstSuccess: Trigger;
afterFirstSuccess: Trigger;
}) {
return async function* () {
const beforeFirstSuccess = o.beforeFirstSuccess();
let currentValue: TriggerInput = yield;
beforeFirstSuccess.next(currentValue);
}): Trigger {
return async function* (getInput) {
const beforeFirstSuccess = o.beforeFirstSuccess(getInput);
yield;
let currentValue = getInput();
beforeFirstSuccess.next();
for (
let res = await beforeFirstSuccess.next(currentValue);
let res = await beforeFirstSuccess.next();
currentValue?.lastResult !== "success" && !res.done;
res = await beforeFirstSuccess.next(currentValue)
res = await beforeFirstSuccess.next()
) {
currentValue = yield;
yield;
currentValue = getInput();
}
const afterFirstSuccess = o.afterFirstSuccess();
const afterFirstSuccess = o.afterFirstSuccess(getInput);
for (
let res = await afterFirstSuccess.next(currentValue);
let res = await afterFirstSuccess.next();
!res.done;
res = await afterFirstSuccess.next(currentValue)
res = await afterFirstSuccess.next()
) {
currentValue = yield;
yield;
currentValue = getInput();
}
};
}

View File

@@ -2,4 +2,6 @@ import { TriggerInput } from "./TriggerInput";
export { changeOnFirstSuccess } from "./changeOnFirstSuccess";
export { cooldownTrigger } from "./cooldownTrigger";
export type Trigger = () => AsyncIterator<unknown, unknown, TriggerInput>;
export type Trigger = (
getInput: () => TriggerInput,
) => AsyncIterator<unknown, unknown, never>;

View File

@@ -82,11 +82,15 @@ export class Daemons<Ids extends string | never> {
const { command } = daemon;
const child = effects.runDaemon(command);
const trigger = (daemon.ready.trigger ?? defaultTrigger)();
let currentInput = {};
const getCurrentInput = () => currentInput;
const trigger = (daemon.ready.trigger ?? defaultTrigger)(
getCurrentInput,
);
for (
let res = await trigger.next({});
let res = await trigger.next();
!res.done;
res = await trigger.next({})
res = await trigger.next()
) {
const response = await daemon.ready.fn();
if (response.status === "passing") {

View File

@@ -1,9 +1,15 @@
import { GenericManifest, ManifestVersion } from "./ManifestTypes";
export function setupManifest<
M extends GenericManifest & { id: Id; version: Version },
Id extends string,
Version extends ManifestVersion,
>(manifest: M): M {
Dependencies extends Record<string, unknown>,
>(
manifest: GenericManifest & {
dependencies: Dependencies;
id: Id;
version: Version;
},
): GenericManifest & { dependencies: Dependencies; id: Id; version: Version } {
return manifest;
}

View File

@@ -7,7 +7,6 @@ import { Migration } from "./Migration";
export function setupMigrations<Migrations extends Array<Migration<any>>>(
manifest: GenericManifest,
initializeActions: ReturnType<typeof setupActions>["initializeActions"],
...migrations: EnsureUniqueId<Migrations>
) {
const sortedMigrations = once(() => {
@@ -19,7 +18,6 @@ export function setupMigrations<Migrations extends Array<Migration<any>>>(
});
const currentVersion = once(() => EmVer.parse(manifest.version));
const init: ExpectedExports.init = async ({ effects, previousVersion }) => {
await initializeActions(effects);
if (!!previousVersion) {
const previousVersionEmVer = EmVer.parse(previousVersion);
for (const [_, migration] of sortedMigrations()

View File

@@ -437,7 +437,7 @@ export type MigrationRes = {
export type ActionResult = {
message: string;
value?: string;
value: null | string;
copyable: boolean;
qr: boolean;
};

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "start-sdk",
"version": "0.4.0-lib0.charlie34",
"version": "0.4.0-lib0.charlie36",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "start-sdk",
"version": "0.4.0-lib0.charlie34",
"version": "0.4.0-lib0.charlie36",
"license": "MIT",
"dependencies": {
"@iarna/toml": "^2.2.5",

View File

@@ -1,6 +1,6 @@
{
"name": "start-sdk",
"version": "0.4.0-lib0.charlie34",
"version": "0.4.0-lib0.charlie36",
"description": "For making the patterns that are wanted in making services for the startOS.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",