API reference@evolu/commonSchedule › whenInput

function whenInput<Input, Output>(
  predicate: Predicate<Input>,
  altSchedule: Schedule<Output, Input>,
): (schedule: Schedule<Output, Input>) => Schedule<Output, Input>;

Defined in: packages/common/src/Schedule.ts:1987

Selects between two schedules based on input.

If Predicate returns true, uses altSchedule; otherwise uses the base schedule. Useful for implementing error-aware backoff where certain errors (e.g., throttling) use different delays.

Each branch has independent state. Place combinators such as take outside whenInput when their state must be shared across both branches.

Selecting a schedule by input

import {
  assertErr,
  assertOk,
  done,
  exponential,
  take,
  testCreateDeps,
  whenInput,
  type Millis,
  type Typed,
} from "@evolu/common";

interface MyError extends Typed<"Throttled" | "NetworkError"> {}

// The outer take shares one retry limit across both error branches.
const awsWithThrottling = take(3)(
  whenInput<MyError, Millis>(
    (error) => error.type === "Throttled",
    exponential("1s"),
  )(exponential("100ms")),
);
const step = awsWithThrottling(testCreateDeps());
assertOk(step({ type: "Throttled" }), [1000, 1000]);
assertOk(step({ type: "NetworkError" }), [100, 100]);
assertOk(step({ type: "Throttled" }), [2000, 2000]);
assertErr(step({ type: "NetworkError" }), done());