UNPKG

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