aboutsummaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/call-limit/call-limit.js
diff options
context:
space:
mode:
authorRebecca Turner <me@re-becca.org>2017-04-12 21:47:49 -0700
committerJeremiah Senkpiel <fishrock123@rocketmail.com>2017-04-25 10:52:01 -0400
commit00842604483e4c2e622dfdb3a97440e07646158f (patch)
treef3346902636a44b6037652523767636bf7e4f2c9 /deps/npm/node_modules/call-limit/call-limit.js
parent061c5da010e0d249379618382a499840d38247b8 (diff)
downloadandroid-node-v8-00842604483e4c2e622dfdb3a97440e07646158f.tar.gz
android-node-v8-00842604483e4c2e622dfdb3a97440e07646158f.tar.bz2
android-node-v8-00842604483e4c2e622dfdb3a97440e07646158f.zip
deps: upgrade npm to 4.5.0
PR-URL: https://github.com/nodejs/node/pull/12480 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Diffstat (limited to 'deps/npm/node_modules/call-limit/call-limit.js')
-rw-r--r--deps/npm/node_modules/call-limit/call-limit.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/deps/npm/node_modules/call-limit/call-limit.js b/deps/npm/node_modules/call-limit/call-limit.js
new file mode 100644
index 0000000000..d6e4534b8a
--- /dev/null
+++ b/deps/npm/node_modules/call-limit/call-limit.js
@@ -0,0 +1,86 @@
+"use strict"
+
+var defaultMaxRunning = 50
+
+var limit = module.exports = function (func, maxRunning) {
+ var running = 0
+ var queue = []
+ if (!maxRunning) maxRunning = defaultMaxRunning
+ return function limited () {
+ var self = this
+ var args = Array.prototype.slice.call(arguments)
+ if (running >= maxRunning) {
+ queue.push({self: this, args: args})
+ return
+ }
+ var cb = typeof args[args.length-1] === 'function' && args.pop()
+ ++ running
+ args.push(function () {
+ var cbargs = arguments
+ -- running
+ cb && process.nextTick(function () {
+ cb.apply(self, cbargs)
+ })
+ if (queue.length) {
+ var next = queue.shift()
+ limited.apply(next.self, next.args)
+ }
+ })
+ func.apply(self, args)
+ }
+}
+
+module.exports.method = function (classOrObj, method, maxRunning) {
+ if (typeof classOrObj === 'function') {
+ var func = classOrObj.prototype[method]
+ classOrObj.prototype[method] = limit(func, maxRunning)
+ } else {
+ var func = classOrObj[method]
+ classOrObj[method] = limit(func, maxRunning)
+ }
+}
+
+module.exports.promise = function (func, maxRunning) {
+ var running = 0
+ var queue = []
+ if (!maxRunning) maxRunning = defaultMaxRunning
+ return function () {
+ var self = this
+ var args = Array.prototype.slice.call(arguments)
+ return new Promise(function (resolve) {
+ if (running >= maxRunning) {
+ queue.push({self: self, args: args, resolve: resolve})
+ return
+ } else {
+ runNext(self, args, resolve)
+ }
+ function runNext (self, args, resolve) {
+ ++ running
+ resolve(
+ func.apply(self, args)
+ .then(finish, function (err) {
+ finish(err)
+ throw err
+ }))
+ }
+
+ function finish () {
+ -- running
+ if (queue.length) {
+ var next = queue.shift()
+ process.nextTick(runNext, next.self, next.args, next.resolve)
+ }
+ }
+ })
+ }
+}
+
+module.exports.promise.method = function (classOrObj, method, maxRunning) {
+ if (typeof classOrObj === 'function') {
+ var func = classOrObj.prototype[method]
+ classOrObj.prototype[method] = limit.promise(func, maxRunning)
+ } else {
+ var func = classOrObj[method]
+ classOrObj[method] = limit.promise(func, maxRunning)
+ }
+}