Fix Plex data transformation, handle socket errors
This commit is contained in:
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "paravel",
|
"name": "paravel",
|
||||||
"version": "0.1.7",
|
"version": "0.1.9",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "paravel",
|
"name": "paravel",
|
||||||
"version": "0.1.7",
|
"version": "0.1.9",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"husky": "^8.0.3",
|
"husky": "^8.0.3",
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "paravel",
|
"name": "paravel",
|
||||||
"version": "0.1.7",
|
"version": "0.1.9",
|
||||||
"description": "Yet another toolkit for nostr",
|
"description": "Yet another toolkit for nostr",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "node build.js",
|
"build": "node build.js",
|
||||||
"pub": "npm i && node build.js && npm publish",
|
"pub": "npm i && npm run check && node build.js && npm publish",
|
||||||
"check:ts": "tsc --noEmit --esModuleInterop --strict src/**/*",
|
"check:ts": "tsc --noEmit --esModuleInterop --strict src/**/*",
|
||||||
"check:es": "eslint src/*",
|
"check:es": "eslint src/*",
|
||||||
"check": "run-p check:*"
|
"check": "run-p check:*"
|
||||||
|
|||||||
+5
-5
@@ -5,10 +5,10 @@ export class Plex {
|
|||||||
this.urls = urls
|
this.urls = urls
|
||||||
this.socket = socket
|
this.socket = socket
|
||||||
this.bus = new EventBus()
|
this.bus = new EventBus()
|
||||||
this.listeners = sockets.map(socket => {
|
this.unsubscribe = socket.bus.addListeners({
|
||||||
return socket.bus.addListener('message', (url, [verb, ...payload]) => {
|
message: (websocketUrl, [{relays}, [verb, ...payload]]) => {
|
||||||
this.bus.emit(verb, url, ...payload)
|
this.bus.emit(verb, relays[0], ...payload)
|
||||||
})
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
async send(...payload) {
|
async send(...payload) {
|
||||||
@@ -18,6 +18,6 @@ export class Plex {
|
|||||||
}
|
}
|
||||||
cleanup() {
|
cleanup() {
|
||||||
this.bus.clear()
|
this.bus.clear()
|
||||||
this.listeners.map(unsubscribe => unsubscribe())
|
this.unsubscribe()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-14
@@ -5,13 +5,14 @@ import {Deferred, defer} from "./Deferred"
|
|||||||
export class Socket {
|
export class Socket {
|
||||||
ws?: WebSocket
|
ws?: WebSocket
|
||||||
url: string
|
url: string
|
||||||
ready?: Deferred<void>
|
ready: Deferred<void>
|
||||||
timeout?: NodeJS.Timeout
|
timeout?: NodeJS.Timeout
|
||||||
queue: string[]
|
queue: string[]
|
||||||
bus: EventBus
|
bus: EventBus
|
||||||
status: string
|
status: string
|
||||||
_onOpen: (e: any) => void
|
_onOpen: (e: any) => void
|
||||||
_onMessage: (e: any) => void
|
_onMessage: (e: any) => void
|
||||||
|
_onError: (e: any) => void
|
||||||
_onClose: (e: any) => void
|
_onClose: (e: any) => void
|
||||||
static STATUS = {
|
static STATUS = {
|
||||||
NEW: "new",
|
NEW: "new",
|
||||||
@@ -22,29 +23,33 @@ export class Socket {
|
|||||||
constructor(url: string) {
|
constructor(url: string) {
|
||||||
this.ws = undefined
|
this.ws = undefined
|
||||||
this.url = url
|
this.url = url
|
||||||
this.ready = undefined
|
this.ready = defer()
|
||||||
this.timeout = undefined
|
this.timeout = undefined
|
||||||
this.queue = []
|
this.queue = []
|
||||||
this.bus = new EventBus()
|
this.bus = new EventBus()
|
||||||
this.status = Socket.STATUS.NEW
|
this.status = Socket.STATUS.NEW
|
||||||
|
|
||||||
this._onOpen = e => {
|
this._onOpen = () => {
|
||||||
this.status = Socket.STATUS.READY
|
this.status = Socket.STATUS.READY
|
||||||
this.ready?.resolve()
|
this.ready.resolve()
|
||||||
this.bus.emit('open')
|
this.bus.emit('open')
|
||||||
}
|
}
|
||||||
|
|
||||||
this._onMessage = e => {
|
this._onMessage = event => {
|
||||||
this.queue.push(e.data as string)
|
this.queue.push(event.data as string)
|
||||||
|
|
||||||
if (!this.timeout) {
|
if (!this.timeout) {
|
||||||
this.handleMessagesAsync()
|
this.handleMessagesAsync()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this._onClose = e => {
|
this._onError = (err: Error) => {
|
||||||
|
this.bus.emit('error', err)
|
||||||
|
}
|
||||||
|
|
||||||
|
this._onClose = () => {
|
||||||
this.disconnect()
|
this.disconnect()
|
||||||
this.ready?.reject()
|
this.ready.reject()
|
||||||
this.status = Socket.STATUS.CLOSED
|
this.status = Socket.STATUS.CLOSED
|
||||||
this.bus.emit('close')
|
this.bus.emit('close')
|
||||||
}
|
}
|
||||||
@@ -61,17 +66,25 @@ export class Socket {
|
|||||||
|
|
||||||
this.ws.addEventListener("open", this._onOpen)
|
this.ws.addEventListener("open", this._onOpen)
|
||||||
this.ws.addEventListener("message", this._onMessage)
|
this.ws.addEventListener("message", this._onMessage)
|
||||||
|
this.ws.addEventListener("error", this._onError)
|
||||||
this.ws.addEventListener("close", this._onClose)
|
this.ws.addEventListener("close", this._onClose)
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.ready?.catch(() => null)
|
await this.ready.catch(() => null)
|
||||||
}
|
}
|
||||||
disconnect() {
|
disconnect() {
|
||||||
this.ws?.close()
|
if (this.ws) {
|
||||||
this.ws?.removeEventListener("open", this._onOpen)
|
const ws = this.ws
|
||||||
this.ws?.removeEventListener("message", this._onMessage)
|
|
||||||
this.ws?.removeEventListener("close", this._onClose)
|
// Avoid "WebSocket was closed before the connection was established"
|
||||||
this.ws = undefined
|
this.ready.then(() => ws.close())
|
||||||
|
|
||||||
|
this.ws.removeEventListener("open", this._onOpen)
|
||||||
|
this.ws.removeEventListener("message", this._onMessage)
|
||||||
|
this.ws.removeEventListener("error", this._onError)
|
||||||
|
this.ws.removeEventListener("close", this._onClose)
|
||||||
|
this.ws = undefined
|
||||||
|
}
|
||||||
}
|
}
|
||||||
cleanup() {
|
cleanup() {
|
||||||
this.disconnect()
|
this.disconnect()
|
||||||
|
|||||||
Reference in New Issue
Block a user