freeform
v0.2.3
Published
a small asynchronous task order manager
Downloads
22
Maintainers
Readme
freeform
A small asynchronous task order manager, primarily for decoupled shutdown handling. This involves a mechanism to define a losely ordered sequence of potentially asynchronous tasks.
Test
Run any of the following:
$ mocha
$ npm test
$ make test
Note: remember to npm install
!
Install
$ npm install freeform
API
// create function optionally takes an options object
var freeform = require('freeform');
var form = freeform({
// default; whether to use the name of the function to the put method if no
// string name specified
functionName: false,
// whether to collect errors in an object or immediately interrupt the process
// when an error occurs (default)
collectErrors: false
});
// just print text instead of running a task
function print(text) {
return function(done) {
console.log(text);
done && done();
};
}
form.put('redis', function(done) {
client.quit(done);
});
form.before('redis', print('stopping redis'));
form.after('redis', print('redis stopped'));
form.put('unsubscribe', function(done) {
client.unsubscribe();
done();
}).goBefore('redis');
// print doesn't actually get a done() callback
form.run(print('all done'));
// runs synchronously because individual tasks are synchronous
put([name], [fn])
Registers a new named task. Returns a named Node.
// define a hello task which doesn't actually do anything
form.put('hello');
// feel free to add a handler to an existing task, add as many as you want
form.put('hello', function(done) {
// process some potentially asynchronous task
// call done when completed
});
// define an anonymous task
form.put(function(done) {
// ...
}).;
before(name|node, [fn])
Registers a new anonymous task which will run before the specified task. Returns an anonymous Node.
var fin = form.put('finally');
// these are functionally equivalent, if not very useful
form.before('finally');
form.before(fin);
// these are functionally equivalent
form.before('finally', function(done) {
// ...
});
form.before(fin, function(done) {
// ...
});
after(name|node, [fn])
Registers a new anonymous task which will run after the specified task. Returns an anonymous Node.
var begin = form.put('begin');
// these are functionally equivalent, if not very useful
form.after('begin');
form.after(begin);
// these are functionally equivalent
form.after('begin', function(done) {
// ...
});
form.after(begin, function(done) {
// ...
});
run(complete)
Runs the contained tasks in-order, call the complete
callback once finished. The system cannot be run multiple times concurrently. Call reset()
to allow the system to run again.
function wait(time) {
return function(done) {
setTimeout(done, time);
};
}
// each task "takes" one second
var second = wait(1000);
form.put('a', second); // not strictly necessary
form.put('b', second).after('a');
form.put('alpha', second).before('beta');
form.put('beta', second); // not strictly necessary
form.run(function() {
// all done after 2000ms
console.log('all done');
});
reset()
Resets the statuses of all tasks and the system as a whole.
node.goBefore(name|node)
Specifies that the node should run before the named node.
Aliased as node.before
.
node.goAfter(name|node)
Specifies that the node should run after the named node.
Aliased as node.after
.
License
The MIT License (MIT)
Copyright © 2013 GlobeSherpa
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.