@bemedev/scheduler
v1.2.1
Published
A cancellable interval and timer library for JavaScript and TypeScript.
Maintainers
Readme
@bemedev/scheduler
A flexible scheduler for managing and executing tasks with advanced timing and state management capabilities.
Installation
pnpm add @bemedev/schedulerUsage
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);
// Pause task execution
scheduler.pause();
// Schedule tasks while paused (they remain queued)
scheduler.schedule(() => console.log('Queued task'));
// Resume task execution (processes queued tasks asynchronously)
await scheduler.resume();
// 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 stateSynchronous Scheduler
import { createSyncScheduler } from '@bemedev/scheduler/sync';
const scheduler = createSyncScheduler();
// Initialize the scheduler
scheduler.start();
// Schedule a synchronous task
scheduler.schedule(() => {
console.log('Task executed');
});
// Pause task execution
scheduler.pause();
// Schedule tasks while paused
scheduler.schedule(() => console.log('Queued task'));
// Resume task execution synchronously (executes queued tasks immediately)
scheduler.resume();
// Stop the scheduler
scheduler.stop();API
Main Export (Async Scheduler)
Creates and returns a new AsyncScheduler instance via createScheduler()
(or createAsyncScheduler()).
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.pause()
Pauses task execution. Saves the current state and sets scheduler.state
to 'paused'. Tasks scheduled while paused are queued without being
processed until resume() is called. Returns the updated state 'paused'.
scheduler.resume()
Resumes task execution:
- Async Scheduler:
await scheduler.resume()restores state and asynchronously processes the next queued task. Returns aPromise<State>. - Sync Scheduler:
scheduler.resume()restores state and synchronously processes all queued tasks. ReturnsState.
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 |
| 'paused' | Paused; tasks are queued |
| 'stopped' | Permanently stopped |
scheduler.isPaused
Read-only. Returns true if scheduler.state is 'paused' or
'stopped', false otherwise.
scheduler.notAvailable
Read-only. Returns true if scheduler.state is not 'available',
false otherwise.
scheduler.performeds
Read-only. Number of tasks successfully executed.
Synchronous Scheduler Export (./sync)
Access via
import { createSyncScheduler, createScheduler } from '@bemedev/scheduler/sync'.
The createSyncScheduler() (or createScheduler()) factory function
creates a synchronous scheduler instance that provides the same interface
as the async scheduler, but executes tasks synchronously without Promise
support.
Async Scheduler Export (./async)
Access via
import { createAsyncScheduler, createScheduler } from '@bemedev/scheduler/async'
(equivalent to the default export from @bemedev/scheduler).
Licence
MIT
CHANGE_LOG
[0.0.1] - 09/03/2026 => 03:36
- Add
Schedulerclass with full lifecycle management - Add
createScheduler()factory function - Add
start,schedule,stopmethods - Add async task support with
AbortController - Test coverage 100%
Auteur
chlbri ([email protected])
