easy-stack

1.0.1 · active · verified Sun Apr 19

easy-stack is a JavaScript library providing a simple Last-In, First-Out (LIFO) stack data structure, designed for both Node.js and browser environments. Its distinguishing feature is `autoRun`, which allows the stack to automatically execute newly added functions if it's not currently running or has not been forcibly stopped. This makes it suitable for managing prioritized operations, such as handling socket messages, processing async/sync tasks in a specific order, or creating extendable base classes for custom stack behaviors. The current stable version is 1.0.1, released primarily to update its licensing from the DBAD Public License to MIT. The package maintains a stable, albeit infrequent, release cadence, focusing on broad compatibility across various CommonJS loaders and vanilla browser usage, targeting Node.js environments from version 6.0.0 and above.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a new easy-stack instance, adding functions to it, and the LIFO execution flow with `this.next()` for sequence control, including dynamic additions.

const Stack = require('easy-stack');

// Create a new Stack instance
const stack = new Stack();

let requestCount = 0;

// Add multiple functions to the stack
for (let i = 0; i < 3; i++) {
    stack.add(function makeRequest() {
        requestCount++;
        // Simulate an asynchronous operation
        console.log(`Processing request ${requestCount} from the stack...`);
        setTimeout(() => {
            console.log(`Request ${requestCount} completed.`);
            // Crucially, call this.next() to advance the stack.
            // This ensures the next item is processed, especially if autoRun is false or after an async step.
            this.next();
        }, 50);
    });
}

// Dynamically add a high-priority task later. It will run automatically if `autoRun` is true and the stack is not stopped.
stack.add(function dynamicTask() {
    console.log('A new high-priority task dynamically added to the stack!');
    this.next();
});

console.log('Stack initialized. Items will process in LIFO order (Last-In, First-Out).');

view raw JSON →