@bemedev/sequence
v0.1.0
Published
A simple utility for managing sequences of timed actions in JavaScript. Provides an easy way to create and run sequences of actions with customizable delays and error handling.
Maintainers
Readme
@bemedev/sequence
A simple utility for managing sequences of timed actions in JavaScript/TypeScript. Provides an easy way to create and run sequences of actions with customizable delays.
Installation
npm install @bemedev/sequence
# or
pnpm add @bemedev/sequenceUsage
import { createSequence } from '@bemedev/sequence';
const seq = createSequence({ delayMultiplier: 2 });
seq
.add(500, () => console.log('Fires after 1000ms'))
.add(300, () => console.log('Fires after 1600ms')) // 500+300=800, ×2=1600ms
.add(200, () => console.log('Fires after 2000ms')); // 800+200=1000, ×2=2000ms
await seq.run();API
createSequence(options?) / sequence(options?)
Factory function that returns a SequenceType instance.
| Option | Type | Default | Description |
| ----------------- | -------- | ------- | ----------------------------------------------- |
| delayMultiplier | number | 1 | Multiplier applied to every delay. Must be > 0. |
Throws RangeError if delayMultiplier <= 0.
SequenceType
Methods
| Method | Signature | Description |
| ------- | ------------------------------------------ | ----------------------------------------------------------------------------------------------------- |
| add | (delay: number, action?: Action) => this | Adds a step. delay (ms) is accumulated onto the previous step's delay. Returns this for chaining. |
| run | () => Promise<void> | Runs all steps. Concurrent calls while running are ignored. No-op if already finished. |
| clear | () => this | Removes all steps and resets state to 'started'. Returns this. |
Getters
| Getter | Type | Description |
| ------- | -------------- | ---------------------------------------------------------------------- |
| size | number | Number of registered steps. |
| state | State | Current state: 'idle' | 'started' | 'running' | 'finished'. |
| renew | SequenceType | New instance with the same options but no entries. |
State machine
idle → started → running → finishednothing
A no-op Action used as the default when no callback is provided to add.
Types
type Action = () => any | Promise<any>;
type SequenceOptions = {
delayMultiplier?: number; // default: 1
};
type State = 'idle' | 'started' | 'running' | 'finished';Licence
MIT
Auteur
chlbri ([email protected])
CHANGELOG
See CHANGELOG.md
