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
Section titled “Import”import { createScheduler } from '@triggery/core';
Signature
Section titled “Signature”function createScheduler(strategy: SchedulerStrategy): SchedulerImpl;
type SchedulerStrategy = 'sync' | 'microtask';
type SchedulerImpl = {
enqueue(task: () => void): void;
flush(): void;
};Parameters
Section titled “Parameters”| Param | Type | Description |
|---|---|---|
strategy | 'sync' | 'microtask' | Dispatch policy. |
Returns
Section titled “Returns”A SchedulerImpl:
| Method | Description |
|---|---|
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. |
Examples
Section titled “Examples”Microtask batching
Section titled “Microtask batching”import { createScheduler function createScheduler(strategy: SchedulerStrategy): SchedulerImpl } from '@triggery/core';
const sched const sched: SchedulerImpl = createScheduler function createScheduler(strategy: SchedulerStrategy): SchedulerImpl ('microtask');
const log const log: number[] : number[] = [];
sched const sched: SchedulerImpl .enqueue function enqueue(task: Task): void (() => log const log: number[] .push Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array. (1));
sched const sched: SchedulerImpl .enqueue function enqueue(task: Task): void (() => log const log: number[] .push Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array. (2));
sched const sched: SchedulerImpl .enqueue function enqueue(task: Task): void (() => log const log: number[] .push Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array. (3));
// At this synchronous point, nothing has run yet.
console var console: Console .log Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) ('queued', log const log: number[] ); // []
await Promise var Promise: PromiseConstructorRepresents the completion of an asynchronous operation .resolve PromiseConstructor.resolve(): Promise<void> (+2 overloads)Creates a new resolved promise. (); // drain microtasks
console var console: Console .log Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) ('drained', log const log: number[] ); // [1, 2, 3]Synchronous dispatch
Section titled “Synchronous dispatch”import { createScheduler function createScheduler(strategy: SchedulerStrategy): SchedulerImpl } from '@triggery/core';
const sched const sched: SchedulerImpl = createScheduler function createScheduler(strategy: SchedulerStrategy): SchedulerImpl ('sync');
const log const log: number[] : number[] = [];
sched const sched: SchedulerImpl .enqueue function enqueue(task: Task): void (() => log const log: number[] .push Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array. (1));
sched const sched: SchedulerImpl .enqueue function enqueue(task: Task): void (() => log const log: number[] .push Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array. (2));
// Already executed inline.
console var console: Console .log Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) (log const log: number[] ); // [1, 2]Error isolation in microtask mode
Section titled “Error isolation in microtask mode”import { createScheduler function createScheduler(strategy: SchedulerStrategy): SchedulerImpl } from '@triggery/core';
const sched const sched: SchedulerImpl = createScheduler function createScheduler(strategy: SchedulerStrategy): SchedulerImpl ('microtask');
sched const sched: SchedulerImpl .enqueue function enqueue(task: Task): void (() => {
throw new Error var Error: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
('oops');
});
sched const sched: SchedulerImpl .enqueue function enqueue(task: Task): void (() => {
console var console: Console .log Console.log(...data: any[]): voidThe **`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 Promise var Promise: PromiseConstructorRepresents the completion of an asynchronous operation .resolve PromiseConstructor.resolve(): Promise<void> (+2 overloads)Creates a new resolved promise. ();
// Output: "[triggery] scheduler task failed: Error: oops"
// Output: "still runs"Related
Section titled “Related” createRuntime The runtime picks a scheduler per trigger automatically.
createFakeScheduler Virtual-clock scheduler for tests.
Scheduling guide Microtask vs sync — when to pick which.
Concurrency strategies How async handlers interleave under each strategy.