async-mutex
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -26,2 +26,5 @@ import { __awaiter, __generator } from "tslib"; | ||
}; | ||
Mutex.prototype.waitForUnlock = function () { | ||
return this._semaphore.waitForUnlock(); | ||
}; | ||
/** @deprecated Deprecated in 0.3.0, will be removed in 0.4.0. Use runExclusive instead. */ | ||
@@ -28,0 +31,0 @@ Mutex.prototype.release = function () { |
@@ -9,2 +9,3 @@ import { __awaiter, __generator } from "tslib"; | ||
this._queue = []; | ||
this._waiters = []; | ||
if (_maxConcurrency <= 0) { | ||
@@ -46,2 +47,15 @@ throw new Error('semaphore must be initialized to a positive value'); | ||
}; | ||
Semaphore.prototype.waitForUnlock = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var waitPromise; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
if (!this.isLocked()) { | ||
return [2 /*return*/, Promise.resolve()]; | ||
} | ||
waitPromise = new Promise(function (resolve) { return _this._waiters.push({ resolve: resolve }); }); | ||
return [2 /*return*/, waitPromise]; | ||
}); | ||
}); | ||
}; | ||
Semaphore.prototype.isLocked = function () { | ||
@@ -77,2 +91,3 @@ return this._value <= 0; | ||
_this._value++; | ||
_this._resolveWaiters(); | ||
_this._dispatch(); | ||
@@ -82,4 +97,8 @@ }; | ||
}; | ||
Semaphore.prototype._resolveWaiters = function () { | ||
this._waiters.forEach(function (waiter) { return waiter.resolve(); }); | ||
this._waiters = []; | ||
}; | ||
return Semaphore; | ||
}()); | ||
export default Semaphore; |
@@ -83,4 +83,5 @@ import { __awaiter, __generator } from "tslib"; | ||
}, | ||
waitForUnlock: function () { return sync.waitForUnlock(); }, | ||
isLocked: function () { return sync.isLocked(); }, | ||
}; | ||
} |
@@ -13,2 +13,2 @@ "use strict"; | ||
Object.defineProperty(exports, "tryAcquire", { enumerable: true, get: function () { return tryAcquire_1.tryAcquire; } }); | ||
tslib_1.__exportStar(require("./errors"), exports); | ||
(0, tslib_1.__exportStar)(require("./errors"), exports); |
@@ -7,2 +7,3 @@ import MutexInterface from './MutexInterface'; | ||
isLocked(): boolean; | ||
waitForUnlock(): Promise<void>; | ||
/** @deprecated Deprecated in 0.3.0, will be removed in 0.4.0. Use runExclusive instead. */ | ||
@@ -9,0 +10,0 @@ release(): void; |
@@ -10,5 +10,5 @@ "use strict"; | ||
Mutex.prototype.acquire = function () { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
var _a, releaser; | ||
return tslib_1.__generator(this, function (_b) { | ||
return (0, tslib_1.__generator)(this, function (_b) { | ||
switch (_b.label) { | ||
@@ -29,2 +29,5 @@ case 0: return [4 /*yield*/, this._semaphore.acquire()]; | ||
}; | ||
Mutex.prototype.waitForUnlock = function () { | ||
return this._semaphore.waitForUnlock(); | ||
}; | ||
/** @deprecated Deprecated in 0.3.0, will be removed in 0.4.0. Use runExclusive instead. */ | ||
@@ -31,0 +34,0 @@ Mutex.prototype.release = function () { |
interface MutexInterface { | ||
acquire(): Promise<MutexInterface.Releaser>; | ||
runExclusive<T>(callback: MutexInterface.Worker<T>): Promise<T>; | ||
waitForUnlock(): Promise<void>; | ||
isLocked(): boolean; | ||
@@ -5,0 +6,0 @@ /** @deprecated Deprecated in 0.3.0, will be removed in 0.4.0. Use runExclusive instead. */ |
@@ -8,2 +8,3 @@ import SemaphoreInterface from './SemaphoreInterface'; | ||
runExclusive<T>(callback: SemaphoreInterface.Worker<T>): Promise<T>; | ||
waitForUnlock(): Promise<void>; | ||
isLocked(): boolean; | ||
@@ -14,3 +15,5 @@ /** @deprecated Deprecated in 0.3.0, will be removed in 0.4.0. Use runExclusive instead. */ | ||
private _dispatch; | ||
private _resolveWaiters; | ||
private _queue; | ||
private _waiters; | ||
private _currentReleaser; | ||
@@ -17,0 +20,0 @@ private _value; |
@@ -11,2 +11,3 @@ "use strict"; | ||
this._queue = []; | ||
this._waiters = []; | ||
if (_maxConcurrency <= 0) { | ||
@@ -28,5 +29,5 @@ throw new Error('semaphore must be initialized to a positive value'); | ||
Semaphore.prototype.runExclusive = function (callback) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
var _a, value, release; | ||
return tslib_1.__generator(this, function (_b) { | ||
return (0, tslib_1.__generator)(this, function (_b) { | ||
switch (_b.label) { | ||
@@ -49,2 +50,15 @@ case 0: return [4 /*yield*/, this.acquire()]; | ||
}; | ||
Semaphore.prototype.waitForUnlock = function () { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
var waitPromise; | ||
var _this = this; | ||
return (0, tslib_1.__generator)(this, function (_a) { | ||
if (!this.isLocked()) { | ||
return [2 /*return*/, Promise.resolve()]; | ||
} | ||
waitPromise = new Promise(function (resolve) { return _this._waiters.push({ resolve: resolve }); }); | ||
return [2 /*return*/, waitPromise]; | ||
}); | ||
}); | ||
}; | ||
Semaphore.prototype.isLocked = function () { | ||
@@ -80,2 +94,3 @@ return this._value <= 0; | ||
_this._value++; | ||
_this._resolveWaiters(); | ||
_this._dispatch(); | ||
@@ -85,4 +100,8 @@ }; | ||
}; | ||
Semaphore.prototype._resolveWaiters = function () { | ||
this._waiters.forEach(function (waiter) { return waiter.resolve(); }); | ||
this._waiters = []; | ||
}; | ||
return Semaphore; | ||
}()); | ||
exports.default = Semaphore; |
interface SemaphoreInterface { | ||
acquire(): Promise<[number, SemaphoreInterface.Releaser]>; | ||
runExclusive<T>(callback: SemaphoreInterface.Worker<T>): Promise<T>; | ||
waitForUnlock(): Promise<void>; | ||
isLocked(): boolean; | ||
@@ -5,0 +6,0 @@ /** @deprecated Deprecated in 0.3.0, will be removed in 0.4.0. Use runExclusive instead. */ |
@@ -10,4 +10,4 @@ "use strict"; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return withTimeout_1.withTimeout(sync, 0, alreadyAcquiredError); | ||
return (0, withTimeout_1.withTimeout)(sync, 0, alreadyAcquiredError); | ||
} | ||
exports.tryAcquire = tryAcquire; |
@@ -12,5 +12,5 @@ "use strict"; | ||
acquire: function () { | ||
return new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
return new Promise(function (resolve, reject) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { | ||
var isTimeout, handle, ticket, release, e_1; | ||
return tslib_1.__generator(this, function (_a) { | ||
return (0, tslib_1.__generator)(this, function (_a) { | ||
switch (_a.label) { | ||
@@ -51,5 +51,5 @@ case 0: | ||
runExclusive: function (callback) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () { | ||
var release, ticket; | ||
return tslib_1.__generator(this, function (_a) { | ||
return (0, tslib_1.__generator)(this, function (_a) { | ||
switch (_a.label) { | ||
@@ -88,2 +88,3 @@ case 0: | ||
}, | ||
waitForUnlock: function () { return sync.waitForUnlock(); }, | ||
isLocked: function () { return sync.isLocked(); }, | ||
@@ -90,0 +91,0 @@ }; |
{ | ||
"name": "async-mutex", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "A mutex for guarding async workflows", | ||
@@ -57,18 +57,18 @@ "scripts": { | ||
"devDependencies": { | ||
"@sinonjs/fake-timers": "^6.0.1", | ||
"@types/mocha": "^8.2.0", | ||
"@types/node": "^14.14.25", | ||
"@types/sinonjs__fake-timers": "^6.0.2", | ||
"@typescript-eslint/eslint-plugin": "^4.14.2", | ||
"@typescript-eslint/parser": "^4.14.2", | ||
"coveralls": "^3.1.0", | ||
"eslint": "^7.19.0", | ||
"@sinonjs/fake-timers": "^7.1.2", | ||
"@types/mocha": "^9.0.0", | ||
"@types/node": "^16.7.8", | ||
"@types/sinonjs__fake-timers": "^6.0.3", | ||
"@typescript-eslint/eslint-plugin": "^4.30.0", | ||
"@typescript-eslint/parser": "^4.30.0", | ||
"coveralls": "^3.1.1", | ||
"eslint": "^7.32.0", | ||
"import-sort-style-eslint": "^6.0.0", | ||
"mocha": "^8.2.1", | ||
"mocha": "^9.1.1", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.2.1", | ||
"prettier-plugin-import-sort": "^0.0.6", | ||
"rollup": "^2.38.5", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.1.3" | ||
"prettier": "^2.3.2", | ||
"prettier-plugin-import-sort": "^0.0.7", | ||
"rollup": "^2.56.3", | ||
"ts-node": "^10.2.1", | ||
"typescript": "^4.4.2" | ||
}, | ||
@@ -87,4 +87,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"tslib": "^2.1.0" | ||
"tslib": "^2.3.1" | ||
} | ||
} |
@@ -212,3 +212,3 @@ [![Build status](https://github.com/DirtyHairy/async-mutex/workflows/Build%20and%20Tests/badge.svg)](https://github.com/DirtyHairy/async-mutex/actions?query=workflow%3A%22Build+and+Tests%22) | ||
This works with `aquire`, too: | ||
This works with `acquire`, too: | ||
if `acquire` is used for locking, the resulting promise will reject with `E_CANCELED`. | ||
@@ -226,2 +226,25 @@ | ||
### Waiting until the mutex is available | ||
You can wait until the mutex is available without locking it by calling `waitForUnlock()`. | ||
This will return a promise that resolve once the mutex can be acquired again. This operation | ||
will not lock the mutex, and there is no gurantee that the mutex will still be available | ||
once a async barrier has been encountered. | ||
Promise style: | ||
```typescript | ||
mutex | ||
.waitForUnlock() | ||
.then(() => { | ||
// ... | ||
}); | ||
``` | ||
Async/await: | ||
```typescript | ||
await mutex.waitForUnlock(); | ||
// ... | ||
``` | ||
## Semaphore API | ||
@@ -363,2 +386,24 @@ | ||
### Waiting until the semaphore is available | ||
You can wait until the semaphore is available without locking it by calling `waitForUnlock()`. | ||
This will return a promise that resolve once the semaphore can be acquired again. This operation | ||
will not lock the semaphore, and there is no gurantee that the semaphore will still be available | ||
once a async barrier has been encountered. | ||
Promise style: | ||
```typescript | ||
semaphore | ||
.waitForUnlock() | ||
.then(() => { | ||
// ... | ||
}); | ||
``` | ||
Async/await: | ||
```typescript | ||
await semaphore.waitForUnlock(); | ||
// ... | ||
``` | ||
## Limiting the time waiting for a mutex or semaphore to become available | ||
@@ -372,3 +417,3 @@ | ||
```typescript | ||
import {withTimeout, E_TIMEOUT} from 'asymc-mutex`; | ||
import {withTimeout, E_TIMEOUT} from 'async-mutex'; | ||
@@ -403,3 +448,3 @@ const mutexWithTimeout = withTimeout(new Mutex(), 100); | ||
```typescript | ||
import {tryAcquire, E_ALREADY_LOCKED} from 'asymc-mutex`; | ||
import {tryAcquire, E_ALREADY_LOCKED} from 'async-mutex'; | ||
@@ -423,3 +468,3 @@ tryAcquire(semaphoreOrMutex) | ||
```typescript | ||
import {tryAcquire, E_ALREADY_LOCKED} from 'asymc-mutex`; | ||
import {tryAcquire, E_ALREADY_LOCKED} from 'async-mutex'; | ||
@@ -431,3 +476,3 @@ try { | ||
} catch (e) { | ||
if (e === E_NOT_AVAILABLE { | ||
if (e === E_ALREADY_LOCKED) { | ||
// ... | ||
@@ -434,0 +479,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49987
792
483
28
Updatedtslib@^2.3.1