mirror of
https://github.com/m1k1o/neko.git
synced 2025-06-17 10:12:34 +02:00
reconnecter split events and add destroy.
This commit is contained in:
parent
a38ad13fb1
commit
f9c096b272
1 changed files with 32 additions and 24 deletions
|
@ -57,7 +57,11 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
|
||||||
...config,
|
...config,
|
||||||
}
|
}
|
||||||
|
|
||||||
this._conn.on('connect', () => {
|
this._conn.on('connect', this.onConnect)
|
||||||
|
this._conn.on('disconnect', this.onDisconnect)
|
||||||
|
}
|
||||||
|
|
||||||
|
private onConnect() {
|
||||||
if (this._timeout) {
|
if (this._timeout) {
|
||||||
window.clearTimeout(this._timeout)
|
window.clearTimeout(this._timeout)
|
||||||
this._timeout = undefined
|
this._timeout = undefined
|
||||||
|
@ -71,9 +75,9 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
|
||||||
} else {
|
} else {
|
||||||
this._conn.disconnect()
|
this._conn.disconnect()
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
this._conn.on('disconnect', () => {
|
private onDisconnect() {
|
||||||
if (this._timeout) {
|
if (this._timeout) {
|
||||||
window.clearTimeout(this._timeout)
|
window.clearTimeout(this._timeout)
|
||||||
this._timeout = undefined
|
this._timeout = undefined
|
||||||
|
@ -85,7 +89,6 @@ export class Reconnecter extends EventEmitter<ReconnecterEvents> {
|
||||||
this.emit('disconnect')
|
this.emit('disconnect')
|
||||||
this.reconnect()
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue