Allow reusing sockets by moving some logic to onClose
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "paravel",
|
"name": "paravel",
|
||||||
"version": "0.2.2",
|
"version": "0.2.3",
|
||||||
"description": "Yet another toolkit for nostr",
|
"description": "Yet another toolkit for nostr",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
+15
-14
@@ -48,12 +48,24 @@ export class Socket extends EventEmitter {
|
|||||||
this.emit('fault', this)
|
this.emit('fault', this)
|
||||||
}
|
}
|
||||||
onClose = () => {
|
onClose = () => {
|
||||||
|
if (this.ws) {
|
||||||
|
const ws = this.ws
|
||||||
|
|
||||||
|
// Avoid "WebSocket was closed before the connection was established"
|
||||||
|
this.ready.then(() => ws.close(), () => null)
|
||||||
|
this.ws.removeEventListener("open", this.onOpen)
|
||||||
|
this.ws.removeEventListener("close", this.onClose)
|
||||||
|
this.ws.removeEventListener("error", this.onError)
|
||||||
|
// @ts-ignore
|
||||||
|
this.ws.removeEventListener("message", this.onMessage)
|
||||||
|
this.ws = undefined
|
||||||
|
}
|
||||||
|
|
||||||
if (this.status !== Socket.STATUS.ERROR) {
|
if (this.status !== Socket.STATUS.ERROR) {
|
||||||
this.disconnect()
|
|
||||||
this.ready.reject()
|
|
||||||
this.status = Socket.STATUS.CLOSED
|
this.status = Socket.STATUS.CLOSED
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.ready.reject()
|
||||||
this.emit('close', this)
|
this.emit('close', this)
|
||||||
}
|
}
|
||||||
connect = () => {
|
connect = () => {
|
||||||
@@ -71,18 +83,7 @@ export class Socket extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
disconnect = () => {
|
disconnect = () => {
|
||||||
if (this.ws) {
|
this.onClose()
|
||||||
const ws = this.ws
|
|
||||||
|
|
||||||
// Avoid "WebSocket was closed before the connection was established"
|
|
||||||
this.ready.then(() => ws.close(), () => null)
|
|
||||||
this.ws.removeEventListener("open", this.onOpen)
|
|
||||||
this.ws.removeEventListener("close", this.onClose)
|
|
||||||
this.ws.removeEventListener("error", this.onError)
|
|
||||||
// @ts-ignore
|
|
||||||
this.ws.removeEventListener("message", this.onMessage)
|
|
||||||
this.ws = undefined
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
receiveMessage = (json: string) => {
|
receiveMessage = (json: string) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user