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:
hydrabolt
2016-04-20 17:45:20 +01:00
parent acc9c9bf12
commit b8283a8f29
6 changed files with 24 additions and 12 deletions

View File

@@ -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);
}
}

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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);
}
}
}

View File

@@ -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) {

View File

@@ -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 = {