https://github.com/mvisat/async-rwlock
Promise-based asynchronous readers-writers lock
https://github.com/mvisat/async-rwlock
async concurrency promise rwlock typescript
Last synced: about 1 year ago
JSON representation
Promise-based asynchronous readers-writers lock
- Host: GitHub
- URL: https://github.com/mvisat/async-rwlock
- Owner: mvisat
- License: mit
- Created: 2018-08-14T06:10:27.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-02-04T16:08:55.000Z (over 7 years ago)
- Last Synced: 2025-03-29T12:17:25.139Z (about 1 year ago)
- Topics: async, concurrency, promise, rwlock, typescript
- Language: TypeScript
- Homepage:
- Size: 61.5 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# async-rwlock
Promise-based asynchronous readers-writers lock. Timeout is also supported.
## Install
```
$ npm install --save async-rwlock
```
or
```
$ yarn add async-rwlock
```
## Quick Usage
```js
const RWLock = require('async-rwlock').RWLock;
const lock = new RWLock();
lock.readLock()
.then(() => {
console.log('read lock acquired');
lock.unlock();
});
lock.writeLock()
.then(() => {
console.log('write lock acquired');
lock.unlock();
});
```
### Async/Await
```ts
import RWLock from 'async-rwlock';
async function testLock() {
const lock = new RWLock();
await lock.readLock();
console.log('read lock acquired');
lock.unlock();
await lock.writeLock();
console.log('write lock acquired');
lock.unlock();
}
(async () => { await testLock(); })();
```
## Advanced Usage
### Timeout
```js
const RWLock = require('async-rwlock').RWLock;
const lock = new RWLock();
lock.readLock()
.then(() => {
console.log('read lock acquired');
// lock.unlock() // oops!
});
lock.writeLock(1000) // 1 second
.then(() => {
console.log('write lock will never be acquired');
})
.catch((err) => {
console.error('promise will be rejected if timeout occured');
console.error(err);
});
```
#### Async/Await
```ts
import RWLock from 'async-rwlock';
async function testLock() {
const lock = new RWLock();
await lock.readLock();
console.log('read lock acquired');
// lock.unlock() // oops!
try {
await lock.writeLock(1000); // 1 second
console.log('write lock will never be acquired');
} catch (err) {
console.error('error will be thrown if timeout occured');
console.error(err);
}
}
(async () => { await testLock(); })();
```
## API
### `new RWLock()`
- Returns: instance of `RWLock`.
#### `readLock([timeout])`
- `timeout?: number`. **Default**: `Infinity`.
- Returns: `Promise`.
#### `writeLock([timeout])`
- `timeout?: number`. **Default**: `Infinity`.
- Returns: `Promise`.
Acquire a read/write lock.
`timeout` is how long it will wait to acquire the lock before promise is rejected in milliseconds. If `timeout` is not in range `0 <= timeout < Infinity`, it will wait indefinitely.
#### `unlock()`
- Returns: `void`.
Release current lock. Must be called after operation using read/write lock is finished.
#### `getState()`
- Returns: `State`.
Get current state of lock. The states are either `Idle`, `Reading`, or `Writing`.
## License
[MIT](LICENSE)