UNPKG

@toolbuilder/semaphore

Version:

Basic semaphore and mutex with both sync and async acquire methods.

47 lines 1.26 kB
/** * @typedef {() => void} Resolver */ /** * Promise based semaphore. */ export class Semaphore { /** * Create a semaphore. * * @param {number} [max] - maximum number of locks that can be acquired at any given time */ constructor(max?: number); /** @private */ private _max; /** @private */ private _active; /** * @private * @type {Resolver[]} */ private _resolvers; /** * Returns whether a lock is available. If one is available, * acquireSync will succeed. * @returns {boolean} - true if a lock is available, false otherwise */ available(): boolean; /** * Acquires a lock synchronously. * @returns {boolean} - true if lock was acquired, false otherwise */ acquireSync(): boolean; /** * Acquires a lock asynchronously. * @returns {PromiseLike<void>} - promise resolves when a lock has been acquired. */ acquire(): PromiseLike<void>; /** * Releases a lock so that it is available to be acquired. * Each acquire or acquireSync call must be matched by exactly one release call. * @returns {void} */ release(): void; } export type Resolver = () => void; //# sourceMappingURL=semaphore.d.ts.map