Fix massive timeout/interval memory leaks

This commit is contained in:
Schuyler Cebulskie
2016-09-19 03:49:42 -04:00
parent 6f7deba4b3
commit 7d8667694d
5 changed files with 41 additions and 28 deletions

View File

@@ -103,8 +103,8 @@ class Client extends EventEmitter {
* @type {?Date}
*/
this.readyTime = null;
this._intervals = [];
this._timeouts = [];
this._intervals = [];
}
/**
@@ -138,34 +138,18 @@ class Client extends EventEmitter {
destroy() {
return new Promise((resolve, reject) => {
this.manager.destroy().then(() => {
this._intervals.map(i => clearInterval(i));
this._timeouts.map(t => clearTimeout(t));
for (const i of this._intervals) clearInterval(i);
for (const t of this._timeouts) clearTimeout(t);
this._timeouts = [];
this._intervals = [];
this.token = null;
this.email = null;
this.password = null;
this._timeouts = [];
this._intervals = [];
resolve();
}).catch(reject);
});
}
setInterval(...params) {
const interval = setInterval(...params);
this._intervals.push(interval);
return interval;
}
setTimeout(...params) {
const restParams = params.slice(1);
const timeout = setTimeout(() => {
this._timeouts.splice(this._timeouts.indexOf(params[0]), 1);
params[0]();
}, ...restParams);
this._timeouts.push(timeout);
return timeout;
}
/**
* This shouldn't really be necessary to most developers as it is automatically invoked every 30 seconds, however
* if you wish to force a sync of Guild data, you can use this. Only applicable to user accounts.
@@ -237,6 +221,31 @@ class Client extends EventEmitter {
get status() {
return this.ws.status;
}
setTimeout(fn, ...params) {
const timeout = setTimeout(() => {
fn();
this._timeouts.splice(this._timeouts.indexOf(timeout), 1);
}, ...params);
this._timeouts.push(timeout);
return timeout;
}
clearTimeout(timeout) {
clearTimeout(timeout);
this._timeouts.splice(this._timeouts.indexOf(timeout), 1);
}
setInterval(...params) {
const interval = setInterval(...params);
this._intervals.push(interval);
return interval;
}
clearInterval(interval) {
clearInterval(interval);
this._intervals.splice(this._intervals.indexOf(interval), 1);
}
}
module.exports = Client;