Re-work socket and reconnection/status logic

This commit is contained in:
Jon Staab
2024-05-22 17:33:20 -07:00
parent f8d74f5984
commit c5e3f6d5a4
3 changed files with 46 additions and 68 deletions
+9 -6
View File
@@ -24,7 +24,7 @@ export class Connection extends Emitter {
createSender = () => {
const worker = new Worker<SocketMessage>({
shouldDefer: (message: SocketMessage) => {
if (!this.socket.isReady()) {
if (!this.socket.isOpen()) {
return true
}
@@ -91,13 +91,16 @@ export class Connection extends Emitter {
this.emit('receive', this, message)
}
ensureConnected = ({shouldReconnect = true}) => {
if (shouldReconnect && !this.socket.isHealthy()) {
this.disconnect()
ensureConnected = async ({shouldReconnect = true}) => {
const isUnhealthy = this.socket.isClosing() || this.socket.isClosed()
const noRecentFault = this.meta.lastFault < Date.now() - 60_000
if (shouldReconnect && isUnhealthy && noRecentFault) {
await this.disconnect()
}
if (this.socket.isPending()) {
this.socket.connect()
if (this.socket.isNew()) {
await this.socket.connect()
}
}