Перейти к содержимому
GitHubXDiscord

createScheduler

Стабильный · с 0.1.0

Создаёт планировщик диспатча, которым рантайм пользуется внутри. Две встроенные стратегии — 'microtask' и 'sync' — закрывают почти любые реальные потребности; createScheduler доступен для кастомной композиции рантайма, низкоуровневых тестов и бенчмарков.

Microtask-планировщик батчит поставленные в очередь задачи и осушает их в queueMicrotask. Sync-планировщик запускает задачи немедленно, в стеке вызывающей стороны.

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

type SchedulerStrategy = 'sync' | 'microtask';

type SchedulerImpl = {
  enqueue(task: () => void): void;
  flush(): void;
};
ПараметрТипОписание
strategy'sync' | 'microtask'Политика диспатча.

SchedulerImpl:

МетодОписание
enqueue(task)Поставить thunk в очередь. Microtask: батчит до следующего осушения микротасок. Sync: запускает немедленно.
flush()Осушить отложенные задачи прямо сейчас. Для тестов и fireSync. Sync-планировщик очередь не держит, поэтому его flush — 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"