Context ![Build Status](https://travis-ci.org/silas/node-context.png?branch=master)
Context type that carries deadlines, cancelation signals, and other request-scoped values.
Influenced by Go's context.
Usage
var context = require('node-context');
function display(ctx, name) {
ctx.once('cancel', function() {
console.log('%s cancel %d (deadline %d)', name, new Date() - ctx.time, ctx.deadline);
});
ctx.once('done', function() {
console.log('%s done', name);
});
}
var main = context();
main.time = new Date();
var master = main.create({ timeout: 1000 });
display(main, 'main');
display(master, 'master');
display(master.create({ timeout: 500 }), 'worker1');
display(master.create(), 'worker2');
process.on('beforeExit', function() {
main.done();
});
Output
worker1 cancel 532 (deadline 1425278555679)
worker1 done
master cancel 1009 (deadline 1425278556175)
worker2 cancel 1010 (deadline 1425278556175)
worker2 done
master done
main done
Documentation
### context.Context([options])
Initialize a new context.
Options
- deadline (Number, optional): time when request expires (milliseconds since Unix epoch)
- timeout (Number, optional): duration in milliseconds until request expires
- parent (Context, optional): parent context; inherits deadlines, cancelation signals, and properties
### Event: 'cancel'
Emitted when the request exceeds it's deadline or is canceled.
This is only emitted once.
### Event: 'done'
Emitted when the request is done.
This is only emitted once.
### ctx.create([options])
Create and return a child context.
This accepts the same options as the context constructor and automatically sets the parent
option.
### ctx.cancel()
Cancel request immediately.
This is safe to call multiple times.
### ctx.deadline
Time in milliseconds since Unix epoch when the request will be canceled if not done.
### ctx.done()
Mark the request as done.
This should always be called when the request is finished and is safe to call multiple times.
License
This work is licensed under the MIT License (see the LICENSE file).