mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
src/client/websocket/packets/WebSocketPacketManager.js
Unify ready and reconnecting properties into a single status property and future-proof Message class The state of the WebSocketManager is now represented by a single status property, removing emittedReady and reconnecting as representations of state. Message class will now also cache users it isn't aware of that appear in mentions and authors.
This commit is contained in:
@@ -12,12 +12,12 @@ class WebSocketManager {
|
||||
this.client = client;
|
||||
this.ws = null;
|
||||
this.packetManager = new PacketManager(this);
|
||||
this.emittedReady = false;
|
||||
this.store = new WebSocketManagerDataStore();
|
||||
this.reconnecting = false;
|
||||
this.status = Constants.Status.IDLE;
|
||||
}
|
||||
|
||||
connect(gateway) {
|
||||
this.status = Constants.Status.CONNECTING;
|
||||
this.store.gateway = gateway;
|
||||
gateway += `/?v=${this.client.options.protocol_version}`;
|
||||
this.ws = new WebSocket(gateway);
|
||||
@@ -91,7 +91,7 @@ class WebSocketManager {
|
||||
}
|
||||
|
||||
checkIfReady() {
|
||||
if (!this.emittedReady) {
|
||||
if (this.status !== Constants.Status.READY) {
|
||||
let unavailableCount = 0;
|
||||
|
||||
for (let guildID in this.client.store.data.guilds) {
|
||||
@@ -99,19 +99,18 @@ class WebSocketManager {
|
||||
}
|
||||
|
||||
if (unavailableCount === 0) {
|
||||
this.status = Constants.Status.READY;
|
||||
this.client.emit(Constants.Events.READY);
|
||||
this.emittedReady = true;
|
||||
this.packetManager.handleQueue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tryReconnect() {
|
||||
this.reconnecting = true;
|
||||
this.status = Constants.Status.RECONNECTING;
|
||||
this.ws.close();
|
||||
this.packetManager.handleQueue();
|
||||
this.client.emit(Constants.Events.RECONNECTING);
|
||||
this.emittedReady = false;
|
||||
this.connect(this.store.gateway);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ class WebSocketPacketManager {
|
||||
|
||||
this.setSequence(packet.s);
|
||||
|
||||
if (!this.ws.emittedReady) {
|
||||
if (this.ws.status !== Constants.Status.READY) {
|
||||
if (BeforeReadyWhitelist.indexOf(packet.t) === -1) {
|
||||
this.queue.push(packet);
|
||||
return;
|
||||
|
||||
@@ -33,7 +33,7 @@ class Guild {
|
||||
|
||||
guildUser.joined_at = guildUser.joined_at || 0;
|
||||
let member = this.store.add('members', new GuildMember(this, guildUser));
|
||||
if (this.client.ws.emittedReady && !noEvent) {
|
||||
if (this.client.ws.status === Constants.Status.READY && !noEvent) {
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member);
|
||||
}
|
||||
|
||||
@@ -44,14 +44,14 @@ class Guild {
|
||||
let oldRoles = member.roles;
|
||||
|
||||
member._roles = data.roles;
|
||||
if (this.client.ws.emittedReady) {
|
||||
if (this.client.ws.status === Constants.Status.READY) {
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_ROLES_UPDATE, this, oldRoles, member.roles);
|
||||
}
|
||||
}
|
||||
|
||||
_removeMember(guildMember) {
|
||||
this.store.remove('members', guildMember);
|
||||
if (this.client.ws.emittedReady) {
|
||||
if (this.client.ws.status === Constants.Status.READY) {
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_REMOVE, this, guildMember);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ class Message {
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
this.author = this.guild.client.store.get('users', data.author.id);
|
||||
this.author = this.guild.client.store.NewUser(data.author);
|
||||
this.content = data.content;
|
||||
this.timestamp = new Date(data.timestamp);
|
||||
this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp) : null;
|
||||
@@ -25,6 +25,9 @@ class Message {
|
||||
let user = this.guild.client.store.get('users', mention.id);
|
||||
if (user) {
|
||||
this.mentions.push(user);
|
||||
} else {
|
||||
user = this.guild.client.store.NewUser(mention);
|
||||
this.mentions.push(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,6 +56,9 @@ class Message {
|
||||
let user = this.guild.client.store.get('users', mention.id);
|
||||
if (user) {
|
||||
this.mentions.push(user);
|
||||
} else {
|
||||
user = this.guild.client.store.NewUser(mention);
|
||||
this.mentions.push(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class ClientDataStore extends AbstractDataStore{
|
||||
}
|
||||
|
||||
get pastReady() {
|
||||
return this.client.ws.emittedReady;
|
||||
return this.client.ws.status === Constants.Status.READY;
|
||||
}
|
||||
|
||||
NewGuild(data) {
|
||||
|
||||
@@ -16,6 +16,13 @@ const DefaultOptions = exports.DefaultOptions = {
|
||||
max_message_cache: 200,
|
||||
};
|
||||
|
||||
const Status = exports.Status = {
|
||||
READY: 0,
|
||||
CONNECTING: 1,
|
||||
RECONNECTING: 2,
|
||||
IDLE: 3,
|
||||
};
|
||||
|
||||
const Package = exports.Package = require('../../package.json');
|
||||
|
||||
const Errors = exports.Errors = {
|
||||
|
||||
Reference in New Issue
Block a user