Comparing version 0.0.4 to 1.0.0
@@ -5,3 +5,3 @@ { | ||
"description": "queue up async function calls without the overhead", | ||
"version": "0.0.4", | ||
"version": "1.0.0", | ||
"keywords": [], | ||
@@ -8,0 +8,0 @@ "dependencies": {}, |
1.0.0 / 2015-03-19 | ||
================== | ||
* BREAKING: 2nd arg is now an options object, not a number for concurrency | ||
* fix: queued items without a callback | ||
* add: timeout support. | ||
* update: .gitignore | ||
0.0.4 / 2014-08-31 | ||
@@ -3,0 +11,0 @@ ================== |
60
index.js
@@ -5,3 +5,4 @@ /** | ||
var slice = [].slice; | ||
var sliced = require('sliced'); | ||
var noop = function(){}; | ||
@@ -18,7 +19,10 @@ /** | ||
* @param {Function} fn | ||
* @param {Number} concurrency | ||
* @param {Object} options | ||
*/ | ||
function enqueue(fn, concurrency) { | ||
concurrency = concurrency || 1; | ||
function enqueue(fn, options) { | ||
options = options || {}; | ||
var concurrency = options.concurrency || 1; | ||
var timeout = options.timeout || false; | ||
var pending = 1; | ||
@@ -28,13 +32,10 @@ var jobs = []; | ||
return function() { | ||
var args = slice.call(arguments); | ||
var args = sliced(arguments); | ||
var last = args[args.length - 1]; | ||
var cb = 'function' == typeof last && last; | ||
var end = 'function' == typeof last && last; | ||
var ctx = this; | ||
// replace original callback with done | ||
if (cb) { | ||
args.pop(); | ||
args.push(done); | ||
} | ||
jobs.push([fn, this, args]); | ||
// remove "on end" function if there is one | ||
end = end ? args.pop() : noop; | ||
jobs.push([ctx, args.concat(once(done))]); | ||
return next(); | ||
@@ -46,12 +47,39 @@ | ||
if (!job) return; | ||
var ctx = job[0]; | ||
var args = job[1]; | ||
var finish = args[args.length - 1]; | ||
pending++; | ||
return job[0].apply(job[1], job[2]); | ||
// support timeouts | ||
if (timeout) { | ||
setTimeout(function() { | ||
finish(new Error('job timed out')) | ||
}, timeout); | ||
} | ||
// call the fn | ||
return fn.apply(job[0], job[1]); | ||
} | ||
function done(fn) { | ||
function done() { | ||
pending--; | ||
next(); | ||
return cb.apply(this, arguments); | ||
return end.apply(this, arguments); | ||
} | ||
} | ||
} | ||
/** | ||
* Once | ||
*/ | ||
function once(fn) { | ||
var called = false; | ||
return function _once() { | ||
if (called) return noop(); | ||
called = true; | ||
return fn.apply(this, arguments); | ||
} | ||
} |
{ | ||
"name": "enqueue", | ||
"version": "0.0.4", | ||
"version": "1.0.0", | ||
"description": "queue up function calls", | ||
@@ -11,3 +11,5 @@ "keywords": [], | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"sliced": "0.0.5" | ||
}, | ||
"devDependencies": { | ||
@@ -14,0 +16,0 @@ "mocha": "*" |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
43816
27
578
1
0
1
+ Addedsliced@0.0.5
+ Addedsliced@0.0.5(transitive)