Added logging, changed some directory structure

This commit is contained in:
2018-01-13 21:33:40 -05:00
parent f079a5f067
commit 8e72ffb917
73656 changed files with 35284 additions and 53718 deletions

View File

@@ -0,0 +1,19 @@
Copyright (c) 2013 Forbes Lindesay
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,75 @@
# throat
Throttle the parallelism of an asynchronous, promise returning, function / functions. This has special utility when you set the concurrency to `1`. That way you get a mutually exclusive lock.
[![Build Status](https://img.shields.io/travis/ForbesLindesay/throat/master.svg)](https://travis-ci.org/ForbesLindesay/throat)
[![Coverage Status](https://img.shields.io/coveralls/ForbesLindesay/throat/master.svg?style=flat)](https://coveralls.io/r/ForbesLindesay/throat?branch=master)
[![Dependency Status](https://img.shields.io/david/ForbesLindesay/throat.svg)](https://david-dm.org/ForbesLindesay/throat)
[![NPM version](https://img.shields.io/npm/v/throat.svg)](https://www.npmjs.com/package/throat)
[![Greenkeeper badge](https://badges.greenkeeper.io/ForbesLindesay/throat.svg)](https://greenkeeper.io/)
[![Sauce Test Status](https://saucelabs.com/browser-matrix/throat.svg)](https://saucelabs.com/u/throat)
## Installation
npm install throat
## API
### throat(concurrency)
This returns a function that acts a bit like a lock (exactly as a lock if concurrency is 1).
Example, only 2 of the following functions will execute at any one time:
```js
// with polyfill or in iojs
require('promise/polyfill')
var throat = require('throat')(2)
// alternatively provide your own promise implementation
var throat = require('throat')(require('promise'))(2)
var resA = throat(function () {
//async stuff
return promise
})
var resA = throat(function () {
//async stuff
return promise
})
var resA = throat(function () {
//async stuff
return promise
})
var resA = throat(function () {
//async stuff
return promise
})
var resA = throat(function () {
//async stuff
return promise
})
```
### throat(concurrency, worker)
This returns a function that is an exact copy of `worker` except that it will only execute up to `concurrency` times in parallel before further requests are queued:
```js
// with polyfill or in iojs
require('promise/polyfill')
var throat = require('throat')
// alternatively provide your own promise implementation
var throat = require('throat')(require('promise'))
var input = ['fileA.txt', 'fileB.txt', 'fileC.txt', 'fileD.txt']
var data = Promise.all(input.map(throat(2, function (fileName) {
return readFile(fileName)
})))
```
Only 2 files will be read at a time, sometimes limiting parallelism in this way can improve scalability.
## License
MIT

View File

@@ -0,0 +1,5 @@
declare function throat<TResult, TFn extends (...args: Array<any>) => Promise<TResult>>(size: number, fn: TFn): TFn;
declare function throat<TResult, TFn extends (...args: Array<any>) => Promise<TResult>>(fn: TFn, size: number): TFn;
declare function throat(size: number): <TResult>(fn: () => Promise<TResult>) => Promise<TResult>;
export = throat;

View File

@@ -0,0 +1,112 @@
'use strict';
module.exports = function (PromiseArgument) {
var Promise;
function throat(size, fn) {
var queue = new Queue();
function run(fn, self, args) {
if (size) {
size--;
var result = new Promise(function (resolve) {
resolve(fn.apply(self, args));
});
result.then(release, release);
return result;
} else {
return new Promise(function (resolve) {
queue.push(new Delayed(resolve, fn, self, args));
});
}
}
function release() {
size++;
if (!queue.isEmpty()) {
var next = queue.shift();
next.resolve(run(next.fn, next.self, next.args));
}
}
if (typeof size === 'function') {
var temp = fn;
fn = size;
size = temp;
}
if (typeof size !== 'number') {
throw new TypeError(
'Expected throat size to be a number but got ' + typeof size
);
}
if (fn !== undefined && typeof fn !== 'function') {
throw new TypeError(
'Expected throat fn to be a function but got ' + typeof fn
);
}
if (typeof fn === 'function') {
return function () {
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
return run(fn, this, args);
};
} else {
return function (fn) {
if (typeof fn !== 'function') {
throw new TypeError(
'Expected throat fn to be a function but got ' + typeof fn
);
}
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
return run(fn, this, args);
};
}
}
if (arguments.length === 1 && typeof PromiseArgument === 'function') {
Promise = PromiseArgument;
return throat;
} else {
Promise = module.exports.Promise;
if (typeof Promise !== 'function') {
throw new Error(
'You must provide a Promise polyfill for this library to work in older environments'
);
}
return throat(arguments[0], arguments[1]);
}
};
/* istanbul ignore next */
if (typeof Promise === 'function') {
module.exports.Promise = Promise;
}
function Delayed(resolve, fn, self, args) {
this.resolve = resolve;
this.fn = fn;
this.self = self || null;
this.args = args;
}
function Queue() {
this._s1 = [];
this._s2 = [];
}
Queue.prototype.push = function (value) {
this._s1.push(value);
};
Queue.prototype.shift = function () {
if (!this._s2.length) {
while (this._s1.length) {
this._s2.push(this._s1.pop());
}
}
return this._s2.pop();
};
Queue.prototype.isEmpty = function () {
return !this._s1.length && !this._s2.length;
};

View File

@@ -0,0 +1,7 @@
// @flow
declare function throat<TResult, TFn: (...args: Array<any>) => Promise<TResult>>(size: number, fn: TFn): TFn;
declare function throat<TResult, TFn: (...args: Array<any>) => Promise<TResult>>(fn: TFn, size: number): TFn;
declare function throat(size: number): <TResult>(fn: () => Promise<TResult>) => Promise<TResult>;
module.exports = throat;

View File

@@ -0,0 +1,78 @@
{
"_args": [
[
"throat@3.2.0",
"C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project"
]
],
"_from": "throat@3.2.0",
"_id": "throat@3.2.0",
"_inBundle": false,
"_integrity": "sha512-/EY8VpvlqJ+sFtLPeOgc8Pl7kQVOWv0woD87KTXVHPIAE842FGT+rokxIhe8xIUP1cfgrkt0as0vDLjDiMtr8w==",
"_location": "/react-scripts/throat",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "throat@3.2.0",
"name": "throat",
"escapedName": "throat",
"rawSpec": "3.2.0",
"saveSpec": null,
"fetchSpec": "3.2.0"
},
"_requiredBy": [
"/react-scripts/jest/jest-cli"
],
"_resolved": "https://registry.npmjs.org/throat/-/throat-3.2.0.tgz",
"_spec": "3.2.0",
"_where": "C:\\Users\\deranjer\\go\\src\\github.com\\deranjer\\goTorrent\\torrent-project",
"author": {
"name": "ForbesLindesay"
},
"bugs": {
"url": "https://github.com/ForbesLindesay/throat/issues"
},
"description": "Throttle the parallelism of an asynchronous (promise returning) function / functions",
"devDependencies": {
"coveralls": "^2.11.2",
"flow-bin": "^0.48.0",
"istanbul": "^0.4.5",
"jest": "^20.0.4",
"promise": "^7.1.1",
"sauce-test": "^1.0.0",
"test-result": "^2.0.0",
"testit": "^2.1.3",
"typescript": "^2.3.4"
},
"files": [
"index.d.ts",
"index.js",
"index.js.flow"
],
"homepage": "https://github.com/ForbesLindesay/throat#readme",
"keywords": [
"promise",
"aplus",
"then",
"throttle",
"concurrency",
"parallelism",
"limit"
],
"license": "MIT",
"name": "throat",
"repository": {
"type": "git",
"url": "git+https://github.com/ForbesLindesay/throat.git"
},
"scripts": {
"coverage": "istanbul cover test/index.js",
"coveralls": "npm run coverage && cat ./coverage/lcov.info | coveralls",
"flow": "flow",
"test": "node test/index.js && npm run test:types && node test/browser.js",
"test:types": "jest",
"tsc": "tsc --noEmit"
},
"version": "3.2.0"
}