* Acquire the lock * * @returns A release function to call when done
()
| 715 | * @returns A release function to call when done |
| 716 | */ |
| 717 | async acquire(): Promise<() => void> { |
| 718 | while (this.locked) { |
| 719 | await new Promise<void>((resolve) => { |
| 720 | this.waitQueue.push(resolve); |
| 721 | }); |
| 722 | } |
| 723 | |
| 724 | this.locked = true; |
| 725 | |
| 726 | return () => { |
| 727 | this.locked = false; |
| 728 | const next = this.waitQueue.shift(); |
| 729 | if (next) { |
| 730 | next(); |
| 731 | } |
| 732 | }; |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * Execute a function while holding the lock |
no test coverage detected