mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 20:24:47 +00:00
chore: Adding mainFn helpers
This commit is contained in:
4
lib/health/trigger/TriggerInput.ts
Normal file
4
lib/health/trigger/TriggerInput.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export type TriggerInput = {
|
||||
lastResult: "success" | "failure" | null;
|
||||
hadSuccess: boolean;
|
||||
};
|
||||
28
lib/health/trigger/changeOnFirstSuccess.ts
Normal file
28
lib/health/trigger/changeOnFirstSuccess.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { TriggerInput } from "./TriggerInput";
|
||||
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);
|
||||
for (
|
||||
let res = await beforeFirstSuccess.next(currentValue);
|
||||
currentValue?.lastResult !== "success" && !res.done;
|
||||
res = await beforeFirstSuccess.next(currentValue)
|
||||
) {
|
||||
currentValue = yield;
|
||||
}
|
||||
const afterFirstSuccess = o.afterFirstSuccess();
|
||||
for (
|
||||
let res = await afterFirstSuccess.next(currentValue);
|
||||
!res.done;
|
||||
res = await afterFirstSuccess.next(currentValue)
|
||||
) {
|
||||
currentValue = yield;
|
||||
}
|
||||
};
|
||||
}
|
||||
8
lib/health/trigger/cooldownTrigger.ts
Normal file
8
lib/health/trigger/cooldownTrigger.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export function cooldownTrigger(timeMs: number) {
|
||||
return async function* () {
|
||||
while (true) {
|
||||
await new Promise((resolve) => setTimeout(resolve, timeMs));
|
||||
yield;
|
||||
}
|
||||
};
|
||||
}
|
||||
7
lib/health/trigger/defaultTrigger.ts
Normal file
7
lib/health/trigger/defaultTrigger.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { cooldownTrigger } from "./cooldownTrigger";
|
||||
import { changeOnFirstSuccess } from "./changeOnFirstSuccess";
|
||||
|
||||
export const defaultTrigger = changeOnFirstSuccess({
|
||||
beforeFirstSuccess: cooldownTrigger(0),
|
||||
afterFirstSuccess: cooldownTrigger(30000),
|
||||
});
|
||||
5
lib/health/trigger/index.ts
Normal file
5
lib/health/trigger/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { TriggerInput } from "./TriggerInput";
|
||||
export { changeOnFirstSuccess } from "./changeOnFirstSuccess";
|
||||
export { cooldownTrigger } from "./cooldownTrigger";
|
||||
|
||||
export type Trigger = () => AsyncIterator<unknown, unknown, TriggerInput>;
|
||||
Reference in New Issue
Block a user