Commit e266f356cf9c371ca1664d82d1192822aa48fbef
uses all map props
Signed-off-by: wanderer <mjbecze@gmail.com>wanderer committed on 11/17/2017, 10:35:37 PM
Parent: d875b8b05b237bdbe18027c2d27eb7ff8197110e
Files changed
index.js | changed |
package.json | changed |
tests/index.js | changed |
index.js | ||
---|---|---|
@@ -3,8 +3,16 @@ | ||
3 | 3 … | * Creates a new instance of LockMap |
4 | 4 … | */ |
5 | 5 … | constructor () { |
6 | 6 … | this._map = new Map() |
7 … | + | |
8 … | + const methods = ['clear', 'delete', 'entries', 'forEach', 'get', 'has', 'keys', 'values', Symbol.iterator] | |
9 … | + const self = this | |
10 … | + methods.forEach(method => { | |
11 … | + self[method] = function () { | |
12 … | + return self._map[method].apply(self._map, arguments) | |
13 … | + } | |
14 … | + }) | |
7 | 15 … | } |
8 | 16 … | |
9 | 17 … | /** |
10 | 18 … | * Creates a lock on a given ID and returns a resolve function to unlock the |
@@ -23,13 +31,12 @@ | ||
23 | 31 … | this._map.set(id, promise) |
24 | 32 … | return r |
25 | 33 … | } |
26 | 34 … | |
27 | - /** | |
28 | - * gets the current lock if any for a given id. If there is a lock this will | |
29 | - * return a promise that resolves once the lock is unlocked | |
30 | - * @return {Promise} | |
31 | - */ | |
32 | - getLock (id) { | |
33 | - return this._map.get(id) | |
35 … | + get size () { | |
36 … | + return this._map.size | |
34 | 37 … | } |
38 … | + | |
39 … | + get [Symbol.toStringTag] () { | |
40 … | + return 'LockMap' | |
41 … | + } | |
35 | 42 … | } |
package.json | ||
---|---|---|
@@ -3,10 +3,10 @@ | ||
3 | 3 … | "version": "0.0.0", |
4 | 4 … | "description": "Creates a promise based map of locks that can be used as a semaphore", |
5 | 5 … | "main": "index.js", |
6 | 6 … | "scripts": { |
7 | - "coverage": "istanbul cover ./tests/index.js", | |
8 | - "coveralls": "npm run coverage && coveralls <coverage/lcov.info", | |
7 … | + "coverage": "nyc npm test", | |
8 … | + "coveralls": "npm run coverage && nyc report --reporter=text-lcov | coveralls", | |
9 | 9 … | "lint": "standard", |
10 | 10 … | "test": "node ./tests/index.js", |
11 | 11 … | "build:docs": "documentation build --github -f md ./index.js > ./docs/index.md" |
12 | 12 … | }, |
@@ -14,14 +14,13 @@ | ||
14 | 14 … | "" |
15 | 15 … | ], |
16 | 16 … | "author": "mjbecze <mjbecze@gmail.com>", |
17 | 17 … | "license": "MPL-2.0", |
18 | - "dependencies": { | |
19 | - }, | |
18 … | + "dependencies": {}, | |
20 | 19 … | "devDependencies": { |
20 … | + "documentation": "^5.3.3", | |
21 | 21 … | "coveralls": "^3.0.0", |
22 | - "documentation": "^5.2.0", | |
23 | - "istanbul": "^0.4.1", | |
22 … | + "nyc": "^11.3.0", | |
24 | 23 … | "standard": "^10.0.0", |
25 | 24 … | "tape": "^4.0.3" |
26 | 25 … | }, |
27 | 26 … | "repository": { |
tests/index.js | ||
---|---|---|
@@ -1,11 +1,12 @@ | ||
1 | 1 … | const tape = require('tape') |
2 | 2 … | const LockMap = require('../') |
3 | 3 … | |
4 | 4 … | tape('lockmap tests', async t => { |
5 | - t.plan(2) | |
5 … | + t.plan(4) | |
6 | 6 … | // create a new lock map |
7 | 7 … | const lockmap = new LockMap() |
8 … | + t.equals(lockmap.toString(), '[object LockMap]') | |
8 | 9 … | const id = 'test' |
9 | 10 … | |
10 | 11 … | let first = true |
11 | 12 … | |
@@ -18,8 +19,10 @@ | ||
18 | 19 … | first = false |
19 | 20 … | unlock() |
20 | 21 … | }, 300) |
21 | 22 … | |
23 … | + t.equals(lockmap.size, 1) | |
24 … | + | |
22 | 25 … | // wait until the lock resolves |
23 | - await lockmap.getLock(id) | |
26 … | + await lockmap.get(id) | |
24 | 27 … | t.equals(first, false) |
25 | 28 … | }) |
Built with git-ssb-web