Home > @imqueue/rpc > IMQLock
IMQLock class
Class IMQLock. Implements promise-based locks.
Signature:
export declare class IMQLock
Example
~~~typescript import { IMQLock, AcquiredLock } from './index.js';
async function doSomething(): Promise<number | AcquiredLock
if (IMQLock.locked('doSomething')) { // skipping error handling this way can cause dead-locks, // so it is always good to wrap locked calls in try/catch! // BTW, IMQLock uses timeouts to avoid dead-locks try { // this code is called only once across multiple async calls, // so all promises will be resolved with the same value const res = Math.random(); IMQLock.release('doSomething', res); return res; }
catch (err) { // release acquired locks with error IMQLock.release('doSomething', null, err); throw err; } }
return lock; }
(async () => { for (let i = 0; i < 10; ++i) { // run doSomething() asynchronously 10 times doSomething().then((res) => console.log(res)); } })(); ~~~
Properties
|
Property |
Modifiers |
Type |
Description |
|---|---|---|---|
|
|
number |
Deadlock timeout in milliseconds {number} | |
|
|
Logger used to log errors that appear during locked calls {ILogger} |
Methods
|
Method |
Modifiers |
Description |
|---|---|---|
|
|
Acquires a lock for a given key. | |
|
|
Returns true if the given key is locked, false otherwise. | |
|
|
Releases a previously acquired lock for a given key. |