git ssb

0+

wanderer🌟 / lockmap



Tree: 38f718562e84b8cd2135ff0de9da998771d971c5

Files: 38f718562e84b8cd2135ff0de9da998771d971c5 / index.js

713 bytesRaw
1module.exports = class LockMap {
2 constructor () {
3 this._map = new Map()
4 }
5
6 /**
7 * Creates a lock on a given ID and returns a resolve function to unlock the
8 * lock
9 * @param {*} id
10 * @return {function} the resolve function to call once it to unlock
11 */
12 lock (id) {
13 let r
14 const promise = new Promise((resolve, reject) => {
15 r = resolve
16 })
17 promise.then(() => {
18 this._map.delete(id)
19 })
20 this._map.set(id, promise)
21 return r
22 }
23
24 /**
25 * gets the current lock if any for a given id. If there is a lock this will
26 * return a promise that resolves once the lock is unlocked
27 * return {Promise}
28 */
29 getLock (id) {
30 return this._map.get(id)
31 }
32}
33

Built with git-ssb-web