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) 2022 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.
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
# ready-resource
|
||||
|
||||
Modern single resource management
|
||||
|
||||
```
|
||||
npm install ready-resource
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
``` js
|
||||
const ReadyResource = require('ready-resource')
|
||||
|
||||
class Thing extends ReadyResource {
|
||||
constructor () {
|
||||
super()
|
||||
}
|
||||
|
||||
async _open () {
|
||||
// open the resource
|
||||
}
|
||||
|
||||
async _close () {
|
||||
// close the resource
|
||||
}
|
||||
}
|
||||
|
||||
const r = new Thing()
|
||||
|
||||
await r.ready() // calls _open once
|
||||
await r.ready() // noop
|
||||
|
||||
await r.close() // calls _close after _open has finished
|
||||
await r.close() // noop
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
declare module 'ready-resource' {
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
/**
|
||||
* Runtime export is CommonJS:
|
||||
* module.exports = class ReadyResource extends EventEmitter {}
|
||||
* This declaration mirrors that shape.
|
||||
*/
|
||||
class ReadyResource extends EventEmitter {
|
||||
constructor();
|
||||
|
||||
/** Set when a call to ready() has started; resolves when _open completes */
|
||||
opening: Promise<void> | null;
|
||||
|
||||
/** Set when a call to close() has started; resolves when _close completes */
|
||||
closing: Promise<void> | null;
|
||||
|
||||
/** True after successful _open */
|
||||
opened: boolean;
|
||||
|
||||
/** True after successful _close (or attempted close after failed open) */
|
||||
closed: boolean;
|
||||
|
||||
/** Ensure the resource is opened; returns the in-flight promise if any */
|
||||
ready(): Promise<void>;
|
||||
|
||||
/** Ensure the resource is closed; returns the in-flight promise if any */
|
||||
close(): Promise<void>;
|
||||
|
||||
/** Override these in subclasses */
|
||||
protected _open(): Promise<void>;
|
||||
protected _close(): Promise<void>;
|
||||
|
||||
// Typed events emitted by the implementation
|
||||
on(event: 'ready', listener: () => void): this;
|
||||
on(event: 'close', listener: () => void): this;
|
||||
once(event: 'ready', listener: () => void): this;
|
||||
once(event: 'close', listener: () => void): this;
|
||||
off(event: 'ready', listener: () => void): this;
|
||||
off(event: 'close', listener: () => void): this;
|
||||
emit(event: 'ready'): boolean;
|
||||
emit(event: 'close'): boolean;
|
||||
|
||||
// Fallback EventEmitter overloads
|
||||
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
||||
once(event: string | symbol, listener: (...args: any[]) => void): this;
|
||||
off(event: string | symbol, listener: (...args: any[]) => void): this;
|
||||
emit(event: string | symbol, ...args: any[]): boolean;
|
||||
}
|
||||
|
||||
// Match CommonJS runtime export
|
||||
export = ReadyResource;
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
const EventEmitter = require('events')
|
||||
|
||||
module.exports = class ReadyResource extends EventEmitter {
|
||||
constructor () {
|
||||
super()
|
||||
|
||||
this.opening = null
|
||||
this.closing = null
|
||||
|
||||
this.opened = false
|
||||
this.closed = false
|
||||
}
|
||||
|
||||
ready () {
|
||||
if (this.opening !== null) return this.opening
|
||||
this.opening = open(this)
|
||||
return this.opening
|
||||
}
|
||||
|
||||
close () {
|
||||
if (this.closing !== null) return this.closing
|
||||
this.closing = close(this)
|
||||
return this.closing
|
||||
}
|
||||
|
||||
async _open () {
|
||||
// add impl here
|
||||
}
|
||||
|
||||
async _close () {
|
||||
// add impl here
|
||||
}
|
||||
}
|
||||
|
||||
async function open (self) {
|
||||
// open after close
|
||||
if (self.closing !== null) return
|
||||
|
||||
try {
|
||||
await self._open()
|
||||
} catch (err) {
|
||||
self.close() // safe to run in bg
|
||||
throw err
|
||||
}
|
||||
|
||||
self.opened = true
|
||||
self.emit('ready')
|
||||
}
|
||||
|
||||
async function close (self) {
|
||||
try {
|
||||
if (self.opened === false && self.opening !== null) await self.opening
|
||||
} catch {
|
||||
// ignore errors on closing
|
||||
}
|
||||
if (self.opened === true || self.opening === null) await self._close()
|
||||
self.closed = true
|
||||
self.emit('close')
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "ready-resource",
|
||||
"version": "1.2.0",
|
||||
"description": "Modern single resource management",
|
||||
"main": "index.js",
|
||||
"imports": {
|
||||
"events": {
|
||||
"bare": "bare-events",
|
||||
"default": "events"
|
||||
}
|
||||
},
|
||||
"types": "index.d.ts",
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "standard && brittle test.js"
|
||||
},
|
||||
"devDependencies": {
|
||||
"brittle": "^3.1.0",
|
||||
"standard": "^17.0.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/holepunchto/ready-resource.git"
|
||||
},
|
||||
"author": "Mathias Buus (@mafintosh)",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/holepunchto/ready-resource/issues"
|
||||
},
|
||||
"homepage": "https://github.com/holepunchto/ready-resource",
|
||||
"dependencies": {
|
||||
"bare-events": "^2.2.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user