Skip to content

Commit 4326c90

Browse files
committed
docs: notes
1 parent 1b62d85 commit 4326c90

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

packages/verrou/src/lock.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export class Lock {
2929
) {
3030
this.#key = key
3131
this.#config = config
32+
this.#ttl = ttl ?? null
3233
this.#lockStore = lockStore
3334
this.#expirationTime = expirationTime ?? null
3435
this.#owner = owner ?? this.#generateOwner()
35-
this.#ttl = ttl ?? null
3636
}
3737

3838
/**
@@ -75,17 +75,30 @@ export class Lock {
7575
const start = Date.now()
7676
while (attemptsDone++ < attemptsMax) {
7777
const now = Date.now()
78+
79+
/**
80+
* Try to acquire the lock
81+
*/
7882
const result = await this.#lockStore.save(this.#key, this.#owner, this.#ttl)
7983
if (result) {
8084
this.#expirationTime = this.#ttl ? now + this.#ttl : null
8185
break
8286
}
8387

88+
/**
89+
* Check if we reached the maximum number of attempts
90+
*/
8491
if (attemptsDone === attemptsMax) throw new E_LOCK_TIMEOUT()
8592

93+
/**
94+
* Or check if we reached the timeout
95+
*/
8696
const elapsed = Date.now() - start
8797
if (timeout && elapsed > timeout) throw new E_LOCK_TIMEOUT()
8898

99+
/**
100+
* Otherwise wait for the delay and try again
101+
*/
89102
await setTimeout(delay)
90103
}
91104

0 commit comments

Comments
 (0)