diff --git a/lib/vasync.js b/lib/vasync.js index 00bfdbc..fe6f501 100644 --- a/lib/vasync.js +++ b/lib/vasync.js @@ -434,6 +434,7 @@ function Barrier(args) this.recent = []; } + this.aborted = {}; this.pending = {}; this.scheduled = false; } @@ -444,14 +445,22 @@ Barrier.prototype.start = function (name) { mod_assert.ok(!this.pending.hasOwnProperty(name), 'operation "' + name + '" is already pending'); + + if (this.aborted.hasOwnProperty(name)) + delete this.aborted[name]; + this.pending[name] = Date.now(); }; Barrier.prototype.done = function (name) { - mod_assert.ok(this.pending.hasOwnProperty(name), + mod_assert.ok(this.pending.hasOwnProperty(name) || + this.aborted.hasOwnProperty(name), 'operation "' + name + '" is not pending'); + if (this.aborted.hasOwnProperty(name)) + return; + if (this.recent) { this.recent.push({ 'name': name, @@ -494,3 +503,20 @@ Barrier.prototype.done = function (name) self.emit('drain'); }); }; + +Barrier.prototype.abort = function (err) +{ + var self = this; + + var now = Date.now(); + Object.keys(this.pending).forEach(function (name) { + self.aborted[name] = now; + }); + this.pending = {}; + this.scheduled = true; + + setImmediate(function () { + self.scheduled = false; + self.emit('drain', err); + }); +};