Implement LazyArray.splice(), implement unshift/shift/push/pop in terms of splice...
[logjson.git] / Mutex.mjs
1 import assert from 'assert'
2
3 class Mutex {
4   constructor() {
5     this.ready = null
6   }
7
8   async acquire() {
9     while (this.ready !== null)
10       await this.ready.promise
11     let ready = {}
12     ready.promise = new Promise(
13       (resolve, reject) => {ready.resolve = resolve}
14     )
15     this.ready = ready
16   }
17
18   release() {
19     let ready = this.ready
20     assert(ready !== null)
21     this.ready = null
22     ready.resolve()
23   }
24 }
25
26 export default Mutex