Comparing version 0.0.0 to 1.0.0
95
index.js
@@ -1,67 +0,46 @@ | ||
var promise = require('promises-a'); | ||
'use strict' | ||
module.exports = throttler; | ||
function throttler(concurrency) { | ||
var inProgress = 0; | ||
var queue = []; | ||
function throttle(fn) { | ||
if (typeof fn != 'function') { | ||
return reject(new TypeError(fn + ' is not a function.')); | ||
} | ||
if (idle() > 0) { | ||
var result = fn(); | ||
inProgress++; | ||
result.then(null, function () {}) | ||
.then(function () { | ||
inProgress--; | ||
if (queue.length) { | ||
queue.shift()(); | ||
} | ||
}); | ||
return result; | ||
var Promise = require('promise') | ||
module.exports = throat | ||
function throat(size, fn) { | ||
var queue = [] | ||
function run(fn, self, args) { | ||
if (size) { | ||
size-- | ||
var result = new Promise(function (resolve) { | ||
resolve(fn.apply(self, args)) | ||
}) | ||
result.done(release, release) | ||
return result | ||
} else { | ||
var def = promise(); | ||
queue.push(function () { | ||
def.fulfill(throttle(fn)); | ||
}); | ||
return def.promise; | ||
return new Promise(function (resolve) { | ||
queue.push(new Delayed(resolve, fn, self, args)) | ||
}) | ||
} | ||
} | ||
throttle.workers = workers; | ||
function workers(val) { | ||
if (typeof val === 'number') | ||
return concurrency = val; | ||
else return concurrency; | ||
function release() { | ||
size++ | ||
if (queue.length) { | ||
var next = queue.shift() | ||
next.resolve(run(next.fn, next.self, next.args)) | ||
} | ||
} | ||
throttle.running = running; | ||
function running() { | ||
return inProgress; | ||
if (typeof fn === 'function') { | ||
return function () { | ||
var args = arguments | ||
return run(fn, this, arguments) | ||
} | ||
} else { | ||
return function (fn) { | ||
return run(fn, this, Array.prototype.slice.call(arguments, 1)) | ||
} | ||
} | ||
throttle.idle = idle; | ||
function idle() { | ||
return concurrency - inProgress; | ||
} | ||
throttle.workOn = function (source, worker) { | ||
var def = promise(); | ||
function nextJob() { | ||
throttle(function () { | ||
return source() | ||
.then(function (job) { | ||
nextJob(); | ||
return worker(job); | ||
}); | ||
}) | ||
.then(null, def.reject); | ||
} | ||
nextJob(); | ||
return def.promise; | ||
}; | ||
return throttle; | ||
} | ||
function reject(reason) { | ||
var p = promise(); | ||
p.reject(reason); | ||
return p.promise; | ||
function Delayed(resolve, fn, self, args) { | ||
this.resolve = resolve | ||
this.fn = fn | ||
this.self = self || null | ||
this.args = args || null | ||
} |
{ | ||
"name": "throat", | ||
"version": "0.0.0", | ||
"description": "Throttle a collection of promise returning functions", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ForbesLindesay/throat.git" | ||
}, | ||
"version": "1.0.0", | ||
"description": "Throttle the parallelism of an asynchronous (promise returning) function / functions", | ||
"keywords": [ | ||
@@ -19,13 +11,20 @@ "promise", | ||
"concurrency", | ||
"parallelism", | ||
"limit" | ||
], | ||
"author": "ForbesLindesay", | ||
"license": "MIT", | ||
"dependencies": { | ||
"promises-a": "~2" | ||
"promise": "~3.2.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1", | ||
"better-assert": "~0.1.0" | ||
} | ||
} | ||
"mocha": "*" | ||
}, | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ForbesLindesay/throat.git" | ||
}, | ||
"author": "ForbesLindesay", | ||
"license": "MIT" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
9881
1
7
219
1
61
1
+ Addedpromise@~3.2.0
+ Addedpromise@3.2.0(transitive)
- Removedpromises-a@~2
- Removedpromises-a@2.3.0(transitive)