async-mutex
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -9,2 +9,9 @@ # Changelog | ||
* Fix documentation for `acquire` | ||
* Fix documentation for `acquire` | ||
## 0.1.2 | ||
* Move to yarn | ||
* Add tslint | ||
* Switch tests to use ES6 | ||
* Add isLocked() |
"use strict"; | ||
var Mutex_1 = require('./Mutex'); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Mutex_1 = require("./Mutex"); | ||
exports.Mutex = Mutex_1.default; |
import MutexInterface from './MutexInterface'; | ||
declare class Mutex implements MutexInterface { | ||
isLocked(): boolean; | ||
acquire(): Promise<MutexInterface.Releaser>; | ||
@@ -4,0 +5,0 @@ runExclusive<T>(callback: MutexInterface.Worker<T>): Promise<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Mutex = (function () { | ||
@@ -7,2 +8,5 @@ function Mutex() { | ||
} | ||
Mutex.prototype.isLocked = function () { | ||
return this._pending; | ||
}; | ||
Mutex.prototype.acquire = function () { | ||
@@ -47,3 +51,2 @@ var _this = this; | ||
}()); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Mutex; |
interface MutexInterface { | ||
acquire(): Promise<MutexInterface.Releaser>; | ||
runExclusive<T>(callback: MutexInterface.Worker<T>): Promise<T>; | ||
isLocked(): boolean; | ||
} | ||
declare module MutexInterface { | ||
declare namespace MutexInterface { | ||
interface Releaser { | ||
@@ -7,0 +8,0 @@ (): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); |
{ | ||
"name": "async-mutex", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A mutex for guarding async workflows", | ||
"scripts": { | ||
"prepublish": "tsc", | ||
"pretest": "typings install && tsc -p tsconfig.test.json", | ||
"test": "mocha -R spec -u tdd test" | ||
"prepublish": "tslint src/**/*.ts test/**/*.ts && tsc", | ||
"pretest": "tslint src/**/*.ts test/**/*.ts && tsc -p tsconfig.test.json", | ||
"test": "mocha -R spec -u tdd build/test" | ||
}, | ||
@@ -21,8 +21,12 @@ "author": "Christian Speckner <cnspeckn@googlemail.com> (https://github.com/DirtyHairy/)", | ||
"devDependencies": { | ||
"mocha": "~3.1.0", | ||
"typescript": "~2.0.3", | ||
"typings": "~1.4.0" | ||
"mocha": "~3.4.2", | ||
"typescript": "~2.4.1" | ||
}, | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts" | ||
"types": "lib/index.d.ts", | ||
"dependencies": { | ||
"@types/mocha": "^2.2.41", | ||
"@types/node": "^8.0.5", | ||
"tslint": "^5.4.3" | ||
} | ||
} |
@@ -108,4 +108,10 @@ [![Build Status](https://travis-ci.org/DirtyHairy/async-mutex.svg?branch=master)](https://travis-ci.org/DirtyHairy/async-mutex) | ||
### Checking whether the mutex is locked | ||
ES5/ES6/Typescript | ||
mutex.isLocked(); | ||
# License | ||
Feel free to use this library under the conditions of the MIT license. |
export {default as Mutex} from './Mutex'; | ||
export {default as MutexInterface} from './MutexInterface'; | ||
export {default as MutexInterface} from './MutexInterface'; |
@@ -5,4 +5,8 @@ import MutexInterface from './MutexInterface'; | ||
isLocked(): boolean { | ||
return this._pending; | ||
} | ||
acquire(): Promise<MutexInterface.Releaser> { | ||
const ticket = new Promise(resolve => this._queue.push(resolve)); | ||
const ticket = new Promise<MutexInterface.Releaser>(resolve => this._queue.push(resolve)); | ||
@@ -45,3 +49,3 @@ if (!this._pending) { | ||
this._pending = true; | ||
this._queue.shift()(this._dispatchNext.bind(this)); | ||
this._queue.shift()!(this._dispatchNext.bind(this)); | ||
} else { | ||
@@ -48,0 +52,0 @@ this._pending = false; |
@@ -7,5 +7,7 @@ interface MutexInterface { | ||
isLocked(): boolean; | ||
} | ||
module MutexInterface { | ||
namespace MutexInterface { | ||
@@ -22,2 +24,2 @@ export interface Releaser { | ||
export default MutexInterface; | ||
export default MutexInterface; |
@@ -1,3 +0,1 @@ | ||
/// <reference path="../typings/index.d.ts"/> | ||
import * as assert from 'assert'; | ||
@@ -55,2 +53,3 @@ | ||
.runExclusive<number>(() => { | ||
// tslint:disable-next-line:no-string-throw | ||
throw 'foo'; | ||
@@ -91,2 +90,29 @@ }) | ||
}); | ||
test('new mutex is unlocked', function() { | ||
assert(!mutex.isLocked()); | ||
}); | ||
test('isLocked reflects the mutex state', async function() { | ||
const lock1 = mutex.acquire(), | ||
lock2 = mutex.acquire(); | ||
assert(mutex.isLocked()); | ||
const releaser1 = await lock1; | ||
assert(mutex.isLocked()); | ||
releaser1(); | ||
assert(mutex.isLocked()); | ||
const releaser2 = await lock2; | ||
assert(mutex.isLocked()); | ||
releaser2(); | ||
assert(!mutex.isLocked()); | ||
}); | ||
}); |
@@ -6,7 +6,6 @@ { | ||
"sourceMap": false, | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"strict": true, | ||
"declaration": true, | ||
"outDir": "lib", | ||
"lib": ["es5", "es6"] | ||
"lib": ["es6"] | ||
}, | ||
@@ -13,0 +12,0 @@ "include": ["src/**/*.ts"], |
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"target": "es6", | ||
"strict": true, | ||
"sourceMap": false, | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"lib": ["es5", "es6", "dom"] | ||
"outDir": "build" | ||
}, | ||
@@ -10,0 +9,0 @@ "include": ["test/**/*.ts"], |
Sorry, the diff of this file is not supported yet
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
38855
2
21
323
116
3
+ Added@types/mocha@^2.2.41
+ Added@types/node@^8.0.5
+ Addedtslint@^5.4.3
+ Added@babel/code-frame@7.24.7(transitive)
+ Added@babel/helper-validator-identifier@7.24.7(transitive)
+ Added@babel/highlight@7.24.7(transitive)
+ Added@types/mocha@2.2.48(transitive)
+ Added@types/node@8.10.66(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbuiltin-modules@1.1.1(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddiff@4.0.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-core-module@2.15.1(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpicocolors@1.1.0(transitive)
+ Addedresolve@1.22.8(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedtslib@1.14.1(transitive)
+ Addedtslint@5.20.1(transitive)
+ Addedtsutils@2.29.0(transitive)
+ Addedtypescript@5.6.2(transitive)
+ Addedwrappy@1.0.2(transitive)