[FIX] Close websocket (#379)

* Fixed a bug when closing websocket

* removeListener fixed
This commit is contained in:
Diego Mello 2018-07-24 19:29:07 -03:00 committed by Guilherme Gazzo
parent 8322e7e576
commit 94c7d9e8cc
1 changed files with 6 additions and 4 deletions

View File

@ -37,6 +37,9 @@ class EventEmitter {
if (idx > -1) { if (idx > -1) {
this.events[event].splice(idx, 1); this.events[event].splice(idx, 1);
} }
if (this.events[event].length === 0) {
delete this.events[event];
}
} }
} }
emit(event, ...args) { emit(event, ...args) {
@ -51,11 +54,10 @@ class EventEmitter {
} }
} }
once(event, listener) { once(event, listener) {
this.on(event, function g(...args) { return this.on(event, function g(...args) {
this.removeListener(event, g); this.removeListener(event, g);
listener.apply(this, args); listener.apply(this, args);
}); });
return listener;
} }
} }
@ -175,7 +177,7 @@ export default class Socket extends EventEmitter {
this.ddp.once(id, (data) => { this.ddp.once(id, (data) => {
// console.log(data); // console.log(data);
this.lastping = new Date(); this.lastping = new Date();
this.ddp.removeListener(id, cancel); this.ddp.removeListener('disconnected', cancel);
return (data.error ? reject(data.error) : resolve({ id, ...data })); return (data.error ? reject(data.error) : resolve({ id, ...data }));
}); });
}); });
@ -187,7 +189,7 @@ export default class Socket extends EventEmitter {
try { try {
// this.connection && this.connection.readyState > 1 && this.connection.close && this.connection.close(300, 'disconnect'); // this.connection && this.connection.readyState > 1 && this.connection.close && this.connection.close(300, 'disconnect');
if (this.connection && this.connection.close) { if (this.connection && this.connection.close) {
this.connection.close(300, 'disconnect'); this.connection.close();
delete this.connection; delete this.connection;
} }
} catch (e) { } catch (e) {