aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@calebboyd
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@calebboyd')
-rw-r--r--node_modules/@calebboyd/semaphore/LICENSE20
-rw-r--r--node_modules/@calebboyd/semaphore/README.md35
-rw-r--r--node_modules/@calebboyd/semaphore/bound.d.ts1
-rw-r--r--node_modules/@calebboyd/semaphore/bound.js12
-rw-r--r--node_modules/@calebboyd/semaphore/deferred.d.ts8
-rw-r--r--node_modules/@calebboyd/semaphore/deferred.js16
-rw-r--r--node_modules/@calebboyd/semaphore/each.d.ts4
-rw-r--r--node_modules/@calebboyd/semaphore/each.js18
-rw-r--r--node_modules/@calebboyd/semaphore/index.d.ts4
-rw-r--r--node_modules/@calebboyd/semaphore/index.js12
-rw-r--r--node_modules/@calebboyd/semaphore/map.d.ts4
-rw-r--r--node_modules/@calebboyd/semaphore/map.js14
-rw-r--r--node_modules/@calebboyd/semaphore/package.json26
-rw-r--r--node_modules/@calebboyd/semaphore/semaphore.d.ts14
-rw-r--r--node_modules/@calebboyd/semaphore/semaphore.js68
15 files changed, 256 insertions, 0 deletions
diff --git a/node_modules/@calebboyd/semaphore/LICENSE b/node_modules/@calebboyd/semaphore/LICENSE
new file mode 100644
index 0000000..1624d72
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2017 Caleb Boyd
+
+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.
diff --git a/node_modules/@calebboyd/semaphore/README.md b/node_modules/@calebboyd/semaphore/README.md
new file mode 100644
index 0000000..cdee8bf
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/README.md
@@ -0,0 +1,35 @@
+## Semaphore
+
+Naive resource management / concurrency primitive
+
+`npm i @calebboyd/semaphore`
+
+### Example
+```javascript
+import { Semaphore } from '@calebboyd/semaphore'
+import { something } from './somewhere'
+
+function doSomeWork () {
+ const lock = new Semaphore(10)
+ while(something.isTrue) {
+ lock.acquire().then(x => () => {
+ something.work().then(lock.release)
+ })
+ }
+}
+```
+The semaphore will only allow 10 locks to be aquired at a time. This limits the concurrency of the asyncronous function `something.work` to 10
+
+#### API:
+
+```typescript
+export class Semaphore {
+ constructor (count: number) {}
+ acquire(): Promise<void>
+ release(): void
+}
+```
+
+## License
+
+MIT
diff --git a/node_modules/@calebboyd/semaphore/bound.d.ts b/node_modules/@calebboyd/semaphore/bound.d.ts
new file mode 100644
index 0000000..4df956b
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/bound.d.ts
@@ -0,0 +1 @@
+export declare const bound: MethodDecorator;
diff --git a/node_modules/@calebboyd/semaphore/bound.js b/node_modules/@calebboyd/semaphore/bound.js
new file mode 100644
index 0000000..e3b0a3e
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/bound.js
@@ -0,0 +1,12 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.bound = function bound(target, propertyKey, descriptor) {
+ return {
+ configurable: true,
+ get: function () {
+ var value = descriptor.value.bind(this);
+ Object.defineProperty(this, propertyKey, { value: value, configurable: true, writable: true });
+ return value;
+ }
+ };
+};
diff --git a/node_modules/@calebboyd/semaphore/deferred.d.ts b/node_modules/@calebboyd/semaphore/deferred.d.ts
new file mode 100644
index 0000000..cbc1ada
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/deferred.d.ts
@@ -0,0 +1,8 @@
+export declare function createDeferredFactory(PromiseCtor?: PromiseConstructor): <T>() => Deferred<T>;
+export declare const createDeferred: <T>() => Deferred<T>;
+export interface Deferred<T> {
+ value: T;
+ promise: Promise<T>;
+ resolve(value?: T | undefined | PromiseLike<T>): void;
+ reject(reason: any): void;
+}
diff --git a/node_modules/@calebboyd/semaphore/deferred.js b/node_modules/@calebboyd/semaphore/deferred.js
new file mode 100644
index 0000000..a096b92
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/deferred.js
@@ -0,0 +1,16 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+function createDeferredFactory(PromiseCtor) {
+ if (PromiseCtor === void 0) { PromiseCtor = Promise; }
+ return function deferred() {
+ var deferred = {};
+ var promise = new PromiseCtor(function (resolve, reject) {
+ deferred.resolve = resolve;
+ deferred.reject = reject;
+ });
+ deferred.promise = promise;
+ return deferred;
+ };
+}
+exports.createDeferredFactory = createDeferredFactory;
+exports.createDeferred = createDeferredFactory();
diff --git a/node_modules/@calebboyd/semaphore/each.d.ts b/node_modules/@calebboyd/semaphore/each.d.ts
new file mode 100644
index 0000000..3c58949
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/each.d.ts
@@ -0,0 +1,4 @@
+export declare function each<T>(this: any, list: Iterable<T>, iterator: Function, {context, concurrency}?: {
+ context?: any;
+ concurrency: null | number;
+}): Promise<undefined>;
diff --git a/node_modules/@calebboyd/semaphore/each.js b/node_modules/@calebboyd/semaphore/each.js
new file mode 100644
index 0000000..a17efc5
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/each.js
@@ -0,0 +1,18 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+var semaphore_1 = require("./semaphore");
+function each(list, iterator, _a) {
+ var _b = _a === void 0 ? { concurrency: null } : _a, context = _b.context, concurrency = _b.concurrency;
+ var arrList = Array.from(list);
+ context = context || this;
+ if (concurrency === null)
+ concurrency = arrList.length;
+ var lock = new semaphore_1.Semaphore(concurrency);
+ var pending = [];
+ var handler = function (locks) { return iterator.call(context, arrList[locks - 1], locks - 1, list); };
+ for (var i = 0; i < arrList.length; i++) {
+ pending.push(lock.acquire().then(handler).then(lock.release));
+ }
+ return Promise.all(pending).then(function () { return void 0; });
+}
+exports.each = each;
diff --git a/node_modules/@calebboyd/semaphore/index.d.ts b/node_modules/@calebboyd/semaphore/index.d.ts
new file mode 100644
index 0000000..c7b409c
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/index.d.ts
@@ -0,0 +1,4 @@
+export { each } from './each';
+export { map } from './map';
+export { createDeferredFactory, createDeferred, Deferred } from './deferred';
+export { Semaphore, createLock } from './semaphore';
diff --git a/node_modules/@calebboyd/semaphore/index.js b/node_modules/@calebboyd/semaphore/index.js
new file mode 100644
index 0000000..2fe1fce
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/index.js
@@ -0,0 +1,12 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+var each_1 = require("./each");
+exports.each = each_1.each;
+var map_1 = require("./map");
+exports.map = map_1.map;
+var deferred_1 = require("./deferred");
+exports.createDeferredFactory = deferred_1.createDeferredFactory;
+exports.createDeferred = deferred_1.createDeferred;
+var semaphore_1 = require("./semaphore");
+exports.Semaphore = semaphore_1.Semaphore;
+exports.createLock = semaphore_1.createLock;
diff --git a/node_modules/@calebboyd/semaphore/map.d.ts b/node_modules/@calebboyd/semaphore/map.d.ts
new file mode 100644
index 0000000..9e451e9
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/map.d.ts
@@ -0,0 +1,4 @@
+export declare function map<T>(this: any, list: Iterable<T>, iterator: Function, {context, concurrency}?: {
+ context?: any;
+ concurrency: null | number;
+}): any;
diff --git a/node_modules/@calebboyd/semaphore/map.js b/node_modules/@calebboyd/semaphore/map.js
new file mode 100644
index 0000000..a13a260
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/map.js
@@ -0,0 +1,14 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+var each_1 = require("./each");
+function map(list, iterator, _a) {
+ var _b = _a === void 0 ? { concurrency: null } : _a, context = _b.context, concurrency = _b.concurrency;
+ var results = [];
+ return each_1.each.call(this, list, function (value, i) {
+ var result = iterator.call(context, value, i, list);
+ results.push(result);
+ return result;
+ }, { concurrency: concurrency })
+ .then(function () { return Promise.all(results); });
+}
+exports.map = map;
diff --git a/node_modules/@calebboyd/semaphore/package.json b/node_modules/@calebboyd/semaphore/package.json
new file mode 100644
index 0000000..8f6b916
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/package.json
@@ -0,0 +1,26 @@
+{
+ "name": "@calebboyd/semaphore",
+ "version": "1.3.1",
+ "description": "Simple promise based counting semaphore",
+ "main": "index.js",
+ "scripts": {
+ "test": "npm run clean && mocha",
+ "clean": "rimraf \"*.{js,d.ts}\"",
+ "prepublishOnly": "npm test && tsc --declaration"
+ },
+ "files": [
+ "*.js",
+ "*.d.ts"
+ ],
+ "author": "Caleb Boyd",
+ "license": "MIT",
+ "devDependencies": {
+ "@types/chai": "^4.1.3",
+ "@types/mocha": "^5.2.0",
+ "chai": "^4.1.2",
+ "mocha": "^5.1.1",
+ "rimraf": "^2.6.2",
+ "ts-node": "^6.0.3",
+ "typescript": "^2.8.3"
+ }
+}
diff --git a/node_modules/@calebboyd/semaphore/semaphore.d.ts b/node_modules/@calebboyd/semaphore/semaphore.d.ts
new file mode 100644
index 0000000..f343f46
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/semaphore.d.ts
@@ -0,0 +1,14 @@
+export declare function createLock(count: number, promise?: PromiseConstructor): Semaphore<number>;
+export declare class Semaphore<T = number> {
+ size: number;
+ private promise;
+ private waiting;
+ private locks;
+ private requestedLockCount;
+ private createDeferred;
+ constructor(size?: number, promise?: PromiseConstructor);
+ readonly count: number;
+ readonly pending: number;
+ acquire(arg?: T): Promise<T>;
+ release(): void;
+}
diff --git a/node_modules/@calebboyd/semaphore/semaphore.js b/node_modules/@calebboyd/semaphore/semaphore.js
new file mode 100644
index 0000000..959db81
--- /dev/null
+++ b/node_modules/@calebboyd/semaphore/semaphore.js
@@ -0,0 +1,68 @@
+"use strict";
+var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+var bound_1 = require("./bound");
+var deferred_1 = require("./deferred");
+function createLock(count, promise) {
+ if (promise === void 0) { promise = Promise; }
+ return new Semaphore(count, promise);
+}
+exports.createLock = createLock;
+var Semaphore = /** @class */ (function () {
+ function Semaphore(size, promise) {
+ if (size === void 0) { size = 1; }
+ if (promise === void 0) { promise = Promise; }
+ this.size = size;
+ this.promise = promise;
+ this.waiting = [];
+ this.locks = 0;
+ this.requestedLockCount = 0;
+ this.createDeferred = this.promise !== Promise
+ ? deferred_1.createDeferredFactory(this.promise)
+ : deferred_1.createDeferred;
+ if (!size || size < 1) {
+ throw new Error('Cannot create Semaphore with size of "' + size + '"');
+ }
+ }
+ Object.defineProperty(Semaphore.prototype, "count", {
+ get: function () { return this.locks; },
+ enumerable: true,
+ configurable: true
+ });
+ Object.defineProperty(Semaphore.prototype, "pending", {
+ get: function () { return this.waiting.length; },
+ enumerable: true,
+ configurable: true
+ });
+ Semaphore.prototype.acquire = function (arg) {
+ ++this.requestedLockCount;
+ arg = (typeof arg !== 'undefined' ? arg : this.requestedLockCount);
+ if (this.locks < this.size) {
+ this.locks++;
+ return this.promise.resolve(arg);
+ }
+ var lock = this.createDeferred();
+ lock.value = arg;
+ this.waiting.push(lock);
+ return lock.promise;
+ };
+ Semaphore.prototype.release = function () {
+ var lock = this.waiting.shift();
+ if (lock) {
+ lock.resolve(lock.value);
+ }
+ else if (--this.locks < 0) {
+ throw new Error('Nothing to release...');
+ }
+ };
+ __decorate([
+ bound_1.bound
+ ], Semaphore.prototype, "release", null);
+ return Semaphore;
+}());
+exports.Semaphore = Semaphore;