@maverick-js/scheduler
v2.1.0
Published
A tiny (~250B) microtask scheduler.
Downloads
2
Readme
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 = () => {};
// Queue tasks.
scheduler.enqueue(taskA);
scheduler.enqueue(taskB);
// Be notified of a flush.
const stop = scheduler.onFlush(() => {
console.log('Flushed!');
});
stop(); // unsubscribe
// Schedule a flush - can be invoked more than once.
scheduler.flush();
// Wait for flush to complete.
await scheduler.tick;
// Synchronously flush the queue whenever desired.
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: