Skip to content

Commit

Permalink
Add type definitions and jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTheFiveNine committed Jul 3, 2021
1 parent 281c622 commit 0077625
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@robthefivenine/mutex.js",
"version": "1.0.1",
"version": "1.0.2",
"description": "A mutex library that enables you to synchronise access to resources across an application.",
"keywords": [
"mutex"
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
Expand All @@ -32,7 +33,7 @@
"rimraf": "^3.0.2"
},
"scripts": {
"build": "babel src --out-dir dist",
"build": "babel src --out-dir dist && cp src/*.ts dist/",
"clean": "rimraf dist",
"lint": "eslint src/",
"prepack": "yarn run clean && yarn build",
Expand Down
61 changes: 61 additions & 0 deletions src/Mutex.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export = Mutex;

declare class Mutex {
/**
* Gets the globally registered mutex corresponding to `name`.
* If a mutex has not already been created for `name`, one will
* be created and returned.
*
* @param name The name of the mutex to retrieve.
*/
static getMutex(name: string): Mutex;

/**
* Creates a new Mutex object.
*
* @param name An optional name used to identify
* the resource the mutex locks.
*/
constructor(name?: string);

/**
* Attempts to acquire a lock on the mutex. If the lock can be
* acquired, it will set `.isLocked` to `true`, and will resolve
* the promise.
*
* If the optional argument, `timeout`, is `0`, the call will wait
* indefinitely until the lock is released.
*
* If `timeout` is a value greater than `0`, the promise will be
* rejected after the number of milliseconds specified in
* `timeout` have elapsed.
*
* @param timeout The number of milliseconds to wait for the lock
* to be acquired.
*/
wait(timeout?: number): Promise<void>;

/**
* Releases the existing lock. If there are any consumers that have
* called `.wait(timeout)` and are still waiting, the first to have
* called `.wait(timeout)` will acquire the lock.
*
* Once there are no more consumers waiting to acquire the lock,
* `.isLocked` will be set to `false`.
*
* **Note**: it is important to always call `.release()` once
* finished accessing the resource. If not, nothing else will
* be able to acquire the lock.
*/
release(): void;

/**
* Returns a boolean value indicating whether or not the mutex is locked.
*/
get isLocked(): boolean;

/**
* Returns the name of the mutex.
*/
get name(): string;
}
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Mutex };
import Mutex = require("./Mutex");

0 comments on commit 0077625

Please sign in to comment.