export default class Lock {
  private doUnlock: (() => void) | null = null;
  private lockPromise: Promise<void> | null = null;

  get locked() {
    return this.lockPromise !== null;
  }

  async wait(): Promise<void> {
    if (!this.lockPromise) {
      this.lockPromise = new Promise(resolve => this.doUnlock = resolve);
    }

    return this.lockPromise;
  }

  release(): void {
    this.doUnlock?.();
    this.doUnlock = null;
    this.lockPromise = null;
  }
}