Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

add barrier.abort (issue #8) #15

Open
wants to merge 2 commits into from

3 participants

@fkuo

No description provided.

@davepacheco
Owner

Thanks for doing this! Can you add some tests, an example, and update README.md? My main question is what the expected behavior is when you start an operation on a barrier that's aborted? The example would help clarify that.

Also, is "drain" the right event for conveying the error?

CC'ing @jclulow, since he's interested in this.

@jclulow

If there's a different event for conveying errors, e.g. "error", then it would be good to ensure that "drain" does not ever fire in the error case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 29, 2014
  1. add barrier.abort

    Fred Kuo authored
  2. send abort's drain on next tick

    Fred Kuo authored
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 1 deletion.
  1. +27 −1 lib/vasync.js
View
28 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);
+ });
+};
Something went wrong with that request. Please try again.