import * as TE from "fp-ts/lib/TaskEither"; import { pipe } from "fp-ts/lib/function"; import type { Schedule } from "../canary"; interface ScheduledJob { id: string; execute: TE.TaskEither; at: Date; schedule: Schedule; } type SchedulerState = ReadonlyArray; export const schedulerLoop = (state: SchedulerState): TE.TaskEither => () => { const loop = (currentState: SchedulerState): TE.TaskEither => pipe( executeDueJobs(currentState), delay(1000), // Delay for 1 second map((newState) => loop(newState)), ); return loop(state)(); };