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

File diff suppressed because one or more lines are too long

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

View File

@@ -15,11 +15,12 @@ class DMChannel extends Channel {
setup(data) {
super.setup(data);
const recipient = new User(this.client, data.recipients[0]);
/**
* The recipient on the other end of the DM
* @type {User}
*/
this.recipient = this.client.store.add('users', new User(this.client, data.recipients[0]));
this.recipient = this.client.users.set(recipient.id, recipient);
/**
* The ID of the last sent message, if available
* @type {?String}

View File

@@ -283,7 +283,7 @@ class Guild {
if (data.presences) {
for (const presence of data.presences) {
const user = this.client.store.get('users', presence.user.id);
const user = this.client.users.get(presence.user.id);
if (user) {
user.status = presence.status;
user.game = presence.game;

View File

@@ -84,7 +84,7 @@ class Message {
*/
this.id = data.id;
for (const mention of data.mentions) {
let user = this.client.store.get('users', mention.id);
let user = this.client.users.get(mention.id);
if (user) {
this.mentions.push(user);
} else {
@@ -96,7 +96,7 @@ class Message {
patch(data) {
if (data.author) {
this.author = this.client.store.get('users', data.author.id);
this.author = this.client.users.get(data.author.id);
}
if (data.content) {
this.content = data.content;
@@ -124,7 +124,7 @@ class Message {
}
if (data.mentions) {
for (const mention of data.mentions) {
let user = this.client.store.get('users', mention.id);
let user = this.client.users.get(mention.id);
if (user) {
this.mentions.push(user);
} else {

View File

@@ -18,10 +18,6 @@ class ClientDataStore extends AbstractDataStore {
this.user = null;
this.email = null;
this.password = null;
this.register('users');
this.register('guilds');
this.register('channels');
}
get pastReady() {
@@ -29,8 +25,9 @@ class ClientDataStore extends AbstractDataStore {
}
newGuild(data) {
const already = this.get('guilds', data.id);
const guild = this.add('guilds', new Guild(this.client, data));
const already = this.client.guilds.get(data.id);
const guild = new Guild(this.client, data);
this.client.guilds.set(guild.id, guild);
if (this.pastReady && !already) {
this.client.emit(Constants.Events.GUILD_CREATE, guild);
}
@@ -39,12 +36,14 @@ class ClientDataStore extends AbstractDataStore {
}
newUser(data) {
return this.add('users', new User(this.client, data));
const user = new User(this.client, data);
this.client.users.set(user.id, user);
return user;
}
newChannel(data, $guild) {
let guild = $guild;
const already = this.get('channels', data.id);
const already = this.client.channels.get(data.id);
let channel;
if (data.type === Constants.ChannelTypes.DM) {
channel = new DMChannel(this.client, data);
@@ -66,25 +65,25 @@ class ClientDataStore extends AbstractDataStore {
this.client.emit(Constants.Events.CHANNEL_CREATE, channel);
}
return this.add('channels', channel);
return this.client.channels.set(channel.id, channel);
}
return null;
}
killGuild(guild) {
const already = this.get('guilds', guild.id);
this.remove('guilds', guild);
const already = this.client.guilds.get(guild.id);
this.client.guilds.delete(guild.id);
if (already && this.pastReady) {
this.client.emit(Constants.Events.GUILD_DELETE, guild);
}
}
killUser(user) {
this.remove('users', user);
this.users.delete(user.id);
}
killChannel(channel) {
this.remove('channels', channel);
this.client.channels.delete(channel.id);
if (channel instanceof GuildChannel) {
channel.guild.channels.delete(channel.id);
}

View File

@@ -21,7 +21,7 @@ client.on('guildUpdate', (old, guild) => {
console.log('guildupdate', old.name, guild.name);
});
client.on('channelCreate', channel => {
// console.log(channel);
console.log(channel);
});
client.on('channelDelete', channel => {
console.log('channDel', channel.name);
@@ -122,7 +122,10 @@ client.on('message', message => {
if (message.content === 'stats') {
let m = '';
m += `I am aware of ${message.guild.channels.size} channels\n`;
m += `I am aware of ${message.guild.members.size} members`;
m += `I am aware of ${message.guild.members.size} members\n`;
m += `I am aware of ${client.channels.size} channels overall\n`;
m += `I am aware of ${client.guilds.size} guilds overall\n`;
m += `I am aware of ${client.users.size} users overall\n`;
message.channel.sendMessage(m);
}