diff options
author | Rebecca Turner <me@re-becca.org> | 2015-09-17 21:36:32 +0300 |
---|---|---|
committer | Kat Marchán <kzm@sykosomatic.org> | 2017-03-10 03:07:49 +0300 |
commit | bb4ddc61e88e970ab6e3177a6e76077cb4630b60 (patch) | |
tree | 94551e3c38c0357f15299ed6f7c5b1a744fe79a2 /node_modules/call-limit | |
parent | 84be534aedb78c65cd8012427fc04871ceeccf90 (diff) |
call-limit@1.1.0
Diffstat (limited to 'node_modules/call-limit')
-rw-r--r-- | node_modules/call-limit/README.md | 85 | ||||
-rw-r--r-- | node_modules/call-limit/call-limit.js | 86 | ||||
-rw-r--r-- | node_modules/call-limit/package.json | 90 |
3 files changed, 261 insertions, 0 deletions
diff --git a/node_modules/call-limit/README.md b/node_modules/call-limit/README.md new file mode 100644 index 000000000..dc4f081dc --- /dev/null +++ b/node_modules/call-limit/README.md @@ -0,0 +1,85 @@ +call-limit +---------- + +Limit the number of simultaneous executions of a async function. + +```javascript +var fs = require('fs') +var limit = require('call-limit') +var limitedStat = limit(fs.stat, 5) +``` + +Or with promise returning functions: + +```javascript +var fs = Bluebird.promisifyAll(require('fs')) +var limit = require('call-limit') +var limitedStat = limit.promise(fs.statAsync, 5) +``` + +### USAGE: + +Given that: + +```javascript +var limit = require('call-limit') +``` + +### limit(func, maxRunning) → limitedFunc + +The returned function will execute up to maxRunning calls of `func` at once. +Beyond that they get queued and called when the previous call completes. + +`func` must accept a callback as the final argument and must call it when +it completes, or `call-limit` won't know to dequeue the next thing to run. + +By contrast, callers to `limitedFunc` do NOT have to pass in a callback, but +if they do they'll be called when `func` calls its callback. + +### limit.promise(func, maxRunning) → limitedFunc + +The returned function will execute up to maxRunning calls of `func` at once. +Beyond that they get queued and called when the previous call completes. + +`func` must return a promise. + +`limitedFunc` will return a promise that resolves with the promise returned +from the call to `func`. + +### limit.method(class, methodName, maxRunning) + +This is sugar for: + +```javascript +class.prototype.methodName = limit(class.prototype.methodName, maxRunning) +``` + +### limit.method(object, methodName, maxRunning) + +This is sugar for: + +```javascript +object.methodName = limit(object.methodName, maxRunning) +``` + +For example `limit.promise.method(fs, 'stat', 5)` is the same as +`fs.stat = limit.promise(fs.stat, 5)`. + +### limit.promise.method(class, methodName, maxRunning) + +This is sugar for: + +```javascript +class.prototype.methodName = limit.promise(class.prototype.methodName, maxRunning) +``` + +### limit.promise.method(object, methodName, maxRunning) + +This is sugar for: + +```javascript +object.methodName = limit.promise(object.methodName, maxRunning) +``` + +For example `limit.promise.method(fs, 'statAsync', 5)` is the same as +`fs.statAsync = limit.promise(fs.statAsync, 5)`. diff --git a/node_modules/call-limit/call-limit.js b/node_modules/call-limit/call-limit.js new file mode 100644 index 000000000..d6e4534b8 --- /dev/null +++ b/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) + } +} diff --git a/node_modules/call-limit/package.json b/node_modules/call-limit/package.json new file mode 100644 index 000000000..631a123f1 --- /dev/null +++ b/node_modules/call-limit/package.json @@ -0,0 +1,90 @@ +{ + "_args": [ + [ + { + "raw": "call-limit@latest", + "scope": null, + "escapedName": "call-limit", + "name": "call-limit", + "rawSpec": "latest", + "spec": "latest", + "type": "tag" + }, + "/Users/rebecca/code/npm-latest" + ] + ], + "_from": "call-limit@latest", + "_id": "call-limit@1.1.0", + "_inCache": true, + "_location": "/call-limit", + "_nodeVersion": "7.7.1", + "_npmOperationalInternal": { + "host": "packages-12-west.internal.npmjs.com", + "tmp": "tmp/call-limit-1.1.0.tgz_1488849297527_0.29955350141972303" + }, + "_npmUser": { + "name": "iarna", + "email": "me@re-becca.org" + }, + "_npmVersion": "4.4.1", + "_phantomChildren": {}, + "_requested": { + "raw": "call-limit@latest", + "scope": null, + "escapedName": "call-limit", + "name": "call-limit", + "rawSpec": "latest", + "spec": "latest", + "type": "tag" + }, + "_requiredBy": [ + "#USER", + "/" + ], + "_resolved": "https://registry.npmjs.org/call-limit/-/call-limit-1.1.0.tgz", + "_shasum": "6fd61b03f3da42a2cd0ec2b60f02bd0e71991fea", + "_shrinkwrap": null, + "_spec": "call-limit@latest", + "_where": "/Users/rebecca/code/npm-latest", + "author": { + "name": "Rebecca Turner", + "email": "me@re-becca.org" + }, + "bugs": { + "url": "https://github.com/iarna/call-limit/issues" + }, + "dependencies": {}, + "description": "Limit the number of simultaneous calls to an async function", + "devDependencies": { + "tap": "^1.0.0" + }, + "directories": {}, + "dist": { + "shasum": "6fd61b03f3da42a2cd0ec2b60f02bd0e71991fea", + "tarball": "https://registry.npmjs.org/call-limit/-/call-limit-1.1.0.tgz" + }, + "files": [ + "call-limit.js" + ], + "gitHead": "2b05fe72f0cf33a2aac951cd68dd949fccb0d9e9", + "homepage": "https://npmjs.com/packages/call-limit", + "license": "ISC", + "main": "call-limit.js", + "maintainers": [ + { + "name": "iarna", + "email": "me@re-becca.org" + } + ], + "name": "call-limit", + "optionalDependencies": {}, + "readme": "ERROR: No README data found!", + "repository": { + "type": "git", + "url": "git+https://github.com/iarna/call-limit.git" + }, + "scripts": { + "test": "tap test/*.js" + }, + "version": "1.1.0" +} |