UNPKG

4.48 kBTypeScriptView Raw
1declare namespace AsyncLock {
2 type AsyncLockDoneCallback<T> = (err?: Error | null, ret?: T) => void;
3
4 interface AsyncLockOptions {
5 /**
6 * Max amount of time an item can remain in the queue before acquiring the lock.
7 *
8 * @default 0 (Never)
9 */
10 timeout?: number | undefined;
11 /**
12 * Max number of tasks allowed in the queue at a time.
13 *
14 * @default 1000
15 */
16 maxPending?: number | undefined;
17 /**
18 * Max amount of time allowed between entering the queue and completing execution.
19 *
20 * @default 0 (Never)
21 */
22 maxOccupationTime?: number | undefined;
23 /**
24 * Max amount of time allowed between acquiring the lock and completing execution.
25 *
26 * @default 0 (Never)
27 */
28 maxExecutionTime?: number | undefined;
29 /**
30 * Make a lock reentrant in the same domain.
31 *
32 * @default false
33 *
34 * @example
35 * import AsyncLock = require('async-lock');
36 * import * as domain from 'domain';
37 *
38 * const lock = new AsyncLock({ domainReentrant: true });
39 * const d = domain.create();
40 * d.run(() => {
41 * lock.acquire('key', () => {
42 * // Enter lock
43 * return lock.acquire('key', () => {
44 * // Enter same lock twice
45 * });
46 * });
47 * });
48 */
49 domainReentrant?: boolean | undefined;
50 /**
51 * Allows to enqueue a task in the front of the queue, skipping all enqueued tasks.
52 *
53 * @default false
54 *
55 * @example
56 * import AsyncLock = require('async-lock');
57 *
58 * const lock = new AsyncLock();
59 * // Add a task to the front of the queue waiting for a given lock
60 * lock.acquire(key, fn1, cb); // runs immediately
61 * lock.acquire(key, fn2, cb); // added to queue
62 * lock.acquire(key, priorityFn, cb, { skipQueue: true }); // jumps queue and runs before fn2
63 */
64 skipQueue?: boolean | undefined;
65 /**
66 * Use your own promise library instead of the global `Promise` variable.
67 *
68 * @example
69 * import AsyncLock = require('async-lock');
70 * import Bluebird = require('bluebird');
71 * import Q = require('q');
72 *
73 * new AsyncLock({ Promise: Bluebird }); // Bluebird
74 * new AsyncLock({ Promise: Q }); // Q
75 */
76 Promise?: unknown;
77 }
78}
79
80declare class AsyncLock {
81 static readonly DEFAULT_TIMEOUT: 0;
82 static readonly DEFAULT_MAX_OCCUPATION_TIME: 0;
83 static readonly DEFAULT_MAX_EXECUTION_TIME: 0;
84 static readonly DEFAULT_MAX_PENDING: 1000;
85
86 constructor(options?: AsyncLock.AsyncLockOptions);
87
88 /**
89 * Lock on asynchronous code.
90 *
91 * @param key resource key or keys to lock
92 * @param fn function to execute
93 * @param opts options
94 *
95 * @example
96 * import AsyncLock = require('async-lock');
97 * const lock = new AsyncLock();
98 *
99 * lock.acquire(
100 * key,
101 * () => {
102 * // return value or promise
103 * },
104 * opts
105 * ).then(() => {
106 * // lock released
107 * });
108 */
109 acquire<T>(
110 key: string | string[],
111 fn: (() => T | PromiseLike<T>) | ((done: AsyncLock.AsyncLockDoneCallback<T>) => any),
112 opts?: AsyncLock.AsyncLockOptions,
113 ): Promise<T>;
114 /**
115 * Lock on asynchronous code.
116 *
117 * @param key resource key or keys to lock
118 * @param fn function to execute
119 * @param cb callback function
120 * @param opts options
121 *
122 * @example
123 * import AsyncLock = require('async-lock');
124 * const lock = new AsyncLock();
125 *
126 * lock.acquire(
127 * key,
128 * (done) => {
129 * // async work
130 * done(err, ret);
131 * },
132 * (err, ret) => {
133 * // lock released
134 * },
135 * opts
136 * );
137 */
138 acquire<T>(
139 key: string | string[],
140 fn: (done: AsyncLock.AsyncLockDoneCallback<T>) => any,
141 cb: AsyncLock.AsyncLockDoneCallback<T>,
142 opts?: AsyncLock.AsyncLockOptions,
143 ): void;
144
145 /**
146 * Whether there is any running or pending async function.
147 */
148 isBusy(key?: string): boolean;
149}
150
151export = AsyncLock;