Priority-based queue implementations for managing items and tasks with FIFO ordering. Includes both generic item queues and specialized task queues with priority-based ordering, FIFO ordering within same priority levels, task execution with before/after hooks, and chainable API for queue operations.
When an event is triggered, Stackpress doesn't just fire off listeners blindly. Instead, it organizes them into a TaskQueue, which then consumes items sequentially by priority. Because events can be defined anywhere, event priority allows you to structure execution like a series of steps—making the flow of your application predictable and easy to follow.
Even better, the TaskQueue makes the EventEmitter a true plugin system: you can insert new code between existing listeners without rewriting or restructuring what's already there. This means features, extensions, or third-party modules can seamlessly "hook into" the event pipeline without breaking your core logic.
const itemQueue = new ItemQueue<string>();
const taskQueue = new TaskQueue<[number]>();An item queue that orders and consumes items sequentially based on priority with FIFO ordering by default. The ItemQueue provides the foundation for priority-based processing where items with higher priority values are processed before items with lower priority values.
The following properties are available when instantiating an ItemQueue.
| Property | Type | Description |
|---|---|---|
queue |
Item<I>[] |
The internal queue array (readonly) |
size |
number |
The number of items in the queue |
The following example shows how to add items with specific priority levels for controlled execution order.
const queue = new ItemQueue<string>();
queue.add('medium', 5);
queue.add('high', 10);
queue.add('low', 1);Parameters
| Parameter | Type | Description |
|---|---|---|
item |
I |
The item to add to the queue |
priority |
number |
Priority level (higher numbers execute first, default: 0) |
Returns
The ItemQueue instance to allow method chaining.
The following example shows how to add items to the bottom of the queue with the lowest priority.
queue.push('bottom-item');Parameters
| Parameter | Type | Description |
|---|---|---|
item |
I |
The item to add to the bottom of the queue |
Returns
The ItemQueue instance to allow method chaining.
The following example shows how to add items to the top of the queue with the highest priority.
queue.shift('top-item');Parameters
| Parameter | Type | Description |
|---|---|---|
item |
I |
The item to add to the top of the queue |
Returns
The ItemQueue instance to allow method chaining.
The following example shows how to consume items one at a time in priority order.
const item = queue.consume(); // Returns highest priority item
console.log(item); // 'top-item'Returns
The next item in the queue, or undefined if the queue is empty.
A task queue that extends ItemQueue specifically for executing functions sequentially with priority-based ordering. The TaskQueue provides advanced features like before/after hooks, execution control, and status reporting for complex task management scenarios.
const queue = new TaskQueue<[number]>();
queue.push(async (x) => console.log(x + 1));
queue.shift(async (x) => console.log(x + 2));
queue.add(async (x) => console.log(x + 3), 10);
await queue.run(5);The following properties are available when instantiating a TaskQueue.
| Property | Type | Description |
|---|---|---|
after |
TaskAction<A> |
Hook called after each task execution |
before |
TaskAction<A> |
Hook called before each task execution |
queue |
Item<TaskAction<A>>[] |
The internal queue array (inherited) |
size |
number |
The number of tasks in the queue (inherited) |
The following example shows how to execute all tasks in the queue sequentially with proper error handling.
const queue = new TaskQueue<[number]>();
queue.push(async (x) => {
console.log(x + 1);
return true; // Continue execution
});
queue.add(async (x) => {
console.log(x + 2);
return false; // Abort execution
}, 10);
const result = await queue.run(5);
console.log(result.code); // 309 (ABORT) or 200 (OK)Parameters
| Parameter | Type | Description |
|---|---|---|
...args |
A |
Arguments to pass to each task function |
Returns
A promise that resolves to a ResponseStatus indicating success, abort, or not found.
The following example shows how to add tasks with specific priority levels for controlled execution order.
queue.add(async (x) => console.log('high priority', x), 10);Parameters
| Parameter | Type | Description |
|---|---|---|
item |
TaskAction<A> |
The task function to add |
priority |
number |
Priority level (higher numbers execute first, default: 0) |
Returns
The TaskQueue instance to allow method chaining.
The following example shows how to add tasks to the bottom of the queue with the lowest priority.
queue.push(async (x) => console.log('low priority', x));Parameters
| Parameter | Type | Description |
|---|---|---|
item |
TaskAction<A> |
The task function to add to the bottom |
Returns
The TaskQueue instance to allow method chaining.
The following example shows how to add tasks to the top of the queue with the highest priority.
queue.shift(async (x) => console.log('high priority', x));Parameters
| Parameter | Type | Description |
|---|---|---|
item |
TaskAction<A> |
The task function to add to the top |
Returns
The TaskQueue instance to allow method chaining.
The following example shows how to set before and after hooks for task execution monitoring and control.
queue.before = async (x) => {
console.log('Before task:', x);
return true; // Continue execution
};
queue.after = async (x) => {
console.log('After task:', x);
return true; // Continue execution
};Parameters
| Parameter | Type | Description |
|---|---|---|
...args |
A |
Arguments passed to the hook function |
Returns
For hooks: false to abort execution, any other value to continue.
The following describes the sequential execution flow when running tasks in a TaskQueue. Understanding this flow is essential for implementing proper error handling and execution control in your applications.
The task execution follows a predictable sequence that allows for monitoring and control at each stage.
- Before Hook — Called before each task (if set)
- Task Execution — The actual task function is called
- After Hook — Called after each task (if set)
If any step returns false, execution is aborted and the queue returns Status.ABORT. This provides fine-grained control over task execution flow.
- Continue Execution — Return
trueor any truthy value - Abort Execution — Return
falseto stop processing remaining tasks
The following status codes are returned by TaskQueue operations to indicate the result of task execution. These codes follow HTTP status code conventions for consistency across the Stackpress ecosystem.
- Status.OK (200) — All tasks completed successfully
- Status.NOT_FOUND (404) — No tasks in the queue
- Status.ABORT (309) — Execution was aborted by a task or hook