stact
Manage a sorted stack of functions and execute them with flow control.
Example
Imagine you want to validate a model before saving it. Your validation handlers could be built up as a stack.
var validators = ; var model = id: 'E48Hy' email: '123@abc.com' name: 'Brian' color: '#2233ff'; // Email is required.validators; // Lookup name in a DB and verify it.validators; // Color should be a valid hex color.validators; // Run the validators (in parallel).validators;
API
Create a stact
This module exports a single factory function for creating stact stacks. It is called like:
var createStact = ;var stack = ;
options
- All stac options are supported.
- func - If set, every item in the stack will run with this function. See Example
- funcProp - If set, stact will assume that items are objects and that the function to run will be found under this property.
- getFunc - If set, stact will call this function for each item, passing the item to it. It should return the function to run for the item.
Add functions to the stack
Add functions to the stack using the API of stac.
The last argument of the function MUST always be a continuation callback.
stack;
All of stac's API is supported ...
... such as weighting your stack:
stack;stack;stack;
... or prioritizing with first() and last():
stack;stack; stack;stack; stack;
Create a stack that revolves around one function.
In some cases you want to call the same function multiple times with different information.
var createStact = ;var stack = ; stack;stack;stack; stack;
stack.run ( [arguments ...], callback )
Run the stack (in parallel), passing arbitray arguments to the functions. Results will be in sorted stack order.
Any error will cause the provided callback to be immediately invoked with the error, but other asychronous callbacks may continue to run in the background. Your callback will only be called once.
stack;
stack.runSeries ( [arguments ...], callback )
Run the stack in series, passing arbitrary arguments to the functions. Results will be in sorted stack order.
Any error will cause the run to immediately end, invoking the provided callback with the error.
stack;
stack.runWaterfall ( [arguments ...], callback )
Run the stack in a 'waterfall'. In this mode each function in the stack will
be calling the next function in the stack, with the last stack item calling
the final callback provided in the stack.runWaterfall()
call.
The arguments can change at any-time, but the most common use-case is probably passing one argument that is being modified by the stack, and other arguments providing supplementary data.
Any error will cause the run to immediately end, invoking the provided callback with the error.
Example with changing arguments
stack; stack; stack; stack;
Example passing along something to be modified
stack; stack; stack; stack;
Terra Eclipse
Developed byTerra Eclipse, Inc. is a nationally recognized political technology and strategy firm located in Aptos, CA and Washington, D.C.
License: MIT
Copyright (C) 2013 Terra Eclipse, Inc. (http://www.terraeclipse.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.