Files: 3bc80dd00aa8914d1a03ba6ccdf60a5dfac4c8a9 / index.js
765 bytesRaw
1 | module.exports = class LockMap { |
2 | /** |
3 | * Creates a new instance of LockMap |
4 | */ |
5 | constructor () { |
6 | this._map = new Map() |
7 | } |
8 | |
9 | /** |
10 | * Creates a lock on a given ID and returns a resolve function to unlock the |
11 | * lock |
12 | * @param {*} id |
13 | * @return {Function} the resolve function to call once it to unlock |
14 | */ |
15 | lock (id) { |
16 | let r |
17 | const promise = new Promise((resolve, reject) => { |
18 | r = resolve |
19 | }) |
20 | promise.then(() => { |
21 | this._map.delete(id) |
22 | }) |
23 | this._map.set(id, promise) |
24 | return r |
25 | } |
26 | |
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) |
34 | } |
35 | } |
36 |
Built with git-ssb-web