diff options
| author | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:10:07 -0700 |
|---|---|---|
| committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:10:07 -0700 |
| commit | 2859955095020b07909b079d34be8a80e7f58ecd (patch) | |
| tree | b10ee799208a50a800fe5cee47ade46c90770d2e /src/util/scheduler.ts | |
| parent | 5dbd16878546886cae5b56b67de6185bfbae0d07 (diff) | |
| download | mon.hatecomputers.club-main.tar.gz mon.hatecomputers.club-main.zip | |
Diffstat (limited to 'src/util/scheduler.ts')
| -rw-r--r-- | src/util/scheduler.ts | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/util/scheduler.ts b/src/util/scheduler.ts index 9f9f594..476aca0 100644 --- a/src/util/scheduler.ts +++ b/src/util/scheduler.ts @@ -1,24 +1,38 @@ import * as TE from "fp-ts/lib/TaskEither"; -import { pipe } from "fp-ts/lib/function"; -import type { Schedule } from "../canary"; +import { pipe, identity } from "fp-ts/lib/function"; +import type { Job, Schedule } from "../canary"; +import * as RA from "fp-ts/lib/ReadonlyArray"; +import * as T from "fp-ts/lib/Task"; +import type { Separated } from "fp-ts/lib/Separated"; interface ScheduledJob { id: string; - execute: TE.TaskEither<Error, boolean>; + execute: () => TE.TaskEither<Error, boolean>; at: Date; schedule: Schedule; } type SchedulerState = ReadonlyArray<ScheduledJob>; +const executeAll = ( + jobs: ReadonlyArray<TE.TaskEither<Error, boolean>>, +): T.Task<Separated<ReadonlyArray<Error>, ReadonlyArray<boolean>>> => + pipe(jobs, RA.wilt(T.ApplicativePar)(identity)); + export const schedulerLoop = (state: SchedulerState): TE.TaskEither<Error, void> => () => { - const loop = (currentState: SchedulerState): TE.TaskEither<Error, void> => + const loop = ( + currentState: SchedulerState, + time: Date, + ): TE.TaskEither<Error, void> => pipe( - executeDueJobs(currentState), - delay(1000), // Delay for 1 second - map((newState) => loop(newState)), + currentState, + RA.filter((job) => job.at <= time), + RA.map(({ execute }) => execute()), + executeAll, + //T.delay(1000), // Delay for 1 second + // map((newState) => loop(newState)), ); return loop(state)(); |
