easy-stack
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
-
ReferenceError: require is not defined
cause Attempting to use `require()` in a plain browser environment without a CommonJS bundler like Webpack or Browserify.fixFor browser usage, include `es5.js` via a `<script>` tag (e.g., `<script src='./node_modules/easy-stack/es5.js'></script>`) to make `Stack` globally available. -
TypeError: Cannot read properties of undefined (reading 'next')
cause This typically occurs when a function added to the stack (`stack.add(myFunction)`) loses its `this` context, preventing access to `this.next()`. This can happen with arrow functions or improperly bound functions.fixEnsure functions passed to `stack.add` are regular functions or are explicitly bound to the stack instance if `this` context is critical (though `easy-stack` usually handles this correctly internally for regular functions). -
TypeError: stack.stop is not a function
cause Incorrectly attempting to call the `stop` property as a method, when it is a boolean property.fixTo stop or resume the stack, assign a boolean value directly: `stack.stop = true;` (to stop) or `stack.stop = false;` (to resume).
Warnings
- breaking The license for easy-stack changed from the DBAD Public License to the MIT License. While not a code-breaking change, this alters the legal terms of use and distribution.
- gotcha The `stop` property is a boolean flag (`stack.stop = true;`) to halt execution, not a method (`stack.stop();`). Attempting to call it as a function will result in a TypeError.
- gotcha easy-stack is fundamentally a CommonJS module. Direct use of ES Modules `import` syntax (`import Stack from 'easy-stack';`) will fail in Node.js environments without a build step or explicit configuration for CJS-ESM interop.
- gotcha The package targets Node.js version 6.0.0 and above. This is an older Node.js version, which means the package may not fully leverage or be tested against modern JavaScript features or security practices available in newer Node.js releases.
- gotcha Functions added to the stack (`stack.add(myFunction)`) must call `this.next()` internally to proceed to the next item in the stack. Failure to do so will halt stack execution prematurely, especially for asynchronous operations.
Install
-
npm install easy-stack -
yarn add easy-stack -
pnpm add easy-stack
Imports
- Stack
import { Stack } from 'easy-stack'; import Stack from 'easy-stack';const Stack = require('easy-stack'); - Stack (ES5)
const Stack = require('easy-stack/es5.js'); - Global Stack (Browser)
require('easy-stack'); // Fails in browser without a CommonJS bundler<script src="./node_modules/easy-stack/es5.js"></script> <script> // Stack is now available globally const stack = new Stack(); </script>
Quickstart
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).');