Methods
new Stack()
Creates an isolated empty stack with auto-run enabled and stop disabled.
- Returns
- A new
Stackinstance.
stack.add(...tasks)
Validates the complete batch, appends it, and starts the newest task when the stack is idle, unblocked, and in auto-run mode.
- Returns
- The stack instance for chaining.
- Throws
TypeErrorwhen any item is not a function. Nothing is added on failure.
stack.next()
Selects and invokes one newest pending task with the stack bound as this. Empty or stopped stacks become idle.
- Returns
undefined; callback return values are ignored.- Throws
- The callback's original error, after restoring the idle state.
stack.clear()
Replaces the pending callback array without interrupting a callback that is already active.
- Returns
- The new empty live array.
stack.contents([tasks])
Reads the live callback array. When an argument is provided, validates and installs that array by reference.
- Returns
- The current live callback array.
- Throws
TypeErrorunless the replacement is an array containing only functions.
State
| Member | Type | Default | Behavior |
|---|---|---|---|
stack.stack | Array | [] | Live pending-task array, bottom first and newest last. Read/write with validation on replacement. |
stack.size | number | 0 | Read-only current pending-task count. |
stack.running | boolean | false | Read-only execution state. It remains true while an active task has not handed off. |
stack.autoRun | any truthy/falsy | true | When truthy, an idle eligible stack starts as soon as work is added. |
stack.stop | any truthy/falsy | false | When truthy, next() consumes nothing and sets running false. |
Advanced schedulers may inspect or reorder pending tasks directly. Prefer add() and whole-array replacement when you want validation before mutation.
Export shapes
import Stack, { Stack as NamedStack } from 'easy-stack';
console.log(Stack === NamedStack); // true
const RequiredStack = require('easy-stack');
console.log(RequiredStack === RequiredStack.Stack); // true