Remove GuildDataStore and move towards storing data in Maps

This commit is contained in:
Amish Shah
2016-08-19 19:58:32 +01:00
parent 75ff9fb096
commit ed818d6e7f
13 changed files with 44 additions and 88 deletions

View File

@@ -92,7 +92,7 @@ class ClientDataResolver {
return null;
}
return guild.store.get('members', user.id);
return guild.members.get(user.id);
}
/**

View File

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

View File

@@ -9,9 +9,9 @@ class GuildRoleCreate extends Action {
const guild = client.store.get('guilds', data.guild_id);
if (guild) {
const already = guild.store.get('roles', data.role.id);
const already = guild.roles.get(data.role.id);
const role = new Role(guild, data.role);
guild.store.add('roles', role);
guild.roles.set(role.id, role);
if (!already) {
client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role);

View File

@@ -14,9 +14,9 @@ class GuildRoleDeleteAction extends Action {
const guild = client.store.get('guilds', data.guild_id);
if (guild) {
let exists = guild.store.get('roles', data.role_id);
let exists = guild.roles.get(data.role_id);
if (exists) {
guild.store.remove('roles', data.role_id);
guild.roles.remove(data.role_id);
this.deleted[guild.id + data.role_id] = exists;
this.scheduleForDeletion(guild.id, data.role_id);
client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, exists);

View File

@@ -12,7 +12,7 @@ class GuildRoleUpdateAction extends Action {
if (guild) {
let oldRole;
const existingRole = guild.store.get('roles', roleData.id);
const existingRole = guild.roles.get(roleData.id);
// exists and not the same
if (existingRole && !existingRole.equals(roleData)) {
oldRole = cloneObject(existingRole);

View File

@@ -11,7 +11,7 @@ class GuildMemberUpdateHandler extends AbstractHandler {
const guild = client.store.get('guilds', data.guild_id);
if (guild) {
const member = guild.store.get('members', data.user.id);
const member = guild.members.get(data.user.id);
if (member) {
guild._updateMember(member, data);
}

View File

@@ -24,7 +24,7 @@ class PresenceUpdateHandler extends AbstractHandler {
}
if (guild) {
const memberInGuild = guild.store.get('members', user.id);
const memberInGuild = guild.members.get(user.id);
if (!memberInGuild) {
const member = guild._addMember({
user,

View File

@@ -11,7 +11,7 @@ class VoiceStateUpdateHandler extends AbstractHandler {
const guild = client.store.get('guilds', data.guild_id);
if (guild) {
const member = guild.store.get('members', data.user_id);
const member = guild.members.get(data.user_id);
if (member) {
const oldVoiceChannelMember = cloneObject(member);
if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) {

View File

@@ -1,6 +1,5 @@
const User = require('./User');
const GuildMember = require('./GuildMember');
const GuildDataStore = require('./datastore/GuildDataStore');
const Constants = require('../util/Constants');
const Role = require('./Role');
@@ -31,10 +30,22 @@ class Guild {
this.client = client;
/**
* The data store of the Guild.
* @type {GuildDataStore}
* A Map of members that are in this Guild. The key is the member's ID, the value is the member.
* @type {Map<String, GuildMember>}
*/
this.store = new GuildDataStore();
this.members = new Map();
/**
* A Map of channels that are in this Guild. The key is the channel's ID, the value is the channel.
* @type {Map<String, GuildChannel>}
*/
this.channels = new Map();
/**
* A Map of roles that are in this Guild. The key is the role's ID, the value is the role.
* @type {Map<String, Role>}
*/
this.roles = new Map();
if (!data) {
return;
@@ -63,7 +74,8 @@ class Guild {
}
guildUser.joined_at = guildUser.joined_at || 0;
const member = this.store.add('members', new GuildMember(this, guildUser));
const member = new GuildMember(this, guildUser);
this.members.set(member.id, member);
if (this.client.ws.status === Constants.Status.READY && !noEvent) {
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member);
}
@@ -81,7 +93,7 @@ class Guild {
}
_removeMember(guildMember) {
this.store.remove('members', guildMember);
this.members.delete(guildMember.id);
}
/**
@@ -236,7 +248,7 @@ class Guild {
this.features = data.features || [];
if (data.members) {
this.store.clear('members');
this.members.clear();
for (const guildUser of data.members) {
this._addMember(guildUser);
}
@@ -246,10 +258,10 @@ class Guild {
* The owner of the guild
* @type {User}
*/
this.owner = this.store.get('members', data.owner_id);
this.owner = this.members.get(data.owner_id);
if (data.channels) {
this.store.clear('channels');
this.channels.clear();
for (const channel of data.channels) {
this.client.store.newChannel(channel, this);
}
@@ -259,12 +271,13 @@ class Guild {
* The embed channel of the Guild.
* @type {GuildChannel}
*/
this.embedChannel = this.store.get('channels', data.embed_channel_id);
this.embedChannel = this.channels.get(data.embed_channel_id);
if (data.roles) {
this.store.clear('roles');
this.roles.clear();
for (const role of data.roles) {
this.store.add('roles', new Role(this, role));
const newRole = new Role(this, role);
this.roles.set(newRole.id, newRole);
}
}
@@ -280,7 +293,7 @@ class Guild {
if (data.voice_states) {
for (const voiceState of data.voice_states) {
const member = this.store.get('members', voiceState.user_id);
const member = this.members.get(voiceState.user_id);
if (member) {
member.serverMute = voiceState.mute;
member.serverDeaf = voiceState.deaf;
@@ -475,51 +488,6 @@ class Guild {
setSplash(splash) {
return this.edit({ splash });
}
/**
* The channels in the guild.
* @type {Array<GuildChannel>}
* @readonly
*/
get channels() { return this.store.getAsArray('channels'); }
/**
* A dictionary mapping the IDs of channels in this guild to the channel itself. If you want to find a channel
* in the guild by ID, use `guild.$channels[id]` rather than filtering `guild.channels` as it is much more efficient.
* @readonly
* @type {Object<String, Channel>}
*/
get $channels() { return this.store.data.channels; }
/**
* The roles in the guild.
* @type {Array<Role>}
* @readonly
*/
get roles() { return this.store.getAsArray('roles'); }
/**
* A dictionary mapping the IDs of roles in this guild to the role itself. If you want to find a role
* in the guild by ID, use `guild.$roles[id]` rather than filtering `guild.roles` as it is much more efficient.
* @readonly
* @type {Object<String, Role>}
*/
get $roles() { return this.store.data.roles; }
/**
* The members of the guild.
* @type {Array<GuildMember>}
* @readonly
*/
get members() { return this.store.getAsArray('members'); }
/**
* A dictionary mapping the IDs of members in this guild to the member object itself. If you want to find a member
* in the guild by ID, use `guild.$members[id]` rather than filtering `guild.members` as it is much more efficient.
* @readonly
* @type {Object<String, GuildMember>}
*/
get $members() { return this.store.data.members; }
}
module.exports = Guild;

View File

@@ -74,14 +74,14 @@ class GuildMember {
*/
get roles() {
const list = [];
const everyoneRole = this.guild.store.get('roles', this.guild.id);
const everyoneRole = this.guild.roles.get(this.guild.id);
if (everyoneRole) {
list.push(everyoneRole);
}
for (const roleID of this._roles) {
const role = this.guild.store.get('roles', roleID);
const role = this.guild.roles.get(roleID);
if (role) {
list.push(role);
}
@@ -114,7 +114,7 @@ class GuildMember {
* @readonly
*/
get voiceChannel() {
return this.guild.store.get('channels', this.voiceChannelID);
return this.guild.channels.get(this.voiceChannelID);
}
/**

View File

@@ -53,10 +53,10 @@ class ClientDataStore extends AbstractDataStore {
if (guild) {
if (data.type === Constants.ChannelTypes.text) {
channel = new TextChannel(guild, data);
guild.store.add('channels', channel);
guild.channels.set(channel.id, channel);
} else if (data.type === Constants.ChannelTypes.voice) {
channel = new VoiceChannel(guild, data);
guild.store.add('channels', channel);
guild.channels.set(channel.id, channel);
}
}
}
@@ -86,7 +86,7 @@ class ClientDataStore extends AbstractDataStore {
killChannel(channel) {
this.remove('channels', channel);
if (channel instanceof GuildChannel) {
channel.guild.store.remove('channels', channel);
channel.guild.channels.delete(channel.id);
}
}

View File

@@ -1,12 +0,0 @@
const AbstractDataStore = require('./AbstractDataStore');
class GuildDataStore extends AbstractDataStore {
constructor() {
super();
this.register('members');
this.register('channels');
}
}
module.exports = GuildDataStore;

View File

@@ -121,8 +121,8 @@ client.on('message', message => {
if (message.content === 'stats') {
let m = '';
m += `I am aware of ${message.guild.channels.length} channels\n`;
m += `I am aware of ${message.guild.members.length} members`;
m += `I am aware of ${message.guild.channels.size} channels\n`;
m += `I am aware of ${message.guild.members.size} members`;
message.channel.sendMessage(m);
}