iamolegga/nest-nsq-transport

View on GitHub
src/wait-group.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { Deferred } from './deferred';

export class WaitGroup {
  private deferred = new Deferred();
  private count = 0;
  private waiting = false;

  add(x: number) {
    this.count += x;
  }

  done() {
    this.count--;
    if (this.waiting && this.count <= 0) {
      this.deferred.resolve();
    }
  }

  async wait() {
    this.waiting = true;
    if (this.count <= 0) this.deferred.resolve();
    return this.deferred.promise;
  }
}