mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 12:21:57 +00:00
chore: Update the types
This commit is contained in:
79
lib/mainFn/Daemons.ts
Normal file
79
lib/mainFn/Daemons.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { HealthReceipt, ReadyProof } from "../health";
|
||||
import { CheckResult } from "../health/checkFns";
|
||||
import { Trigger } from "../health/trigger";
|
||||
import { Effects, ValidIfNoStupidEscape } from "../types";
|
||||
import { InterfaceReceipt } from "./interfaceReceipt";
|
||||
import { RunningMainRet } from "./RunningMainRet";
|
||||
type Daemon<
|
||||
Ids extends string | never,
|
||||
Command extends string,
|
||||
Id extends string
|
||||
> = {
|
||||
id: Id;
|
||||
command: ValidIfNoStupidEscape<Command> | [string, ...string[]];
|
||||
|
||||
ready: {
|
||||
display: null | {
|
||||
name: "Websocket Live";
|
||||
message: "The websocket is live";
|
||||
};
|
||||
fn: () => Promise<CheckResult> | CheckResult;
|
||||
trigger?: Trigger;
|
||||
};
|
||||
requires?: Exclude<Ids, Id>[];
|
||||
};
|
||||
|
||||
const todo = <A>(): A => {
|
||||
throw new Error("TODO");
|
||||
};
|
||||
/**
|
||||
* Used during the main of a function, it allows us to describe and ensure a set of daemons are running.
|
||||
* With the dependency, we are using this like an init system, where we can ensure that a daemon is running
|
||||
* The return type of this is used in the runningMain
|
||||
```ts
|
||||
Daemons.with({
|
||||
effects,
|
||||
started,
|
||||
interfacesReceipt,
|
||||
})
|
||||
.addDaemon({
|
||||
id: "nostr",
|
||||
command: "./nostr-rs-relay --db /data",
|
||||
ready: {
|
||||
display: {
|
||||
name: "Websocket Live",
|
||||
message: "The websocket is live",
|
||||
},
|
||||
fn: () => checkPortListening(effects, 8080),
|
||||
},
|
||||
})
|
||||
.build()
|
||||
```
|
||||
*/
|
||||
export class Daemons<Ids extends string | never> {
|
||||
private constructor(
|
||||
readonly effects: Effects,
|
||||
readonly started: (onTerm: () => void) => null,
|
||||
readonly daemons?: Daemon<Ids, "command", never>[]
|
||||
) {}
|
||||
|
||||
static with(config: {
|
||||
effects: Effects;
|
||||
started: (onTerm: () => void) => null;
|
||||
interfaceReceipt: InterfaceReceipt;
|
||||
healthReceipts: HealthReceipt[];
|
||||
}) {
|
||||
return new Daemons<never>(config.effects, config.started);
|
||||
}
|
||||
addDaemon<Id extends string, Command extends string>(
|
||||
newDaemon: Daemon<Ids, Command, Id>
|
||||
) {
|
||||
const daemons = [...(this?.daemons ?? [])];
|
||||
daemons.push(newDaemon as any);
|
||||
return new Daemons<Ids | Id>(this.effects, this.started, daemons);
|
||||
}
|
||||
|
||||
build() {
|
||||
return todo<RunningMainRet>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user