import { Future } from '../promise'; /** * Semaphore with async api * @param permits - number of permits * @public * * @example * const sem = new Semaphore(5) * const release = await sem.acquire() * // do something * release() */ export declare class Semaphore { /** * check if semaphore is frozen (non-undefined value), other uses are not guaranteed */ frozen?: Future; private _permits; private queue; /** * constructs a new Semaphore with n permits * @param permits - number of permits */ constructor(permits?: number); /** * Acquire a permit, resolve when resouce is available. * @returns a function to release semaphore */ acquire(timeout?: number): Promise<() => void>; /** * Try to synchronosly acquire if there's remaining permits * @returns a function to release the semaphore * @throws Error if semaphore is drained */ tryAcquire(): () => void; /** * Schedule a task to run when a permit is available and automatically release after run. */ schedule(fn: () => T): Promise>; /** * Give n permits to the semaphore, will immediately start this number of waiting tasks if not frozen * @param permits - number of permits * @throws RangeError - if permits is less than 0 */ grant(permits?: number): void; /** * Destroy n permits, effective until `remain` fills the n permits * * **note**: you may need to check if `permits > semaphore.permits`, or it will wait until granted that many permits * @param permits - number of permits * @throws RangeError - if permits is less than 0 */ revoke(permits?: number): Promise; /** * Freeze this semaphore, calling `acquire` won't resolve and `tryAcquire` will throw (release can still be called). * * NOTE: don't call this again if {@link Semaphore.frozen}, not supported yet */ freeze(): void; /** * unfreeze this semaphore, it is synchronos and the returned value should be ignored * @returns a promise that's already resolved you can add a */ unfreeze(): Promise; /** * Get the number of remaining permits */ get remain(): number; /** * Get the number of total permits currently */ get permits(): number; /** * Check if all permits are being used * * always return `true` if `permits = 0` */ get isFull(): boolean; /** * Check if no task is using the semaphore' * * always return `true` if `permits = 0` */ get isEmpty(): boolean; private releaser; /** * resolves next n values in the queue. * * If semaphore is frozen, wait for `frozen` to resolve first. * These queued items are although the first _permits_ elements in the queue, they are not resolved. */ private resolveNext; private remove; } /** * transfer n permits from one semaphore to another * @param from - semaphore to revoke permits * @param to - semaphore to grant permits * @param tokens - number of permits, defaults to 1 * * @internal */ export declare const transfer: (from: Semaphore, to: Semaphore, tokens?: number) => Promise; //# sourceMappingURL=semaphore.d.ts.map