Skip to content

Commit

Permalink
Cleaning up the namespace mechanism, eliminating unnecessary this bin…
Browse files Browse the repository at this point in the history
…dings, etc.
  • Loading branch information
David Ellis committed Oct 8, 2013
1 parent 55c337e commit 07945d1
Showing 1 changed file with 48 additions and 66 deletions.
114 changes: 48 additions & 66 deletions lib/queue-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,6 @@ var nextTick = global.setImmediate ? global.setImmediate : process.nextTick;

var q;

// `exists` returns whether or not a named queue exists in
function exists(queueName) {
return !!this.namedQueues[queueName] && typeof(this.namedQueues[queueName]) === 'object';
}

// `clearQueue` removes a queue from a q environment
function clearQueue(nameOrQueue) {
if(typeof(nameOrQueue) === 'string') {
if(this.namedQueues[nameOrQueue]) delete this.namedQueues[nameOrQueue];
} else {
Object.keys(this.namedQueues).forEach(function(name) {
if(this.namedQueues[name] === nameOrQueue) delete this.namedQueues[name];
}.bind(this));
}
}

// `addQueue` adds a queue to a q environment
function addQueue(name, queue) {
this.namedQueues[name] = queue;
}

// `makeAsync` and `makeSync` helper methods allow a more fluent API to force
// async or sync methods, suggested by a colleague, and fits better in the
// `queue-flow` API.
Expand All @@ -73,13 +52,6 @@ function makeSync(qfMethod) {
};
}

// `tuple` converts an object into an array of arrays. The arrays are tuples of the
// key-value pairs from the object, so they can be processed individually in a queue-flow
// if desired, rather than considering the whole object as a single item in the queue.
function tuple(obj) {
return Object.keys(obj).reduce(function(outArr, key) { return outArr.concat([[key, obj[key]]]); }, []);
}

// ## The `Q` constructor function, which either uses the supplied queueing engine, or
// uses the built-in in-memory engine.
function Q(nameOrArray, options, namespace) {
Expand Down Expand Up @@ -763,45 +735,59 @@ Q.prototype.drain = function drain() {
return undefined;
};

// Determines if this queue has a name, and either finds or returns said named queue,
// or decides it's an unnamed queue and returns said queue.
function qFunc(nameOrArray, options) {
options = options || {};
if(typeof(nameOrArray) === "string") {
if(!this.namedQueues[nameOrArray]) {
this.namedQueues[nameOrArray] = new Q(nameOrArray, options, q);
}
return this.namedQueues[nameOrArray];
} else if(nameOrArray instanceof Array) {
return new Q(nameOrArray, options, q);
} else if(nameOrArray instanceof Object &&
typeof(nameOrArray.pipe) === 'function' &&
typeof(nameOrArray.on) === 'function') {
var newQ = new Q(undefined, options, q);
nameOrArray.on('data', newQ.push.bind(newQ));
nameOrArray.on('end', newQ.close.bind(newQ));
return newQ;
} else {
return new Q(undefined, options, q);
}
}

function ns() {
// The q environment
var qEnv = {};

var q = qFunc.bind(qEnv);

qEnv.q = q;
var namedQueues = {};

// Determines if this queue has a name, and either finds or returns said named queue,
// or decides it's an unnamed queue and returns said queue.
var q = function qFunc(nameOrArray, options) {
options = options || {};
if(typeof(nameOrArray) === "string") {
if(!namedQueues[nameOrArray]) {
namedQueues[nameOrArray] = new Q(nameOrArray, options, q);
}
return namedQueues[nameOrArray];
} else if(nameOrArray instanceof Array) {
return new Q(nameOrArray, options, q);
} else if(nameOrArray instanceof Object &&
typeof(nameOrArray.pipe) === 'function' &&
typeof(nameOrArray.on) === 'function') {
var newQ = new Q(undefined, options, q);
nameOrArray.on('data', newQ.push.bind(newQ));
nameOrArray.on('end', newQ.close.bind(newQ));
return newQ;
} else {
return new Q(undefined, options, q);
}
};

// Hash of named queues
qEnv.namedQueues = {};
// `exists` returns whether or not a named queue exists in
q.exists = function exists(queueName) {
return !!namedQueues[queueName] && typeof(namedQueues[queueName]) === 'object';
};

q.exists = exists.bind(qEnv);
// `clearQueue` removes a queue from a q environment
q.clearQueue = function clearQueue(nameOrQueue) {
if(typeof(nameOrQueue) === 'string') {
if(namedQueues[nameOrQueue]) delete namedQueues[nameOrQueue];
} else {
Object.keys(namedQueues).forEach(function(name) {
if(namedQueues[name] === nameOrQueue) delete namedQueues[name];
});
}
};

q.clearQueue = clearQueue.bind(qEnv);
// `addQueue` adds a queue to a q environment
q.addQueue = function addQueue(name, queue) {
namedQueues[name] = queue;
};

q.addQueue = addQueue.bind(qEnv);
// `tuple` converts an object into an array of arrays. The arrays are tuples of the
// key-value pairs from the object, so they can be processed individually in a queue-flow
// if desired, rather than considering the whole object as a single item in the queue.
q.tuple = function tuple(obj) {
return Object.keys(obj).reduce(function(outArr, key) { return outArr.concat([[key, obj[key]]]); }, []);
};

// Create a new queue-flow environment/namespace
q.ns = ns;
Expand All @@ -810,13 +796,9 @@ function ns() {
// Expose the `Q` constructor function (below) so third parties can extend its prototype
q.Q = Q;

// Specify the default constructor function for this namespace (may be overridden by user)
q.defaultQ = Q;

// ## Methods of `q`, mostly helper functions for users of `queue-flow` and the `Q` methods
q.makeAsync = makeAsync;
q.makeSync = makeSync;
q.tuple = tuple;

return q;
}
Expand Down

0 comments on commit 07945d1

Please sign in to comment.