async-mutex
Advanced tools
Comparing version 0.2.3 to 0.2.4
# Changelog | ||
## 0.2.4 | ||
* Calling Semaphore::release on a semaphore with concurrency > 1 will not work | ||
as expected; throw an exception in this case | ||
* Make the warning on using Semaphore::release and Mutex::release more prominent | ||
## 0.2.3 | ||
@@ -4,0 +10,0 @@ |
import { __awaiter, __generator } from "tslib"; | ||
var Semaphore = /** @class */ (function () { | ||
function Semaphore(_value) { | ||
this._value = _value; | ||
function Semaphore(_maxConcurrency) { | ||
this._maxConcurrency = _maxConcurrency; | ||
this._queue = []; | ||
if (_value <= 0) { | ||
if (_maxConcurrency <= 0) { | ||
throw new Error('semaphore must be initialized to a positive value'); | ||
} | ||
this._value = _maxConcurrency; | ||
} | ||
@@ -43,2 +44,5 @@ Semaphore.prototype.acquire = function () { | ||
Semaphore.prototype.release = function () { | ||
if (this._maxConcurrency > 1) { | ||
throw new Error('this method is unavailabel on semaphores with concurrency > 1; use the scoped release returned by acquire instead'); | ||
} | ||
if (this._currentReleaser) { | ||
@@ -45,0 +49,0 @@ this._currentReleaser(); |
import SemaphoreInterface from './SemaphoreInterface'; | ||
declare class Semaphore implements SemaphoreInterface { | ||
private _value; | ||
constructor(_value: number); | ||
private _maxConcurrency; | ||
constructor(_maxConcurrency: number); | ||
acquire(): Promise<[number, SemaphoreInterface.Releaser]>; | ||
@@ -12,3 +12,4 @@ runExclusive<T>(callback: SemaphoreInterface.Worker<T>): Promise<T>; | ||
private _currentReleaser; | ||
private _value; | ||
} | ||
export default Semaphore; |
@@ -5,8 +5,9 @@ "use strict"; | ||
var Semaphore = /** @class */ (function () { | ||
function Semaphore(_value) { | ||
this._value = _value; | ||
function Semaphore(_maxConcurrency) { | ||
this._maxConcurrency = _maxConcurrency; | ||
this._queue = []; | ||
if (_value <= 0) { | ||
if (_maxConcurrency <= 0) { | ||
throw new Error('semaphore must be initialized to a positive value'); | ||
} | ||
this._value = _maxConcurrency; | ||
} | ||
@@ -46,2 +47,5 @@ Semaphore.prototype.acquire = function () { | ||
Semaphore.prototype.release = function () { | ||
if (this._maxConcurrency > 1) { | ||
throw new Error('this method is unavailabel on semaphores with concurrency > 1; use the scoped release returned by acquire instead'); | ||
} | ||
if (this._currentReleaser) { | ||
@@ -48,0 +52,0 @@ this._currentReleaser(); |
{ | ||
"name": "async-mutex", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "A mutex for guarding async workflows", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -136,4 +136,8 @@ [![Build Status](https://travis-ci.org/DirtyHairy/async-mutex.svg?branch=master)](https://travis-ci.org/DirtyHairy/async-mutex) | ||
A locked mutex can also be released by calling the `release` method on the mutex: | ||
A locked mutex can also be released by calling the `release` method on the mutex. This will | ||
release the current lock on the mutex. | ||
**WARNING:** Using this API comes with the inherent danger of releasing a mutex locked | ||
in an entirely unrelated place. Use with care. | ||
Promise style: | ||
@@ -146,2 +150,3 @@ ```typescript | ||
// Please read and understand the WARNING above before using this API. | ||
mutex.release(); | ||
@@ -157,2 +162,3 @@ }); | ||
} finally { | ||
// Please read and understand the WARNING above before using this API. | ||
mutex.release(); | ||
@@ -162,5 +168,2 @@ } | ||
**WARNING:** Using this API comes with the inherent danger of releasing a mutex locked | ||
in an entirely unrelated place. Use with care. | ||
### Synchronized code execution | ||
@@ -246,4 +249,9 @@ | ||
A locked semaphore can also be released by calling the `release` method on the semaphore: | ||
A locked semaphore can also be released by calling the `release` method on the semaphore. | ||
This will release the most recent lock on the semaphore. As such, this will only work with | ||
semaphores with `maxValue == 1`. Calling this on other semaphores will throw an exception. | ||
**WARNING:** Using this API comes with the inherent danger of releasing a semaphore locked | ||
in an entirely unrelated place. Use with care. | ||
Promise style: | ||
@@ -256,2 +264,3 @@ ```typescript | ||
// Please read and understand the WARNING above before using this API. | ||
semaphore.release(); | ||
@@ -267,2 +276,3 @@ }); | ||
} finally { | ||
// Please read and understand the WARNING above before using this API. | ||
semaphore.release(); | ||
@@ -272,5 +282,2 @@ } | ||
**WARNING:** Using this API comes with the inherent danger of releasing a semaphore locked | ||
in an entirely unrelated place. Use with care. | ||
### Synchronized code execution | ||
@@ -277,0 +284,0 @@ |
Sorry, the diff of this file is not supported yet
37148
566
337