class · Browser and Node

# VirtualScheduler

Virtual time. Also a trust-core Clock (now, iso, date), so Endpoints on a simulated mesh see the same time as the radio. Every run with the same inputs is identical.

`@toip-tools/mesh-transport/sim` · [View source](https://github.com/yourbuddyconner/toip-tools/blob/main/packages/mesh-transport/src/scheduler.ts#L29)

Import:

```ts
import { VirtualScheduler } from "@toip-tools/mesh-transport/sim";
```

## Constructor

Constructor:

```ts
new (startMs?: number): VirtualScheduler;
```

### Parameters

**`startMs` (optional)**

`number`

Default: `Date.parse("2026-11-06T12:00:00.000Z")`

### Returns

`VirtualScheduler`

## Methods

### `now`

Signature:

```ts
now(): number;
```

No parameters.

#### Returns

`number`

### `iso`

Signature:

```ts
iso(): string;
```

No parameters.

#### Returns

`string`

### `date`

Signature:

```ts
date(): Date;
```

No parameters.

#### Returns

`Date`

### `setTimeout`

Signature:

```ts
setTimeout(fn: () => void, ms: number): unknown;
```

#### Parameters

- **`fn` (required)**: `() => void`
- **`ms` (required)**: `number`

#### Returns

`unknown`

### `clearTimeout`

Signature:

```ts
clearTimeout(handle: unknown): void;
```

#### Parameters

- **`handle` (required)**: `unknown`

#### Returns

`void`

### `pending`

Timers still waiting.

Signature:

```ts
pending(): number;
```

No parameters.

#### Returns

`number`

### `hold`

Freezes virtual time until `p` settles, and returns it. For real I/O inside a simulation (a post to a real relay, say): without it, timers would fire as fast as they can while the I/O is out, and a sender would time out in virtual seconds that took a few real milliseconds.

Signature:

```ts
hold<T>(p: Promise<T>): Promise<T>;
```

#### Parameters

- **`p` (required)**: `Promise<T>`

#### Returns

`Promise<T>`

### `holding`

Whether some held I/O is still out.

Signature:

```ts
holding(): boolean;
```

No parameters.

#### Returns

`boolean`

### `advance`

Moves time forward by `ms`, running every timer due on the way.

Signature:

```ts
advance(ms: number): Promise<void>;
```

#### Parameters

- **`ms` (required)**: `number`

#### Returns

`Promise<void>`

### `runUntilIdle`

Runs timers until none are left, or until `maxMs` of virtual time has passed.

Signature:

```ts
runUntilIdle(opts?: {
    maxMs?: number;
}): Promise<void>;
```

#### Parameters

**`opts` (optional)**

`{ maxMs?: number; }`

Default: `{}`

**Fields of `opts`**

- **`maxMs` (optional)**: `number | undefined`

#### Returns

`Promise<void>`

### `run`

Runs timers until `p` settles, and returns its result. When nothing is scheduled but `p` is still pending (real I/O, say), it waits in real time, up to `realIdleMs`.

Signature:

```ts
run<T>(p: Promise<T>, opts?: {
    maxMs?: number;
    realIdleMs?: number;
}): Promise<T>;
```

#### Parameters

**`p` (required)**

`Promise<T>`

**`opts` (optional)**

`{ maxMs?: number; realIdleMs?: number; }`

Default: `{}`

**Fields of `opts`**

- **`maxMs` (optional)**: `number | undefined`
- **`realIdleMs` (optional)**: `number | undefined`

#### Returns

`Promise<T>`

[Usage guide](https://toip.tools/toolkit/mesh-transport/#usage) · [Error handling](https://toip.tools/toolkit/mesh-transport/#errors) · [All exports](https://toip.tools/toolkit/mesh-transport/api/sim/)

---

This is the Markdown version of https://toip.tools/toolkit/mesh-transport/api/sim/VirtualScheduler/. An index of the documentation for AI assistants is at https://toip.tools/llms.txt.
