File size: 1,300 Bytes
cfbbc1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Barrier = void 0;
/**
 * A simple barrier to synchronize flow of multiple async operations.
 */
class Barrier {
    constructor(targetCount) {
        this.currentCount = 0;
        this.targetCount = targetCount;
        this.promise = new Promise(resolve => {
            this.resolve = resolve;
        });
    }
    /**
     * Signal that a participant has reached the barrier.
     *
     * The barrier will be resolved once `targetCount` participants have reached it.
     */
    signal() {
        this.currentCount += 1;
        if (this.currentCount === this.targetCount) {
            this.resolve();
        }
    }
    /**
     * Wait for the barrier to be resolved.
     *
     * @returns A promise that resolves when the barrier is resolved.
     */
    async wait() {
        return this.promise;
    }
    /**
     * Signal that a participant has reached the barrier and wait for the barrier to be resolved.
     *
     * The barrier will be resolved once `targetCount` participants have reached it.
     *
     * @returns A promise that resolves when the barrier is resolved.
     */
    async signalAndWait() {
        this.signal();
        return this.wait();
    }
}
exports.Barrier = Barrier;