Skip to content

Commit

Permalink
Removed that confusing construct, which had been reduced to only the
Browse files Browse the repository at this point in the history
'filter' function, and adding more comments.
  • Loading branch information
David Ellis committed Oct 8, 2013
1 parent 1022fb7 commit 1d1345b
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions lib/queue-flow.js
Expand Up @@ -120,6 +120,10 @@ Q.prototype.handlerCallback = function handlerCallback(openQueueStruct, done) {
}
};

// `OpenQueueStruct` is a simple object with fixed types, which should be
// easier for V8 and other JS engines to optimize internally. `done` is
// always a boolean and `doneFunc` is always a function. V8 prefers
// constructor functions to JSON for optimization purposes.
function OpenQueueStruct() {
this.done = false;
this.doneFunc = function() {};
Expand Down Expand Up @@ -292,27 +296,6 @@ Q.prototype.wait = function wait(delay) {
Q.prototype.waitAsync = makeAsync(Q.prototype.wait);
Q.prototype.waitSync = makeSync(Q.prototype.wait);

// `inOut` is a helper function used by several of the Q prototype methods that
// take an input queue and produce an output queue.
var inOut = function inOut(outQueue, setter, callback) {
if(isAsync(callback, 2)) {
this.setHandlers(function(self, value, struct) {
callback(value, function(result) {
self.handlerCallback(struct, setter.bind(this, value, result));
});
}, function(end) {
outQueue[end]();
});
} else {
this.setHandlers(function(self, value, struct) {
self.handlerCallback(struct, setter.bind(this, value, callback(value)));
}, function(end) {
outQueue[end]();
});
}
return outQueue;
};

// `map` creates an output queue, and executes
// the given callback on each value, pushing the
// result into the output queue before continuing
Expand Down Expand Up @@ -387,10 +370,28 @@ Q.prototype.reduceSync = makeSync(Q.prototype.reduce);
// original value *only* if the callback returns true.
Q.prototype.filter = function filter(callback) {
var outQueue = new Q(null, this.options, this.namespace);
return inOut.bind(this)(outQueue, function filterSetter(value, result) {
if(result) outQueue.push(value);
}, callback);
if(isAsync(callback, 2)) {
this.setHandlers(function(self, value, struct) {
callback(value, function(result) {
self.handlerCallback(struct, function() {
if(result) outQueue.push(value);
});
});
}, function(end) {
outQueue[end]();
});
} else {
this.setHandlers(function(self, value, struct) {
self.handlerCallback(struct, function() {
if(callback(value)) outQueue.push(value);
});
}, function(end) {
outQueue[end]();
});
}
return outQueue;
};

Q.prototype.filterAsync = makeAsync(Q.prototype.filter);
Q.prototype.filterSync = makeSync(Q.prototype.filter);

Expand Down

0 comments on commit 1d1345b

Please sign in to comment.