Skip to content
GitHubXDiscord

createScheduler

Stable · since 0.1.0

Constructs the dispatch scheduler used internally by the runtime. The two built-in strategies — 'microtask' and 'sync' — cover almost every real-world need; createScheduler is exposed for custom runtime composition, low-level tests, and benchmarking.

The microtask scheduler batches enqueued tasks and drains them in queueMicrotask. The sync scheduler runs tasks immediately, in the caller’s stack.

import { createScheduler } from '@triggery/core';
function createScheduler(strategy: SchedulerStrategy): SchedulerImpl;

type SchedulerStrategy = 'sync' | 'microtask';

type SchedulerImpl = {
  enqueue(task: () => void): void;
  flush(): void;
};
ParamTypeDescription
strategy'sync' | 'microtask'Dispatch policy.

A SchedulerImpl:

MethodDescription
enqueue(task)Schedule a thunk. Microtask: batches into the next microtask drain. Sync: runs immediately.
flush()Drain any pending tasks now. For tests and fireSync. Sync scheduler keeps no queue, so its flush is a no-op.
import { createSchedulerfunction createScheduler(strategy: SchedulerStrategy): SchedulerImpl } from '@triggery/core';

const schedconst sched: SchedulerImpl = createSchedulerfunction createScheduler(strategy: SchedulerStrategy): SchedulerImpl('microtask');
const logconst log: number[]: number[] = [];

schedconst sched: SchedulerImpl.enqueuefunction enqueue(task: Task): void(() => logconst log: number[].pushArray<number>.push(...items: number[]): number
Appends new elements to the end of an array, and returns the new length of the array.
@paramitems New elements to add to the array.
(1));
schedconst sched: SchedulerImpl.enqueuefunction enqueue(task: Task): void(() => logconst log: number[].pushArray<number>.push(...items: number[]): number
Appends new elements to the end of an array, and returns the new length of the array.
@paramitems New elements to add to the array.
(2));
schedconst sched: SchedulerImpl.enqueuefunction enqueue(task: Task): void(() => logconst log: number[].pushArray<number>.push(...items: number[]): number
Appends new elements to the end of an array, and returns the new length of the array.
@paramitems New elements to add to the array.
(3));
// At this synchronous point, nothing has run yet. consolevar console: Console.logConsole.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
('queued', logconst log: number[]); // []
await Promisevar Promise: PromiseConstructor
Represents the completion of an asynchronous operation
.resolvePromiseConstructor.resolve(): Promise<void> (+2 overloads)
Creates a new resolved promise.
@returnsA resolved promise.
(); // drain microtasks
consolevar console: Console.logConsole.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
('drained', logconst log: number[]); // [1, 2, 3]
import { createSchedulerfunction createScheduler(strategy: SchedulerStrategy): SchedulerImpl } from '@triggery/core';

const schedconst sched: SchedulerImpl = createSchedulerfunction createScheduler(strategy: SchedulerStrategy): SchedulerImpl('sync');
const logconst log: number[]: number[] = [];

schedconst sched: SchedulerImpl.enqueuefunction enqueue(task: Task): void(() => logconst log: number[].pushArray<number>.push(...items: number[]): number
Appends new elements to the end of an array, and returns the new length of the array.
@paramitems New elements to add to the array.
(1));
schedconst sched: SchedulerImpl.enqueuefunction enqueue(task: Task): void(() => logconst log: number[].pushArray<number>.push(...items: number[]): number
Appends new elements to the end of an array, and returns the new length of the array.
@paramitems New elements to add to the array.
(2));
// Already executed inline. consolevar console: Console.logConsole.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
(logconst log: number[]); // [1, 2]
import { createSchedulerfunction createScheduler(strategy: SchedulerStrategy): SchedulerImpl } from '@triggery/core';

const schedconst sched: SchedulerImpl = createSchedulerfunction createScheduler(strategy: SchedulerStrategy): SchedulerImpl('microtask');

schedconst sched: SchedulerImpl.enqueuefunction enqueue(task: Task): void(() => {
  throw new Error
var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
('oops');
}); schedconst sched: SchedulerImpl.enqueuefunction enqueue(task: Task): void(() => { consolevar console: Console.logConsole.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
('still runs');
}); await Promisevar Promise: PromiseConstructor
Represents the completion of an asynchronous operation
.resolvePromiseConstructor.resolve(): Promise<void> (+2 overloads)
Creates a new resolved promise.
@returnsA resolved promise.
();
// Output: "[triggery] scheduler task failed: Error: oops" // Output: "still runs"