promised-map
Advanced tools
+10
-6
| { | ||
| "name": "promised-map", | ||
| "version": "0.2.0", | ||
| "version": "0.3.0", | ||
| "description": "A map of promises that can be resolved or rejected by key", | ||
@@ -17,2 +17,6 @@ "author": { | ||
| "main": "src/index.js", | ||
| "files": [ | ||
| "src", | ||
| "README.md" | ||
| ], | ||
| "scripts": { | ||
@@ -41,8 +45,8 @@ "lint": "eslint src test", | ||
| "chai": "^4.2.0", | ||
| "eslint": "^6.6.0", | ||
| "eslint-plugin-mocha": "^6.2.1", | ||
| "husky": "^3.0.9", | ||
| "lint-staged": "^9.4.2", | ||
| "eslint": "^7.18.0", | ||
| "eslint-plugin-mocha": "^8.0.0", | ||
| "husky": "^4.3.8", | ||
| "lint-staged": "^10.5.3", | ||
| "markdown-toc": "^1.2.0", | ||
| "mocha": "^6.2.2" | ||
| "mocha": "^8.2.1" | ||
| }, | ||
@@ -49,0 +53,0 @@ "keywords": [ |
+11
-0
| # promised-map | ||
| [](https://github.com/vitalets/promised-map/actions) | ||
| [](https://www.npmjs.com/package/promised-map) | ||
@@ -28,2 +29,3 @@ [](https://www.npmjs.com/package/promised-map) | ||
| // create promise associated with key 'foo' | ||
| const promise = map.wait('foo'); | ||
@@ -39,2 +41,11 @@ | ||
| map.has('foo'); | ||
| // resolve all promises and clear map | ||
| map.resolveAll(42); | ||
| // reject all promises and clear map | ||
| map.rejectAll(new Error('error')); | ||
| // iterate all promises | ||
| map.forEach(({ promise, resolve, reject, timestamp }, key) => { ... }); | ||
| ``` | ||
@@ -41,0 +52,0 @@ |
+59
-4
@@ -5,3 +5,6 @@ /** | ||
| class PromisedMap { | ||
| module.exports = class PromisedMap { | ||
| /** | ||
| * Constructor. | ||
| */ | ||
| constructor() { | ||
@@ -11,2 +14,7 @@ this._map = new Map(); | ||
| /** | ||
| * Returns map size. | ||
| * | ||
| * @returns {number} | ||
| */ | ||
| get size() { | ||
@@ -16,2 +24,9 @@ return this._map.size; | ||
| /** | ||
| * Creates and returns new promise for provided key. | ||
| * If key already exists in map - existing pending promise will be returned. | ||
| * | ||
| * @param {*} key | ||
| * @returns {Promise} | ||
| */ | ||
| wait(key) { | ||
@@ -32,2 +47,9 @@ let {promise} = this._map.get(key) || {}; | ||
| /** | ||
| * Resolves promise in map by key and removes key from map. | ||
| * If no such key in map - nothing happens. | ||
| * | ||
| * @param {*} key | ||
| * @param {*} value | ||
| */ | ||
| resolve(key, value) { | ||
@@ -41,2 +63,9 @@ const {resolve} = this._map.get(key) || {}; | ||
| /** | ||
| * Rejects promise in map by key and removes key from map. | ||
| * If no such key in map - nothing happens. | ||
| * | ||
| * @param {*} key | ||
| * @param {*} value | ||
| */ | ||
| reject(key, value) { | ||
@@ -50,2 +79,8 @@ const {reject} = this._map.get(key) || {}; | ||
| /** | ||
| * Does map has provided key. | ||
| * | ||
| * @param {*} key | ||
| * @returns {boolean} | ||
| */ | ||
| has(key) { | ||
@@ -55,2 +90,9 @@ return this._map.has(key); | ||
| /** | ||
| * Deletes key from map. | ||
| * Caution: previously returned promise will no be resolved or rejected. | ||
| * | ||
| * @param {*} key | ||
| * @returns {boolean} | ||
| */ | ||
| delete(key) { | ||
@@ -60,2 +102,7 @@ return this._map.delete(key); | ||
| /** | ||
| * Resolves all promise in map and removes all keys. | ||
| * | ||
| * @param {*} value | ||
| */ | ||
| resolveAll(value) { | ||
@@ -66,2 +113,7 @@ this._map.forEach(({resolve}) => resolve(value)); | ||
| /** | ||
| * Rejects all promise in map and removes all keys. | ||
| * | ||
| * @param {*} value | ||
| */ | ||
| rejectAll(value) { | ||
@@ -72,7 +124,10 @@ this._map.forEach(({reject}) => reject(value)); | ||
| /** | ||
| * Iterator. | ||
| * | ||
| * @param {function} fn | ||
| */ | ||
| forEach(fn) { | ||
| this._map.forEach(fn); | ||
| } | ||
| } | ||
| module.exports = PromisedMap; | ||
| }; |
4851
47.94%113
98.25%53
26.19%