API reference@evolu/commonSchedule › compensate

function compensate<Output, Input>(
  schedule: Schedule<Output, Input>,
): Schedule<Output, Input>;

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

Adjusts delay by subtracting time elapsed beyond the previously returned delay.

In a normal executor loop, this corresponds to the previous execution time. If the runtime wakes later than requested, the extra lag is also compensated. If execution and lag took longer than the delay, returns 0.

When composing with delay-shaping combinators such as maxDelay, put compensate near the outside of the stack so it observes the final returned delay.

For window-aligned scheduling, use fixed instead.

Compensating for execution time

import { assertOk, compensate, spaced, testCreateDeps } from "@evolu/common";

const fastDeps = testCreateDeps();
const fastStep = compensate(spaced("5s"))(fastDeps);
assertOk(fastStep(undefined), [5000, 5000]);
// Five seconds waiting plus one second working leaves four seconds.
fastDeps.time.advance("6s");
assertOk(fastStep(undefined), [5000, 4000]);

const slowDeps = testCreateDeps();
const slowStep = compensate(spaced("5s"))(slowDeps);
assertOk(slowStep(undefined), [5000, 5000]);
// Five seconds waiting plus six seconds working leaves no delay.
slowDeps.time.advance("11s");
assertOk(slowStep(undefined), [5000, 0]);