Comparing version 1.2.0 to 1.3.0
@@ -1,2 +0,2 @@ | ||
var mutexify = function() { | ||
var mutexify = function () { | ||
var queue = [] | ||
@@ -3,0 +3,0 @@ var used = null |
{ | ||
"name": "mutexify", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "mutex lock for javascript", | ||
@@ -8,6 +8,9 @@ "main": "index.js", | ||
"devDependencies": { | ||
"standard": "^14.3.3", | ||
"tape": "^3.0.2" | ||
}, | ||
"scripts": { | ||
"test": "tape test.js" | ||
"test": "tape test.js", | ||
"posttest": "npm run lint", | ||
"lint": "standard" | ||
}, | ||
@@ -14,0 +17,0 @@ "repository": { |
@@ -48,4 +48,26 @@ # mutexify | ||
### Usage with promises | ||
`mutexify` provides a Promise-based alternative. | ||
```js | ||
const mutexify = require('mutexify/promise') | ||
;(async () => { | ||
var lock = mutexify() | ||
var release = await lock() | ||
console.log('i am now locked') | ||
setTimeout(function () { | ||
release() | ||
}, 1000) | ||
release = await lock() | ||
console.log('1 second later') | ||
release() | ||
})() | ||
``` | ||
## License | ||
MIT |
37
test.js
var tape = require('tape') | ||
var mutexify = require('./') | ||
var mutexifyPromise = require('./promise') | ||
tape('locks', function(t) { | ||
tape('locks', function (t) { | ||
t.plan(21) | ||
@@ -12,7 +13,7 @@ | ||
for (var i = 0; i < 10; i++) { | ||
lock(function(release) { | ||
lock(function (release) { | ||
t.ok(!used, 'one at the time') | ||
t.ok(lock.locked, 'locked') | ||
used = true | ||
setImmediate(function() { | ||
setImmediate(function () { | ||
used = false | ||
@@ -25,6 +26,6 @@ release() | ||
tape('calls callback', function(t) { | ||
tape('calls callback', function (t) { | ||
var lock = mutexify() | ||
var cb = function(err, value) { | ||
var cb = function (err, value) { | ||
t.same(err, null) | ||
@@ -35,3 +36,3 @@ t.same(value, 'hello world') | ||
lock(function(release) { | ||
lock(function (release) { | ||
release(cb, null, 'hello world') | ||
@@ -41,3 +42,3 @@ }) | ||
tape('calls the locking callbacks in a different stack', function(t) { | ||
tape('calls the locking callbacks in a different stack', function (t) { | ||
t.plan(2) | ||
@@ -50,3 +51,3 @@ | ||
lock(function(release) { | ||
lock(function (release) { | ||
t.ok(topScopeFinished, 'the test function has already finished running') | ||
@@ -57,3 +58,3 @@ release() | ||
lock(function(release) { | ||
lock(function (release) { | ||
t.ok(secondScopeFinished, 'the last lock\'s call stack is done') | ||
@@ -66,1 +67,19 @@ release() | ||
}) | ||
tape('locks with promises', async function (t) { | ||
t.plan(21) | ||
var lock = mutexifyPromise() | ||
var used = false | ||
t.ok(!lock.locked, 'not locked') | ||
for (var i = 0; i < 10; i++) { | ||
var release = await lock() | ||
t.ok(!used, 'one at the time') | ||
t.ok(lock.locked, 'locked') | ||
used = true | ||
setImmediate(function () { | ||
used = false | ||
release() | ||
}) | ||
} | ||
}) |
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
5633
100
73
2