@tinyactors/scxmljs
    Preparing search index...

    Class VirtualClock

    Deterministic time for tests and playback. Timers live in a binary heap (O(log n) to schedule and fire) and cancelling is O(1), so a clock with hundreds of thousands of pending timers stays fast.

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Accessors

    • get idle(): boolean

      True when nothing is scheduled: no deferred work and no pending timers.

      Returns boolean

    • get nextTimerAt(): number | undefined

      Virtual time of the next timer, if any.

      Returns number | undefined

    • get pending(): number

      Number of pending tasks (deferred work + timers).

      Returns number

    • get ready(): number

      Deferred work waiting to run now (e.g. queued external events).

      Returns number

    Methods

    • Advance by ms, running everything due.

      Parameters

      • ms: number

      Returns number

    • Cancel a timer from setTimeout. Unknown or already-fired handles are ignored.

      Parameters

      • handle: unknown

      Returns void

    • Run fn soon, after the current task (a microtask on the real clock).

      Parameters

      • fn: () => void

      Returns void

    • The current time in ms (real or virtual).

      Returns number

    • Runs deferred work, then fires timers in time order, advancing virtual time, until nothing is pending or the next timer is later than until (absolute virtual ms). Returns the number of tasks run.

      Parameters

      • until: number = Infinity
      • maxTasks: number = 1_000_000

      Returns number

    • Run exactly one task: the oldest deferred one, else the earliest timer (advancing virtual time to it). Returns false when nothing is pending.

      Returns boolean

    • Run fn after ms milliseconds of this clock's time; returns a handle for clearTimeout.

      Parameters

      • fn: () => void
      • ms: number

      Returns number