Skip to content

Commit 1281cb5

Browse files
committed
doc: update doc accordingly
1 parent 94ce278 commit 1281cb5

3 files changed

Lines changed: 55 additions & 54 deletions

File tree

docs/content/docs/api.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,38 @@ Acquire the lock. If the lock is already acquired, it will wait until it is rele
1414

1515
```ts
1616
const lock = verrou.createLock('key', '10s')
17-
await lock.acquire()
18-
await lock.acquire({ retry: { timeout: 1000 } })
19-
await lock.acquire({ retry: { timeout: '1s' } })
17+
const acquired = await lock.acquire()
18+
const acquired = await lock.acquire({ retry: { timeout: 1000 } })
19+
const acquired = await lock.acquire({ retry: { timeout: '1s' } })
20+
21+
if (acquired) {
22+
await criticalSection()
23+
}
2024
```
2125

2226
Accept an optional object with the following properties:
2327

2428
- `retry`: An object with the following properties:
25-
- `timeout`: The maximum time to wait for the lock to be acquired. If the timeout is reached, an `E_LOCK_TIMEOUT` error will be thrown. Defaults to `Infinity`.
29+
- `timeout`: The maximum time to wait for the lock to be acquired. Defaults to `Infinity`.
2630
- `delay`: The delay in miliseconds between each retry. Defaults to `250`
2731
- `attempts`: The maximum number of attempts to acquire the lock.
2832

33+
`acquire` will return a boolean indicating if the lock was acquired or not
34+
35+
### `acquireImmediately`
36+
37+
Try to acquire the lock immediately ( without retrying ). If the lock is already acquired, it will return `false` immediately.
38+
39+
```ts
40+
import { errors } from '@verrou/core'
41+
42+
const lock = verrou.createLock('key')
43+
const acquired = await lock.acquireImmediately()
44+
if (acquired) {
45+
await criticalSection()
46+
}
47+
```
48+
2949
### `release`
3050

3151
Release the lock. Note that only the lock owner can release the lock.
@@ -39,45 +59,31 @@ await lock.release()
3959

4060
### `run`
4161

42-
Acquire the lock, run the callback, and release the lock.
62+
Acquire the lock, run the callback, and release the lock. The method will return a tuple with the first value being a boolean indicating if the lock was acquired or not, and the second value being the result of the callback.
4363

4464
```ts
4565
const lock = verrou.createLock('key')
46-
await lock.run(() => {
66+
const [executed, result] = await lock.run(() => {
4767
// do something
68+
return 'result'
4869
})
4970
```
5071

72+
5173
### `runImmediately`
5274

53-
Same as `run`, but try to acquire the lock immediately ( without retrying ). If the lock is already acquired, it will throws a `E_LOCK_ALREADY_ACQUIRED` error.
75+
Same as `run`, but try to acquire the lock immediately ( without retrying ).
5476

5577
```ts
5678
const lock = verrou.createLock('key')
57-
await lock.runImmediately(async () => {
79+
const [executed, result] = await lock.runImmediately(async () => {
5880
// do something
81+
return 'result'
5982
})
6083
```
6184

6285
Accept an optional object with the same properties as `acquire`.
6386

64-
### `acquireImmediately`
65-
66-
Try to acquire the lock immediately ( without retrying ). If the lock is already acquired, it will throws a `E_LOCK_ALREADY_ACQUIRED` error.
67-
68-
```ts
69-
import { errors } from '@verrou/core'
70-
71-
const lock = verrou.createLock('key')
72-
try {
73-
await lock.acquireImmediately()
74-
} catch (err) {
75-
if (err instanceof E_LOCK_ALREADY_ACQUIRED) {
76-
77-
}
78-
}
79-
```
80-
8187
### `isLocked`
8288

8389
Check if the lock is acquired.

docs/content/docs/introduction.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ const verrou = new Verrou({
3636

3737
```ts
3838
// title: Manual lock
39-
import { Verrou, E_LOCK_TIMEOUT } from '@verrou/core'
39+
import { Verrou } from '@verrou/core'
4040

4141
const lock = verrou.createLock('my-resource')
42+
const acquired = await lock.acquire()
43+
4244
try {
43-
await lock.acquire()
4445
await doSomething()
45-
} catch (error) {
46-
if (error instanceof E_LOCK_TIMEOUT) {
47-
// handle timeout
48-
}
4946
} finally {
5047
await lock.release()
5148
}

docs/content/docs/usage.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ import { verrou } from './verrou.js'
3838
const lock = verrou.createLock('my-resource', null)
3939

4040
// We gonna wait for the lock to be acquired
41-
await lock.acquire()
42-
43-
// Do your critical code here
44-
doSomething()
41+
if (await lock.acquire()) {
42+
// Do your critical code here
43+
doSomething()
4544

46-
// Once you are done, release the lock.
47-
await lock.release()
45+
// Once you are done, release the lock.
46+
await lock.release()
47+
}
4848
```
4949

5050
But we are still missing error handling. What if my `doSomething` method throws an error? The lock will never be released. To prevent this, always make sure to wrap your code with a try/catch/finaly block.
@@ -53,10 +53,10 @@ But we are still missing error handling. What if my `doSomething` method throws
5353
import { verrou } from './verrou.js'
5454

5555
const lock = verrou.createLock('my-resource', null)
56+
const acquired = await lock.acquire()
57+
if (!acquired) return 'Lock not acquired'
5658

5759
try {
58-
await lock.acquire()
59-
6060
// Do your critical code here
6161
doSomething()
6262
} catch (error) {
@@ -115,29 +115,27 @@ await lock.acquire({
115115

116116
In general, you will either use the `retry.attempts` or `retry.timeout` options.
117117

118-
### Handling errors
118+
### Handling lock acquisition failure
119119

120-
If ever you can't acquire a lock, an error will be thrown. You can catch it and handle it like this :
120+
If ever you can't acquire a lock, `acquire` and `acquireImmediately` will return `false`. You can check if the lock was acquired by checking this value.
121121

122122
```ts
123123
import { errors } from '@verrou/core'
124124

125-
try {
126-
await lock.acquire()
127-
} catch (error) {
128-
if (error instanceof errors.E_LOCK_TIMEOUT) {
129-
// Handle the error
130-
}
125+
const acquired = await lock.acquire()
126+
if (!acquired) {
127+
return 'Lock not acquired'
131128
}
129+
```
132130

133-
await lock.run(async () => {
134-
// Do your critical code here
135-
doSomething()
136-
}).catch(error => {
137-
if (error instanceof errors.E_LOCK_TIMEOUT) {
138-
// Handle the error
139-
}
131+
`run` and `runImmediately` methods will return a tuple with the first value being a boolean indicating if the lock was acquired or not.
132+
133+
```ts
134+
const [acquired, result] = await lock.run(async () => {
135+
return doSomething()
140136
})
137+
138+
if (!acquired) return 'Lock not acquired'
141139
```
142140

143141
### Sharing a lock between multiple processes

0 commit comments

Comments
 (0)