reconnecter split events and add destroy.

This commit is contained in:
Miroslav Šedivý 2021-07-15 22:36:52 +02:00
parent a38ad13fb1
commit f9c096b272

View File

@ -57,35 +57,38 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
...config, ...config,
} }
this._conn.on('connect', () => { this._conn.on('connect', this.onConnect)
if (this._timeout) { this._conn.on('disconnect', this.onDisconnect)
window.clearTimeout(this._timeout) }
this._timeout = undefined
}
this._connected = true private onConnect() {
if (this._timeout) {
window.clearTimeout(this._timeout)
this._timeout = undefined
}
if (this._open) { this._connected = true
this._last_connected = new Date()
this.emit('connect')
} else {
this._conn.disconnect()
}
})
this._conn.on('disconnect', () => { if (this._open) {
if (this._timeout) { this._last_connected = new Date()
window.clearTimeout(this._timeout) this.emit('connect')
this._timeout = undefined } else {
} this._conn.disconnect()
}
}
this._connected = false private onDisconnect() {
if (this._timeout) {
window.clearTimeout(this._timeout)
this._timeout = undefined
}
if (this._open) { this._connected = false
this.emit('disconnect')
this.reconnect() if (this._open) {
} this.emit('disconnect')
}) this.reconnect()
}
} }
public get isOpen(): boolean { public get isOpen(): boolean {
@ -158,4 +161,9 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
this.close(new Error('reconnection failed')) this.close(new Error('reconnection failed'))
} }
} }
public destroy() {
this._conn.off('connect', this.onConnect)
this._conn.off('disconnect', this.onDisconnect)
}
} }