Migrate most of the Client Data Store to Clien

This commit is contained in:
Amish Shah
2016-08-19 22:21:30 +01:00
parent ad8b4c7698
commit 82ab92ca2a
31 changed files with 75 additions and 55 deletions

View File

@@ -56,6 +56,22 @@ class Client extends EventEmitter {
* @private
*/
this.actions = new ActionsManager(this);
/**
* A map of the Client's stored users
* @type {Map<String, User>}
*/
this.users = new Map();
/**
* A map of the Client's stored guilds
* @type {Map<String, Guild>}
*/
this.guilds = new Map();
/**
* A map of the Client's stored channels
* @type {Map<String, Channel>}
*/
this.channels = new Map();
}
/**

View File

@@ -38,7 +38,7 @@ class ClientDataResolver {
if (user instanceof User) {
return user;
} else if ($string(user)) {
return this.client.store.get('users', user);
return this.client.users.get(user.id);
} else if (user instanceof Message) {
return user.author;
} else if (user instanceof Guild) {
@@ -133,7 +133,7 @@ class ClientDataResolver {
}
if ($string(channel)) {
return this.client.store.get('channels', channel);
return this.client.channels.get(channel.id);
}
return null;

View File

@@ -10,7 +10,7 @@ class ChannelDeleteAction extends Action {
handle(data) {
const client = this.client;
let channel = client.store.get('channels', data.id);
let channel = client.channels.get(data.id);
if (channel) {
client.store.killChannel(channel);

View File

@@ -6,7 +6,7 @@ class ChannelUpdateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.store.get('channels', data.id);
const channel = client.channels.get(data.id);
if (channel) {
const oldChannel = cloneObject(channel);

View File

@@ -11,7 +11,7 @@ class GuildDeleteAction extends Action {
handle(data) {
const client = this.client;
let guild = client.store.get('guilds', data.id);
let guild = client.guilds.get(data.id);
if (guild) {
if (guild.available && data.unavailable) {
@@ -26,7 +26,7 @@ class GuildDeleteAction extends Action {
};
}
// delete guild
client.store.remove('guilds', guild);
client.guilds.delete(guild.id);
this.deleted[guild.id] = guild;
this.scheduleForDeletion(guild.id);
} else if (this.deleted[data.id]) {

View File

@@ -11,7 +11,7 @@ class GuildMemberRemoveAction extends Action {
handle(data) {
const client = this.client;
const guild = client.store.get('guilds', data.guild_id);
const guild = client.guilds.get(data.guild_id);
if (guild) {
let member = guild.members.get(data.user.id);
if (member) {

View File

@@ -6,7 +6,7 @@ class GuildRoleCreate extends Action {
handle(data) {
const client = this.client;
const guild = client.store.get('guilds', data.guild_id);
const guild = client.guilds.get(data.guild_id);
if (guild) {
const already = guild.roles.get(data.role.id);

View File

@@ -11,7 +11,7 @@ class GuildRoleDeleteAction extends Action {
handle(data) {
const client = this.client;
const guild = client.store.get('guilds', data.guild_id);
const guild = client.guilds.get(data.guild_id);
if (guild) {
let exists = guild.roles.get(data.role_id);

View File

@@ -6,7 +6,7 @@ class GuildRoleUpdateAction extends Action {
handle(data) {
const client = this.client;
const guild = client.store.get('guilds', data.guild_id);
const guild = client.guilds.get(data.guild_id);
const roleData = data.role;

View File

@@ -12,7 +12,7 @@ class GuildUpdateAction extends Action {
handle(data) {
const client = this.client;
const guild = client.store.get('guilds', data.id);
const guild = client.guilds.get(data.id);
if (guild) {
const oldGuild = cloneObject(guild);

View File

@@ -5,7 +5,7 @@ class MessageCreateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.store.get('channels', data.channel_id);
const channel = client.channels.get(data.channel_id);
if (channel) {
const message = channel._cacheMessage(new Message(channel, data, client));

View File

@@ -10,7 +10,7 @@ class MessageDeleteAction extends Action {
handle(data) {
const client = this.client;
const channel = client.store.get('channels', data.channel_id);
const channel = client.channels.get(data.channel_id);
if (channel) {
let message = channel.messages.get(data.id);

View File

@@ -6,7 +6,7 @@ class MessageUpdateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.store.get('channels', data.channel_id);
const channel = client.channels.get(data.channel_id);
if (channel) {
const message = channel.messages.get(data.id);

View File

@@ -102,7 +102,7 @@ class RESTMethods {
}
getExistingDM(recipient) {
const dmChannel = this.rest.client.store.getAsArray('channels')
const dmChannel = Array.from(this.rest.client.channels.values())
.filter(channel => channel.recipient)
.filter(channel => channel.recipient.id === recipient.id);

View File

@@ -167,11 +167,11 @@ class WebSocketManager {
* the `READY` event.
* @returns {null}
*/
checkIfReady() {
checkIfReady(a) {
if (this.status !== Constants.Status.READY) {
let unavailableCount = 0;
for (const guildID in this.client.store.data.guilds) {
unavailableCount += this.client.store.data.guilds[guildID].available ? 0 : 1;
for (const guildID of this.client.guilds.keys()) {
unavailableCount += this.client.guilds.get(guildID).available ? 0 : 1;
}
if (unavailableCount === 0) {

View File

@@ -9,8 +9,8 @@ class GuildBanAddHandler extends AbstractHandler {
const data = packet.d;
const client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
const user = client.store.get('users', data.user.id);
const guild = client.guilds.get(data.guild_id);
const user = client.users.get(data.user.id);
if (guild && user) {
client.emit(Constants.Events.GUILD_BAN_ADD, guild, user);

View File

@@ -10,8 +10,8 @@ class GuildBanRemoveHandler extends AbstractHandler {
const data = packet.d;
const client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
const user = client.store.get('users', data.user.id);
const guild = client.guilds.get(data.guild_id);
const user = client.users.get(data.user.id);
if (guild && user) {
client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user);

View File

@@ -6,7 +6,7 @@ class GuildCreateHandler extends AbstractHandler {
const data = packet.d;
const client = this.packetManager.client;
const guild = client.store.get('guilds', data.id);
const guild = client.guilds.get(data.id);
if (guild) {
if (!guild.available && !data.unavailable) {

View File

@@ -8,7 +8,7 @@ class GuildMemberAddHandler extends AbstractHandler {
const data = packet.d;
const client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
const guild = client.guilds.get(data.guild_id);
if (guild) {
guild._addMember(data);

View File

@@ -8,7 +8,7 @@ class GuildMemberUpdateHandler extends AbstractHandler {
const data = packet.d;
const client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
const guild = client.guilds.get(data.guild_id);
if (guild) {
const member = guild.members.get(data.user.id);

View File

@@ -8,7 +8,7 @@ class GuildMembersChunkHandler extends AbstractHandler {
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
const guild = client.guilds.get(data.guild_id);
const members = [];
if (guild) {

View File

@@ -7,8 +7,8 @@ class PresenceUpdateHandler extends AbstractHandler {
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
let user = client.store.get('users', data.user.id);
const guild = client.store.get('guilds', data.guild_id);
let user = client.users.get(data.user.id);
const guild = client.guilds.get(data.guild_id);
function makeUser(newUser) {
return client.store.newUser(newUser);

View File

@@ -9,8 +9,9 @@ class ReadyHandler extends AbstractHandler {
const data = packet.d;
const client = this.packetManager.client;
client.store.user = client.store.add('users', new ClientUser(client, data.user));
const clientUser = new ClientUser(client, data.user);
client.store.user = clientUser;
client.users.set(clientUser.id, clientUser);
for (const guild of data.guilds) {
client.store.newGuild(guild);
}
@@ -21,7 +22,7 @@ class ReadyHandler extends AbstractHandler {
this.packetManager.ws.sessionID = data.session_id;
this.packetManager.ws.checkIfReady();
this.packetManager.ws.checkIfReady('abc');
}
}

View File

@@ -23,8 +23,8 @@ class TypingStartHandler extends AbstractHandler {
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
const channel = client.store.get('channels', data.channel_id);
const user = client.store.get('users', data.user_id);
const channel = client.channels.get(data.channel_id);
const user = client.users.get(data.user_id);
const timestamp = new Date(data.timestamp * 1000);
function tooLate() {

View File

@@ -8,7 +8,7 @@ class VoiceStateUpdateHandler extends AbstractHandler {
handle(packet) {
const data = packet.d;
const client = this.packetManager.client;
const guild = client.store.get('guilds', data.guild_id);
const guild = client.guilds.get(data.guild_id);
if (guild) {
const member = guild.members.get(data.user_id);