ensure that close is not intercepted by any other event. (#29)

This commit is contained in:
Miroslav Šedivý 2023-04-22 14:23:06 +02:00 committed by GitHub
parent d0b318572c
commit 9f8310fe10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -23,6 +23,7 @@ export interface NekoConnectionEvents {
export class NekoConnection extends EventEmitter<NekoConnectionEvents> { export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
private _open = false private _open = false
private _closing = false
public websocket = new NekoWebSocket() public websocket = new NekoWebSocket()
public logger = new NekoLoggerFactory(this.websocket) public logger = new NekoLoggerFactory(this.websocket)
@ -204,18 +205,24 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
} }
public close(error?: Error) { public close(error?: Error) {
if (this._open) { // we want to make sure that close event is only emitted once
// and is not intercepted by any other close event
const active = this._open && !this._closing
if (active) {
// set state to disconnected // set state to disconnected
Vue.set(this._state.websocket, 'connected', false) Vue.set(this._state.websocket, 'connected', false)
Vue.set(this._state.webrtc, 'connected', false) Vue.set(this._state.webrtc, 'connected', false)
Vue.set(this._state, 'status', 'disconnected') Vue.set(this._state, 'status', 'disconnected')
this._closing = true
} }
// close all reconnectors // close all reconnectors
Object.values(this._reconnector).forEach((r) => r.close()) Object.values(this._reconnector).forEach((r) => r.close())
if (this._open) { if (active) {
this._open = false this._open = false
this._closing = false
this.emit('close', error) this.emit('close', error)
} }
} }

View File

@ -442,7 +442,7 @@
}) })
this.neko.events.on('connection.closed', (error?: Error) => { this.neko.events.on('connection.closed', (error?: Error) => {
if (error) { if (error) {
alert('Connection closed with error:' + error.message) alert('Connection closed with error: ' + error.message)
} else { } else {
alert('Connection closed without error.') alert('Connection closed without error.')
} }