fix: multi-repo local dev (file:../ deps, package exports, missing deps)
This commit is contained in:
+7
@@ -0,0 +1,7 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- '10'
|
||||
- '12'
|
||||
- '14'
|
||||
cache:
|
||||
npm: false
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Mathias Buus
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
# mutexify
|
||||
|
||||
Bike shed mutex lock implementation in node.js
|
||||
|
||||
```
|
||||
npm install mutexify
|
||||
```
|
||||
|
||||
[](http://travis-ci.org/mafintosh/mutexify)
|
||||
|
||||
Hasn't this been done before? Yes, but the specific semantics of this made some of my code simpler.
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
``` js
|
||||
var mutexify = require('mutexify')
|
||||
var lock = mutexify()
|
||||
|
||||
lock(function(release) {
|
||||
console.log('i am now locked')
|
||||
setTimeout(function() {
|
||||
release()
|
||||
}, 1000)
|
||||
})
|
||||
|
||||
lock(function(release) {
|
||||
console.log('1 second later')
|
||||
release()
|
||||
})
|
||||
```
|
||||
|
||||
A common pattern is to call a callback after you release the lock.
|
||||
To do this in a one-liner pass the callback and the value to `release(cb, err, value)`
|
||||
|
||||
``` js
|
||||
var write = function(data, cb) {
|
||||
lock(function(release) {
|
||||
fs.writeFile('locked-file.txt', data, release.bind(null, cb))
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
`mutexify` guarantees that the order that a mutex was requested in is the order that access will be given.
|
||||
|
||||
You can read the lock's current state on the `lock.locked` property.
|
||||
|
||||
### 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
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
var queueTick = require('queue-tick')
|
||||
|
||||
var mutexify = function () {
|
||||
var queue = []
|
||||
var used = null
|
||||
|
||||
var call = function () {
|
||||
used(release)
|
||||
}
|
||||
|
||||
var acquire = function (fn) {
|
||||
if (used) return queue.push(fn)
|
||||
used = fn
|
||||
acquire.locked = true
|
||||
queueTick(call)
|
||||
return 0
|
||||
}
|
||||
|
||||
acquire.locked = false
|
||||
|
||||
var release = function (fn, err, value) {
|
||||
used = null
|
||||
acquire.locked = false
|
||||
if (queue.length) acquire(queue.shift())
|
||||
if (fn) fn(err, value)
|
||||
}
|
||||
|
||||
return acquire
|
||||
}
|
||||
|
||||
module.exports = mutexify
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "mutexify",
|
||||
"version": "1.4.0",
|
||||
"description": "mutex lock for javascript",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"queue-tick": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"standard": "^14.3.3",
|
||||
"tape": "^3.0.2"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tape test.js",
|
||||
"posttest": "npm run lint",
|
||||
"lint": "standard"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mafintosh/mutexify.git"
|
||||
},
|
||||
"author": "Mathias Buus (@mafintosh)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mafintosh/mutexify/issues"
|
||||
},
|
||||
"keywords": [
|
||||
"mutex",
|
||||
"lock"
|
||||
],
|
||||
"homepage": "https://github.com/mafintosh/mutexify"
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
var mutexify = require('.')
|
||||
|
||||
var mutexifyPromise = function () {
|
||||
var lock = mutexify()
|
||||
|
||||
var acquire = function () {
|
||||
return new Promise(lock)
|
||||
}
|
||||
|
||||
Object.defineProperty(acquire, 'locked', {
|
||||
get: function () { return lock.locked },
|
||||
enumerable: true
|
||||
})
|
||||
|
||||
return acquire
|
||||
}
|
||||
|
||||
module.exports = mutexifyPromise
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
var tape = require('tape')
|
||||
var mutexify = require('./')
|
||||
var mutexifyPromise = require('./promise')
|
||||
|
||||
tape('locks', function (t) {
|
||||
t.plan(21)
|
||||
|
||||
var lock = mutexify()
|
||||
var used = false
|
||||
t.ok(!lock.locked, 'not locked')
|
||||
|
||||
for (var i = 0; i < 10; i++) {
|
||||
lock(function (release) {
|
||||
t.ok(!used, 'one at the time')
|
||||
t.ok(lock.locked, 'locked')
|
||||
used = true
|
||||
setImmediate(function () {
|
||||
used = false
|
||||
release()
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
tape('calls callback', function (t) {
|
||||
var lock = mutexify()
|
||||
|
||||
var cb = function (err, value) {
|
||||
t.same(err, null)
|
||||
t.same(value, 'hello world')
|
||||
t.end()
|
||||
}
|
||||
|
||||
lock(function (release) {
|
||||
release(cb, null, 'hello world')
|
||||
})
|
||||
})
|
||||
|
||||
tape('calls the locking callbacks in a different stack', function (t) {
|
||||
t.plan(2)
|
||||
|
||||
var lock = mutexify()
|
||||
|
||||
var topScopeFinished = false
|
||||
var secondScopeFinished = false
|
||||
|
||||
lock(function (release) {
|
||||
t.ok(topScopeFinished, 'the test function has already finished running')
|
||||
release()
|
||||
secondScopeFinished = true
|
||||
})
|
||||
|
||||
lock(function (release) {
|
||||
t.ok(secondScopeFinished, 'the last lock\'s call stack is done')
|
||||
release()
|
||||
t.end()
|
||||
})
|
||||
|
||||
topScopeFinished = true
|
||||
})
|
||||
|
||||
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()
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user