File size: 2,947 Bytes
f06e713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
 * A mutex lock for coordination across async functions
 */
export default class AwaitLock {
    #acquired = false;
    #waitingResolvers = new Set();
    /**
     * Whether the lock is currently acquired or not. Accessing this property does not affect the
     * status of the lock.
     */
    get acquired() {
        return this.#acquired;
    }
    /**
     * Acquires the lock, waiting if necessary for it to become free if it is already locked. The
     * returned promise is fulfilled once the lock is acquired.
     *
     * A timeout (in milliseconds) may be optionally provided. If the lock cannot be acquired before
     * the timeout elapses, the returned promise is rejected with an error. The behavior of invalid
     * timeout values depends on how `setTimeout` handles those values.
     *
     * After acquiring the lock, you **must** call `release` when you are done with it.
     */
    acquireAsync({ timeout } = {}) {
        if (!this.#acquired) {
            this.#acquired = true;
            return Promise.resolve();
        }
        if (timeout == null) {
            return new Promise((resolve) => {
                this.#waitingResolvers.add(resolve);
            });
        }
        let resolver;
        let timer;
        return Promise.race([
            new Promise((resolve) => {
                resolver = () => {
                    clearTimeout(timer);
                    resolve();
                };
                this.#waitingResolvers.add(resolver);
            }),
            new Promise((_, reject) => {
                timer = setTimeout(() => {
                    this.#waitingResolvers.delete(resolver);
                    reject(new Error(`Timed out waiting for lock`));
                }, timeout);
            }),
        ]);
    }
    /**
     * Acquires the lock if it is free and otherwise returns immediately without waiting. Returns
     * `true` if the lock was free and is now acquired, and `false` otherwise.
     *
     * This method differs from calling `acquireAsync` with a zero-millisecond timeout in that it runs
     * synchronously without waiting for the JavaScript task queue.
     */
    tryAcquire() {
        if (!this.#acquired) {
            this.#acquired = true;
            return true;
        }
        return false;
    }
    /**
     * Releases the lock and gives it to the next waiting acquirer, if there is one. Each acquirer
     * must release the lock exactly once.
     */
    release() {
        if (!this.#acquired) {
            throw new Error(`Cannot release an unacquired lock`);
        }
        if (this.#waitingResolvers.size > 0) {
            // Sets preserve insertion order like a queue
            const [resolve] = this.#waitingResolvers;
            this.#waitingResolvers.delete(resolve);
            resolve();
        }
        else {
            this.#acquired = false;
        }
    }
}
//# sourceMappingURL=AwaitLock.js.map