finally-aggregator
Advanced tools
+45
| var FinallyAggregator = require('./'); | ||
| var assert = require('assert'); | ||
| var requested_attempts = 0; | ||
| var finished_attempts = 0; | ||
| function attempt(fns, expected_args, expected_state, after) { | ||
| requested_attempts++; | ||
| var acc = new FinallyAggregator(); | ||
| var state = {}; | ||
| fns.forEach(function (fn) { | ||
| acc.add(fn.bind(state)); | ||
| }); | ||
| acc.finish(function () { | ||
| finished_attempts++; | ||
| assert.equal(JSON.stringify(expected_args), JSON.stringify([].slice.call(arguments))) | ||
| assert.equal(JSON.stringify(expected_state), JSON.stringify(state)); | ||
| if (typeof after === 'function') after(); | ||
| }); | ||
| return acc; | ||
| } | ||
| process.on('exit', function () { | ||
| assert.equal(requested_attempts, finished_attempts); | ||
| }); | ||
| function makeStateFn(state) { | ||
| return function (next) { | ||
| for (var k in state) this[k] = state[k]; | ||
| next(); | ||
| } | ||
| } | ||
| function makeErrFn(err) { | ||
| return function (next) { | ||
| throw err; | ||
| next(); | ||
| } | ||
| } | ||
| requested_attempts++; | ||
| var acc1 = attempt([], [null, []], {}, function after() { | ||
| acc1.add(function () { | ||
| finished_attempts++; | ||
| }) | ||
| }); | ||
| attempt([makeStateFn({x:1})], [null, []], {x:1}); | ||
| attempt([makeErrFn({x:1})], [null, [{x:1}]], {}); |
+9
-2
@@ -25,3 +25,4 @@ function Cleanup() { | ||
| var errs = []; | ||
| if (this.todo) for (var i = 0; i < this.todo.length; i++) { | ||
| var i = 0; | ||
| if (this.todo) for (; i < this.todo.length; i++) { | ||
| setImmediate(function (action) { | ||
@@ -45,7 +46,13 @@ var used = false; | ||
| i--; | ||
| check_done(); | ||
| } | ||
| function check_done() { | ||
| if (i == 0 && typeof all_done === 'function') { | ||
| all_done(null, errs); | ||
| setImmediate(function () { | ||
| all_done(null, errs); | ||
| }); | ||
| } | ||
| } | ||
| check_done(); | ||
| this.todo = null; | ||
| } |
+2
-2
| { | ||
| "name": "finally-aggregator", | ||
| "version": "1.2.0", | ||
| "version": "1.2.1", | ||
| "description": "", | ||
| "main": "index.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| "test": "node test.js" | ||
| }, | ||
@@ -9,0 +9,0 @@ "repository": { |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
4248
41.13%5
25%96
104.26%0
-100%