Add client.destroy();

This commit is contained in:
Amish Shah
2016-08-30 13:05:50 +01:00
parent 56352220af
commit d249aa10cc
18 changed files with 74 additions and 42 deletions

View File

@@ -105,6 +105,8 @@ class Client extends EventEmitter {
* @type {?Number}
*/
this.readyTime = null;
this._intervals = [];
this._timeouts = [];
}
/**
@@ -135,6 +137,32 @@ class Client extends EventEmitter {
return this.rest.methods.loginToken(email);
}
/**
* Destroys the client and logs out. Resolves with null if successful.
* @returns {Promise<null, Error>}
*/
destroy() {
return new Promise((resolve, reject) => {
this.manager.destroy().then(() => {
this._intervals.map(i => clearInterval(i));
this._timeouts.map(t => clearTimeout(t));
this.token = null;
this.email = null;
this.password = null;
resolve();
})
.catch(reject);
});
}
setInterval(...params) {
this._intervals.push(setInterval(...params));
}
setTimeout(...params) {
this._timeouts.push(setTimeout(...params));
}
/**
* Caches a user, or obtains it from the cache if it's already cached.
* If the user isn't already cached, it will only be obtainable by OAuth bot accounts.

View File

@@ -35,7 +35,7 @@ class ClientManager {
})
.catch(reject);
setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 300);
this.client.setTimeout(() => reject(Constants.Errors.TOOK_TOO_LONG), 1000 * 300);
}
/**
@@ -44,13 +44,24 @@ class ClientManager {
* @returns {null}
*/
setupKeepAlive(time) {
this.heartbeatInterval = setInterval(() => {
this.heartbeatInterval = this.client.setInterval(() => {
this.client.ws.send({
op: Constants.OPCodes.HEARTBEAT,
d: Date.now(),
});
}, time);
}
destroy() {
return new Promise((resolve) => {
if (!this.client.user.bot) {
this.client.rest.methods.logout().then(resolve);
} else {
this.client.ws.destroy();
resolve();
}
});
}
}
module.exports = ClientManager;

View File

@@ -26,10 +26,7 @@ class ChannelDeleteAction extends Action {
}
scheduleForDeletion(id) {
this.timeouts.push(
setTimeout(() => delete this.deleted[id],
this.client.options.rest_ws_bridge_timeout)
);
this.client.setTimeout(() => delete this.deleted[id], this.client.options.rest_ws_bridge_timeout);
}
}

View File

@@ -6,7 +6,6 @@ class GuildDeleteAction extends Action {
constructor(client) {
super(client);
this.deleted = {};
this.timeouts = [];
}
handle(data) {
@@ -39,10 +38,7 @@ class GuildDeleteAction extends Action {
}
scheduleForDeletion(id) {
this.timeouts.push(
setTimeout(() => delete this.deleted[id],
this.client.options.rest_ws_bridge_timeout)
);
this.client.setTimeout(() => delete this.deleted[id], this.client.options.rest_ws_bridge_timeout)
}
}

View File

@@ -5,7 +5,6 @@ class GuildMemberRemoveAction extends Action {
constructor(client) {
super(client);
this.timeouts = [];
this.deleted = {};
}
@@ -39,10 +38,7 @@ class GuildMemberRemoveAction extends Action {
}
scheduleForDeletion(guildID, userID) {
this.timeouts.push(
setTimeout(() => delete this.deleted[guildID + userID],
this.client.options.rest_ws_bridge_timeout)
);
this.client.setTimeout(() => delete this.deleted[guildID + userID], this.client.options.rest_ws_bridge_timeout);
}
}

View File

@@ -5,7 +5,6 @@ class GuildRoleDeleteAction extends Action {
constructor(client) {
super(client);
this.timeouts = [];
this.deleted = {};
}
@@ -37,10 +36,7 @@ class GuildRoleDeleteAction extends Action {
}
scheduleForDeletion(guildID, roleID) {
this.timeouts.push(
setTimeout(() => delete this.deleted[guildID + roleID],
this.client.options.rest_ws_bridge_timeout)
);
this.client.setTimeout(() => delete this.deleted[guildID + roleID], this.client.options.rest_ws_bridge_timeout)
}
}

View File

@@ -4,7 +4,6 @@ class MessageDeleteAction extends Action {
constructor(client) {
super(client);
this.timeouts = [];
this.deleted = {};
}
@@ -33,10 +32,8 @@ class MessageDeleteAction extends Action {
}
scheduleForDeletion(channelID, messageID) {
this.timeouts.push(
setTimeout(() => delete this.deleted[channelID + messageID],
this.client.options.rest_ws_bridge_timeout)
);
this.client.setTimeout(
() => delete this.deleted[channelID + messageID], this.client.options.rest_ws_bridge_timeout);
}
}

View File

@@ -30,6 +30,10 @@ class RESTMethods {
});
}
logout() {
return this.rest.makeRequest('post', Constants.Endpoints.logout, true);
}
getGateway() {
return new Promise((resolve, reject) => {
this.rest.makeRequest('get', Constants.Endpoints.gateway, true)

View File

@@ -53,7 +53,7 @@ class SequentialRequestHandler extends RequestHandler {
}
if (err) {
if (err.status === 429) {
setTimeout(() => {
this.restManager.client.setTimeout(() => {
this.waiting = false;
this.globalLimit = false;
resolve();
@@ -73,7 +73,7 @@ class SequentialRequestHandler extends RequestHandler {
const data = res && res.body ? res.body : {};
item.resolve(data);
if (this.requestRemaining === 0) {
setTimeout(() => {
this.restManager.client.setTimeout(() => {
this.waiting = false;
resolve(data);
}, (this.requestResetTime - Date.now()) + this.timeDifference + 1000);

View File

@@ -66,7 +66,7 @@ class VoiceConnectionWebSocket extends EventEmitter {
}
_setHeartbeat(interval) {
this.heartbeat = setInterval(() => {
this.heartbeat = this.voiceConnection.manager.client.setInterval(() => {
this.send({
op: Constants.VoiceOPCodes.HEARTBEAT,
d: null,

View File

@@ -70,7 +70,7 @@ class StreamDispatcher extends EventEmitter {
}
if (this.paused) {
data.timestamp = (data.timestamp + 4294967295) ? data.timestamp + 960 : 0;
return setTimeout(() => this._send(), data.length * 10);
return this.player.connection.manager.client.setTimeout(() => this._send(), data.length * 10);
}
const bufferLength = 1920 * data.channels;
this._setSpeaking(true);
@@ -78,7 +78,7 @@ class StreamDispatcher extends EventEmitter {
if (!buffer) {
data.missed++;
return setTimeout(() => this._send(), data.length * 10);
return this.player.connection.manager.client.setTimeout(() => this._send(), data.length * 10);
}
data.missed = 0;
@@ -97,7 +97,7 @@ class StreamDispatcher extends EventEmitter {
const nextTime = data.startTime + (data.count * data.length);
setTimeout(() => this._send(), data.length + (nextTime - Date.now()));
this.player.connection.manager.client.setTimeout(() => this._send(), data.length + (nextTime - Date.now()));
} catch (e) {
this._triggerTerminalState('error', e);
}

View File

@@ -76,11 +76,17 @@ class WebSocketManager {
this.doQueue();
}
destroy() {
this.ws.close(1000);
this._queue = [];
this.status = Constants.Status.IDLE;
}
doQueue() {
const item = this._queue[0];
if (this.ws.readyState === WebSocket.OPEN && item) {
if (this._remaining === 0) {
return setTimeout(() => {
return this.client.setTimeout(() => {
this.doQueue();
}, 1000);
}
@@ -88,7 +94,7 @@ class WebSocketManager {
this.ws.send(item);
this._queue.shift();
this.doQueue();
setTimeout(() => this._remaining++, 1000);
this.client.setTimeout(() => this._remaining++, 1000);
}
}
@@ -144,11 +150,10 @@ class WebSocketManager {
* @returns {null}
*/
eventClose(event) {
console.log(event.code);
if (event.code === 4004) {
throw Constants.Errors.BAD_LOGIN;
}
if (!this.reconnecting) {
if (!this.reconnecting && event.code !== 1000) {
this.tryReconnect();
}
}

View File

@@ -28,7 +28,7 @@ class TypingStartHandler extends AbstractHandler {
const timestamp = new Date(data.timestamp * 1000);
function tooLate() {
return setTimeout(() => {
return client.setTimeout(() => {
client.emit(Constants.Events.TYPING_STOP, channel, user, channel.typingMap[user.id]);
delete channel.typingMap[user.id];
}, 6000);