API reference › @evolu/common › Schedule › unfoldSchedule
function unfoldSchedule<State>(
initial: State,
next: (state: State) => State,
): Schedule<State>;
Defined in: packages/common/src/Schedule.ts:541
Creates a schedule by unfolding a state.
Each step outputs the current state and computes the next state using the provided function. Never stops — combine with take or maxElapsed to limit.
Example
// Counter: 0, 1, 2, 3, ...
const counter = unfoldSchedule(0, (n) => n + 1);
// Custom backoff: 100, 150, 225, 338, ... (×1.5 each time)
const customBackoff = unfoldSchedule(100, (delay) => Math.round(delay * 1.5));
// State machine
type Phase = "init" | "warmup" | "active";
const phases = unfoldSchedule<Phase>("init", (phase) => {
switch (phase) {
case "init":
return "warmup";
case "warmup":
return "active";
case "active":
return "active";
}
});