@maverick-js/scheduler
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

Scheduler

package-badge license-badge size-badge

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:

Readme

Keywords

none

Package Sidebar

Install

npm i @maverick-js/scheduler

Weekly Downloads

0

Version

2.1.0

License

MIT

Unpacked Size

5.57 kB

Total Files

5

Last publish

Collaborators

  • mihar-22