Remove all data stores! Move to maps

This commit is contained in:
Amish Shah
2016-08-19 22:34:59 +01:00
parent 82ab92ca2a
commit 8d5d90e222
17 changed files with 81 additions and 111 deletions

View File

@@ -27,7 +27,7 @@ class ClientUser extends User {
* @returns {Promise<ClientUser>}
* @example
* // set username
* client.store.user.setUsername('discordjs')
* client.user.setUsername('discordjs')
* .then(user => console.log(`My new username is ${user.username}`))
* .catch(console.log);
*/
@@ -42,7 +42,7 @@ class ClientUser extends User {
* @returns {Promise<ClientUser>}
* @example
* // set email
* client.store.user.setEmail('bob@gmail.com')
* client.user.setEmail('bob@gmail.com')
* .then(user => console.log(`My new email is ${user.email}`))
* .catch(console.log);
*/
@@ -57,7 +57,7 @@ class ClientUser extends User {
* @returns {Promise<ClientUser>}
* @example
* // set password
* client.store.user.setPassword('password')
* client.user.setPassword('password')
* .then(user => console.log('New password set!'))
* .catch(console.log);
*/
@@ -70,7 +70,7 @@ class ClientUser extends User {
* @returns {Promise<ClientUser>}
* @example
* // set avatar
* client.store.user.setAvatar(fs.readFileSync('./avatar.png'))
* client.user.setAvatar(fs.readFileSync('./avatar.png'))
* .then(user => console.log(`New avatar set!`))
* .catch(console.log);
*/

View File

@@ -70,7 +70,7 @@ class Guild {
_addMember(guildUser, noEvent) {
if (!(guildUser.user instanceof User)) {
guildUser.user = this.client.store.newUser(guildUser.user);
guildUser.user = this.client.dataManager.newUser(guildUser.user);
}
guildUser.joined_at = guildUser.joined_at || 0;
@@ -263,7 +263,7 @@ class Guild {
if (data.channels) {
this.channels.clear();
for (const channel of data.channels) {
this.client.store.newChannel(channel, this);
this.client.dataManager.newChannel(channel, this);
}
}

View File

@@ -32,7 +32,7 @@ class Message {
* The author of the message
* @type {User}
*/
this.author = this.client.store.newUser(data.author);
this.author = this.client.dataManager.newUser(data.author);
/**
* The content of the message
* @type {String}
@@ -88,7 +88,7 @@ class Message {
if (user) {
this.mentions.push(user);
} else {
user = this.client.store.newUser(mention);
user = this.client.dataManager.newUser(mention);
this.mentions.push(user);
}
}
@@ -128,7 +128,7 @@ class Message {
if (user) {
this.mentions.push(user);
} else {
user = this.client.store.newUser(mention);
user = this.client.dataManager.newUser(mention);
this.mentions.push(user);
}
}

View File

@@ -1,40 +0,0 @@
class AbstractDataStore {
constructor() {
this.data = {};
}
register(name) {
this.data[name] = {};
}
add(location, object) {
if (this.data[location][object.id]) {
return this.data[location][object.id];
}
this.data[location][object.id] = object;
return object;
}
clear(location) {
this.data[location] = {};
}
remove(location, object) {
const id = (typeof object === 'string' || object instanceof String) ? object : object.id;
if (this.data[location][id]) {
delete this.data[location][id];
return true;
}
return false;
}
get(location, value) {
return this.data[location][value];
}
getAsArray(location) {
return Object.values(this.data[location]);
}
}
module.exports = AbstractDataStore;

View File

@@ -1,105 +0,0 @@
const AbstractDataStore = require('./AbstractDataStore');
const Constants = require('../../util/Constants');
const cloneObject = require('../../util/CloneObject');
const Guild = require('../Guild');
const User = require('../User');
const DMChannel = require('../DMChannel');
const TextChannel = require('../TextChannel');
const VoiceChannel = require('../VoiceChannel');
const GuildChannel = require('../GuildChannel');
class ClientDataStore extends AbstractDataStore {
constructor(client) {
super();
this.client = client;
this.token = null;
this.session = null;
this.user = null;
this.email = null;
this.password = null;
}
get pastReady() {
return this.client.ws.status === Constants.Status.READY;
}
newGuild(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);
}
return guild;
}
newUser(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.client.channels.get(data.id);
let channel;
if (data.type === Constants.ChannelTypes.DM) {
channel = new DMChannel(this.client, data);
} else {
guild = guild || this.get('guilds', data.guild_id);
if (guild) {
if (data.type === Constants.ChannelTypes.text) {
channel = new TextChannel(guild, data);
guild.channels.set(channel.id, channel);
} else if (data.type === Constants.ChannelTypes.voice) {
channel = new VoiceChannel(guild, data);
guild.channels.set(channel.id, channel);
}
}
}
if (channel) {
if (this.pastReady && !already) {
this.client.emit(Constants.Events.CHANNEL_CREATE, channel);
}
return this.client.channels.set(channel.id, channel);
}
return null;
}
killGuild(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.users.delete(user.id);
}
killChannel(channel) {
this.client.channels.delete(channel.id);
if (channel instanceof GuildChannel) {
channel.guild.channels.delete(channel.id);
}
}
updateGuild(currentGuild, newData) {
const oldGuild = cloneObject(currentGuild);
currentGuild.setup(newData);
if (this.pastReady) {
this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild);
}
}
updateChannel(currentChannel, newData) {
currentChannel.setup(newData);
}
}
module.exports = ClientDataStore;