fix: multi-repo local dev (file:../ deps, package exports, missing deps)
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2020 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.
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
# signal-promise
|
||||
|
||||
Simple wait/notify promise with optional max wait time
|
||||
|
||||
```
|
||||
npm install signal-promise
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
``` js
|
||||
const Signal = require('signal-promise')
|
||||
|
||||
const input = new Signal()
|
||||
|
||||
while (await input.wait()) {
|
||||
console.log('someone typed!')
|
||||
}
|
||||
|
||||
process.stdin.on('stdin', function () {
|
||||
input.notify()
|
||||
})
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
#### `s = new Signal()`
|
||||
|
||||
Make a new signal
|
||||
|
||||
#### `await s.wait([maxWaitTime])`
|
||||
|
||||
Wait for someone to call notify. If you specify `maxWaitTime`
|
||||
the promise will resolve after `maxWaitTime` ms if no notify call has happened.
|
||||
|
||||
As a convenience the promise resolves to `true` so it's easy to use it as a condition.
|
||||
|
||||
#### `s.notify([error])`
|
||||
|
||||
Notify everyone waiting. If you pass an error the wait promise
|
||||
will reject with that error.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
const Signal = require('./')
|
||||
|
||||
const s = new Signal()
|
||||
|
||||
main().catch(console.error)
|
||||
|
||||
async function main () {
|
||||
while (await s.wait(2000)) {
|
||||
console.log('nu!')
|
||||
}
|
||||
console.log('efter')
|
||||
}
|
||||
|
||||
process.stdin.on('data', function () {
|
||||
s.notify()
|
||||
})
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
module.exports = class Signal {
|
||||
constructor () {
|
||||
this._resolve = null
|
||||
this._reject = null
|
||||
this._promise = null
|
||||
this._bind = bind.bind(this)
|
||||
this._onerror = clear.bind(this)
|
||||
this._onsuccess = clear.bind(this, null)
|
||||
this._timers = new Set()
|
||||
}
|
||||
|
||||
wait (max) {
|
||||
if (!this._promise) {
|
||||
this._promise = new Promise(this._bind)
|
||||
this._promise.then(this._onsuccess).catch(this._onerror)
|
||||
}
|
||||
if (max) return this._sleep(max)
|
||||
return this._promise
|
||||
}
|
||||
|
||||
_sleep (max) {
|
||||
const s = new Promise((resolve, reject) => {
|
||||
const done = () => {
|
||||
this._timers.delete(state)
|
||||
resolve(true)
|
||||
}
|
||||
const id = setTimeout(done, max)
|
||||
const state = { id, resolve, reject }
|
||||
this._timers.add(state)
|
||||
})
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
notify (err) {
|
||||
if (!this._promise) return
|
||||
const resolve = this._resolve
|
||||
const reject = this._reject
|
||||
this._promise = null
|
||||
if (err) reject(err)
|
||||
else resolve(true)
|
||||
}
|
||||
}
|
||||
|
||||
function clear (err) {
|
||||
for (const { id, resolve, reject } of this._timers) {
|
||||
clearTimeout(id)
|
||||
if (err) reject(err)
|
||||
else resolve(true)
|
||||
}
|
||||
this._timers.clear()
|
||||
}
|
||||
|
||||
function bind (resolve, reject) {
|
||||
this._resolve = resolve
|
||||
this._reject = reject
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "signal-promise",
|
||||
"version": "1.0.3",
|
||||
"description": "Simple wait/notify promise with optional max wait time",
|
||||
"main": "index.js",
|
||||
"dependencies": {},
|
||||
"devDependencies": {},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mafintosh/signal-promise.git"
|
||||
},
|
||||
"author": "Mathias Buus (@mafintosh)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mafintosh/signal-promise/issues"
|
||||
},
|
||||
"homepage": "https://github.com/mafintosh/signal-promise"
|
||||
}
|
||||
Reference in New Issue
Block a user