node-context
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -27,7 +27,2 @@ /** | ||
self.done = false; | ||
// ensure we cleanup context when done | ||
self.once('done', function() { self.__done(); }); | ||
var now = +new Date(); | ||
@@ -45,3 +40,4 @@ | ||
if (parent) { | ||
parent.once('done', function() { self.__cancel(); }); | ||
parent.once('cancel', function() { self.cancel(); }); | ||
parent.once('done', function() { self.done(); }); | ||
@@ -54,6 +50,6 @@ utils.copyEvent(parent, this); | ||
} else { | ||
self.__timeout(now); | ||
self._timeout(now); | ||
} | ||
} else if (self.deadline) { | ||
self.__timeout(now); | ||
self._timeout(now); | ||
} | ||
@@ -65,15 +61,2 @@ } | ||
/** | ||
* Cancel | ||
*/ | ||
Context.prototype.cancel = Context.prototype.__cancel = function() { | ||
if (this.done) return false; | ||
this.done = true; | ||
this.emit('done'); | ||
return true; | ||
}; | ||
/** | ||
* Create child context | ||
@@ -89,11 +72,35 @@ */ | ||
/** | ||
* Cancel | ||
*/ | ||
Context.prototype.cancel = function() { | ||
if (this._cancel || this._done) return false; | ||
this._cancel = true; | ||
this.emit('cancel'); | ||
return this.done(); | ||
}; | ||
/** | ||
* Request done, cleanup context | ||
*/ | ||
Context.prototype.__done = function() { | ||
// cancel deadline timeout | ||
if (this.__timeoutId) clearTimeout(this.__timeoutId); | ||
Context.prototype.done = function() { | ||
var self = this; | ||
// cleanup references | ||
this.removeAllListeners(); | ||
process.nextTick(function() { | ||
// cancel deadline timeout | ||
if (self._timeoutId) clearTimeout(self._timeoutId); | ||
// cleanup references | ||
self.removeAllListeners(); | ||
}); | ||
if (self._done) return false; | ||
self._done = true; | ||
self.emit('done'); | ||
return true; | ||
}; | ||
@@ -105,9 +112,9 @@ | ||
Context.prototype.__timeout = function(now) { | ||
Context.prototype._timeout = function(now) { | ||
var self = this; | ||
var timeout = function() { self.__cancel(); }; | ||
var timeout = function() { self.cancel(); }; | ||
if (self.deadline > now) { | ||
self.__timeoutId = setTimeout(timeout, self.deadline - now); | ||
self._timeoutId = setTimeout(timeout, self.deadline - now); | ||
} else { | ||
@@ -114,0 +121,0 @@ process.nextTick(timeout); |
{ | ||
"name": "node-context", | ||
"version": "0.2.0", | ||
"description": "Context type that carries deadlines and cancelation signals.", | ||
"version": "0.3.0", | ||
"description": "Context type that carries deadlines, cancelation signals, and other request-scoped values.", | ||
"main": "lib", | ||
@@ -6,0 +6,0 @@ "devDependencies": { |
@@ -12,21 +12,25 @@ # Context [![Build Status](https://travis-ci.org/silas/node-context.png?branch=master)](https://travis-ci.org/silas/node-context) | ||
function work(ctx, name) { | ||
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(name + ' done %d (deadline %d)', new Date() - ctx.time, ctx.deadline); | ||
console.log('%s done', name); | ||
}); | ||
} | ||
function main() { | ||
var ctx = context({ timeout: 1000 }); | ||
ctx.time = new Date(); | ||
var main = context(); | ||
main.time = new Date(); | ||
ctx.once('done', function() { | ||
console.log('main done %d (deadline %d)', new Date() - ctx.time, ctx.deadline); | ||
}); | ||
var master = main.create({ timeout: 1000 }); | ||
work(ctx.create({ timeout: 500 }), 'work1'); | ||
work(ctx.create(), 'work2'); | ||
} | ||
display(main, 'main'); | ||
display(master, 'master'); | ||
display(master.create({ timeout: 500 }), 'worker1'); | ||
display(master.create(), 'worker2'); | ||
main(); | ||
process.on('beforeExit', function() { | ||
main.done(); | ||
}); | ||
``` | ||
@@ -37,5 +41,9 @@ | ||
``` | ||
work1 done 524 (deadline 1424550908411) | ||
main done 1002 (deadline 1424550908908) | ||
work2 done 1005 (deadline 1424550908908) | ||
worker1 cancel 532 (deadline 1425278555679) | ||
worker1 done | ||
master cancel 1009 (deadline 1425278556175) | ||
worker2 cancel 1010 (deadline 1425278556175) | ||
worker2 done | ||
master done | ||
main done | ||
``` | ||
@@ -56,4 +64,4 @@ | ||
<a name="context-event-done"/> | ||
### Event: 'done' | ||
<a name="context-event-cancel"/> | ||
### Event: 'cancel' | ||
@@ -64,8 +72,8 @@ Emitted when the request exceeds it's deadline or is canceled. | ||
<a name="context-cancel"/> | ||
### ctx.cancel() | ||
<a name="context-event-done"/> | ||
### Event: 'done' | ||
Cancel/finish request immediately. | ||
Emitted when the request is done. | ||
This should always be called when the request is finished and is safe to call multiple times. | ||
This is only emitted once. | ||
@@ -79,9 +87,23 @@ <a name="context-create"/> | ||
<a name="context-cancel"/> | ||
### ctx.cancel() | ||
Cancel request immediately. | ||
This is safe to call multiple times. | ||
<a name="context-deadline"/> | ||
### ctx.deadline | ||
Number of milliseconds since Unix epoch. | ||
Time in milliseconds since Unix epoch when the request will be canceled if not done. | ||
<a name="context-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). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7265
124
105
0