• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

snatalenko / node-cqrs / 10223129684

02 Aug 2024 11:08PM UTC coverage: 94.639%. First build
10223129684

Pull #21

github

snatalenko
Separate github workflows for tests and coveralls
Pull Request #21: Migrate to TypeScript

552 of 854 branches covered (64.64%)

2231 of 2360 new or added lines in 28 files covered. (94.53%)

2348 of 2481 relevant lines covered (94.64%)

21.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.04
/src/infrastructure/InMemoryLock.ts
1
import { ILockable, ILockableWithIndication } from "../interfaces";
4✔
2
import { Deferred } from "./utils";
4✔
3

4✔
4
export class InMemoryLock implements ILockableWithIndication {
42✔
5

42✔
6
        #lockMarker: Deferred<void> | undefined;
42✔
7
        #innerLock: ILockable | undefined;
42✔
8

42✔
9
        /**
42✔
10
         * Indicates if lock is acquired
42✔
11
         */
42✔
12
        get locked(): boolean {
42✔
13
                return !!this.#lockMarker;
92✔
14
        }
92✔
15

42✔
16
        /**
42✔
17
         * Creates an instance of InMemoryLock
42✔
18
         *
42✔
19
         * @param innerLock ILockable instance that can persist lock state outside of the current process
42✔
20
         */
42✔
21
        constructor(innerLock?: ILockable) {
42✔
22
                this.#innerLock = innerLock;
64✔
23
        }
64✔
24

42✔
25
        /**
42✔
26
         * Acquire the lock on the current instance.
42✔
27
         * Resolves when the lock is successfully acquired
42✔
28
         */
42✔
29
        async lock(): Promise<void> {
42✔
30
                while (this.locked)
22✔
31
                        await this.once('unlocked');
22!
32

22✔
33
                try {
22✔
34
                        this.#lockMarker = new Deferred();
22✔
35
                        if (this.#innerLock)
22✔
36
                                await this.#innerLock.lock();
22!
37
                }
22✔
38
                catch (err: any) {
22!
NEW
39
                        try {
×
NEW
40
                                await this.unlock();
×
NEW
41
                        }
×
NEW
42
                        catch (unlockErr: any) {
×
NEW
43
                                // unlocking errors are ignored
×
NEW
44
                        }
×
NEW
45
                        throw err;
×
NEW
46
                }
×
47
        }
22✔
48

42✔
49
        /**
42✔
50
         * Release the lock acquired earlier
42✔
51
         */
42✔
52
        async unlock(): Promise<void> {
42✔
53
                try {
46✔
54
                        if (this.#innerLock)
46✔
55
                                await this.#innerLock.unlock();
46!
56
                }
46✔
57
                finally {
46✔
58
                        this.#lockMarker?.resolve();
46✔
59
                        this.#lockMarker = undefined;
46✔
60
                }
46✔
61
        }
46✔
62

42✔
63
        /**
42✔
64
         * Wait until the lock is released.
42✔
65
         * Resolves immediately if the lock is not acquired
42✔
66
         */
42✔
67
        once(event: 'unlocked'): Promise<void> {
42✔
68
                if (event !== 'unlocked')
6✔
69
                        throw new TypeError(`Unexpected event type: ${event}`);
6!
70

6✔
71
                return this.#lockMarker?.promise ?? Promise.resolve();
6!
72
        }
6✔
73
}
42✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc