npm.io
1.1.0 • Published 1 month ago

@bemedev/scheduler

Licence
MIT
Version
1.1.0
Deps
1
Size
47 kB
Vulns
0
Weekly
0

@bemedev/scheduler

A flexible scheduler for managing and executing tasks with advanced timing and state management capabilities.


Installation

pnpm add @bemedev/scheduler

Usage

Async Scheduler (default)
import { createScheduler } from '@bemedev/scheduler';

const scheduler = createScheduler();

// Initialize the scheduler (optionally with an initial task)
scheduler.start();

// Schedule a synchronous task
scheduler.schedule(() => {
  console.log('Task executed');
});

// Schedule an asynchronous task
scheduler.schedule(async () => {
  await fetch('/api/data');
});

// Schedule with immediate execution (bypasses the queue)
scheduler.schedule(() => console.log('Immediate!'), true);

// Stop the scheduler (cancels pending async tasks)
scheduler.stop();

// Check scheduler state
console.log(scheduler.performeds); // number of tasks executed
console.log(scheduler.state); // current state
Synchronous Scheduler
import { SyncScheduler } from '@bemedev/scheduler/sync';

const scheduler = new SyncScheduler();

// Initialize the scheduler
scheduler.start();

// Schedule a synchronous task
scheduler.schedule(() => {
  console.log('Task executed');
});

// Stop the scheduler
scheduler.stop();

API

Main Export (Async Scheduler)

Creates and returns a new Scheduler instance via createScheduler().

scheduler.start(callback?)

Initializes the scheduler. Optionally executes an initial task immediately. No-op if already started. Returns this.

scheduler.schedule(callback, immediate?)

Schedules a task for execution.

Parameter Type Default Description
callback () => any | Promise<any> The task to execute
immediate boolean false If true, bypasses the queue and runs immediately with abort support
scheduler.stop()

Stops the scheduler and cancels any in-flight async tasks via AbortController. Returns the final state 'stopped'.

scheduler.state

Read-only. Current state of the scheduler:

Value Description
'available' Ready to process tasks
'processing' Currently executing a task
'stopped' Permanently stopped
scheduler.performeds

Read-only. Number of tasks successfully executed.

Synchronous Scheduler Export (./sync)

Access via import { SyncScheduler } from '@bemedev/scheduler/sync'.

The SyncScheduler class provides the same interface as the async scheduler but executes tasks synchronously without Promise support.

Async Scheduler Export (./async)

Access via import { createScheduler } from '@bemedev/scheduler/async' (equivalent to the default export).


Licence

MIT

CHANGE_LOG

[0.0.1] - 09/03/2026 => 03:36

  • Add Scheduler class with full lifecycle management
  • Add createScheduler() factory function
  • Add start, schedule, stop methods
  • Add async task support with AbortController
  • Test coverage 100%

Auteur

chlbri (bri_lvi@icloud.com)

My github


Liens

Keywords