added interval fixes - bots die gracefully now

This commit is contained in:
Amish Shah
2015-12-06 19:02:10 +00:00
parent 4486b623f5
commit e072c58460
5 changed files with 109 additions and 65 deletions

View File

@@ -75,14 +75,27 @@ export default class InternalClient {
this.channels = new Cache();
this.servers = new Cache();
this.private_channels = new Cache();
this.typingIntervals = [];
this.intervals = {
typing : [],
kai : null,
misc : []
};
this.voiceConnection = null;
this.resolver = new Resolver(this);
this.readyTime = null;
this.messageAwaits = {};
}
cleanIntervals(){
for(let interval of this.intervals.typing.concat(this.intervals.misc).concat(this.intervals.kai)){
if(interval){
clearInterval(interval);
}
}
}
get uptime() {
return (this.readyTime ? Date.now() - this.readyTime : null);
}
@@ -830,12 +843,12 @@ export default class InternalClient {
return this.resolver.resolveChannel(channel)
.then(channel => {
if(this.typingIntervals[channel.id]){
if(this.intervals.typing[channel.id]){
// typing interval already exists, leave it alone
throw new Error("Already typing in that channel");
}
this.typingIntervals[channel.id] = setInterval(
this.intervals.typing[channel.id] = setInterval(
() => this.sendTyping(channel)
.catch(error => this.emit("error", error)),
4000
@@ -851,13 +864,13 @@ export default class InternalClient {
return this.resolver.resolveChannel(channel)
.then(channel => {
if(!this.typingIntervals[channel.id]){
if(!this.intervals.typing[channel.id]){
// typing interval doesn't exist
throw new Error("Not typing in that channel");
}
clearInterval(this.typingIntervals[channel.id]);
this.typingIntervals[channel.id] = false;
clearInterval(this.intervals.typing[channel.id]);
this.intervals.typing[channel.id] = false;
});
}
@@ -1016,6 +1029,7 @@ export default class InternalClient {
self.websocket = null;
self.state = ConnectionState.DISCONNECTED;
client.emit("disconnected");
self.cleanIntervals();
};
this.websocket.onerror = e => {
@@ -1051,7 +1065,7 @@ export default class InternalClient {
});
self.state = ConnectionState.READY;
setInterval(() => self.sendWS({ op: 1, d: Date.now() }), data.heartbeat_interval);
self.intervals.kai = setInterval(() => self.sendWS({ op: 1, d: Date.now() }), data.heartbeat_interval);
client.emit("ready");
client.emit("debug", `ready packet took ${Date.now() - startTime}ms to process`);