@hortemo/semaphore
Version:
A minimal FIFO semaphore for JS/TS.
21 lines (19 loc) • 721 B
text/typescript
/** Function returned by {@link Semaphore.acquire}. Call once to return the permit. */
type Releaser = () => void;
/** Promise-based FIFO semaphore. */
declare class Semaphore {
private _permits;
private _waitQueue;
/**
* Create a semaphore.
*
* @param permits Non-negative initial permit count.
* @throws {Error} If `permits` is not a non-negative integer.
*/
constructor(permits: number);
/** Resolve with a {@link Releaser} when a permit is available. Call it once to return the permit. */
acquire(): Promise<Releaser>;
/** Try to grant permits to the wait queue while any are available. */
private _dispatch;
}
export { type Releaser, Semaphore as default };