Scheduler
This is a tiny (~250B minzipped) scheduler which batches and runs tasks off the microtask queue.
Installation
$: npm i @maverick-js/scheduler
$: pnpm i @maverick-js/scheduler
$: yarn add @maverick-js/scheduler
Usage
import { createScheduler } from '@maverick-js/scheduler';
const scheduler = createScheduler();
const taskA = () => {};
const taskB = () => {};
scheduler.enqueue(taskA);
scheduler.enqueue(taskB);
const stop = scheduler.onFlush(() => {
console.log('Flushed!');
});
stop();
scheduler.flush();
await scheduler.tick;
scheduler.flushSync();
Extra reading:
- The source file is only ~80 LOC so feel free to dig through.
- You can read more about microtasks on MDN.
Inspiration
@maverick-js/scheduler
was made possible based on my learnings from: