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

@@ -2,7 +2,7 @@ const EventEmitter = require('events').EventEmitter;
const mergeDefault = require('../util/MergeDefault');
const Constants = require('../util/Constants');
const RESTManager = require('./rest/RESTManager');
const ClientDataStore = require('../structures/datastore/ClientDataStore');
const ClientDataManager = require('./ClientDataManager');
const ClientManager = require('./ClientManager');
const ClientDataResolver = require('./ClientDataResolver');
const WebSocketManager = require('./websocket/WebSocketManager');
@@ -27,11 +27,11 @@ class Client extends EventEmitter {
*/
this.rest = new RESTManager(this);
/**
* The data store of the Client
* @type {ClientDataStore}
* The data manager of the Client
* @type {ClientDataManager}
* @private
*/
this.store = new ClientDataStore(this);
this.dataManager = new ClientDataManager(this);
/**
* The manager of the Client
* @type {ClientManager}
@@ -72,6 +72,26 @@ class Client extends EventEmitter {
* @type {Map<String, Channel>}
*/
this.channels = new Map();
/**
* The authorization token for the logged in user/bot.
* @type {?String}
*/
this.token = null;
/**
* The ClientUser representing the logged in Client
* @type {?ClientUser}
*/
this.user = null;
/**
* The email, if there is one, for the logged in Client
* @type {?String}
*/
this.email = null;
/**
* The password, if there is one, for the logged in Client
* @type {?String}
*/
this.password = null;
}
/**
@@ -102,14 +122,6 @@ class Client extends EventEmitter {
return this.rest.methods.loginToken(email);
}
/**
* The User of the logged in Client, only available after `READY` has been fired.
* @type {ClientUser}
*/
get user() {
return this.store.user;
}
}
module.exports = Client;

View File

@@ -0,0 +1,97 @@
const Constants = require('../util/Constants');
const cloneObject = require('../util/CloneObject');
const Guild = require('../structures/Guild');
const User = require('../structures/User');
const DMChannel = require('../structures/DMChannel');
const TextChannel = require('../structures/TextChannel');
const VoiceChannel = require('../structures/VoiceChannel');
const GuildChannel = require('../structures/GuildChannel');
class ClientDataManager {
constructor(client) {
this.client = client;
}
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 = ClientDataManager;

View File

@@ -27,7 +27,7 @@ class ClientManager {
* @returns {null}
*/
connectToWebSocket(token, resolve, reject) {
this.client.store.token = token;
this.client.token = token;
this.client.rest.methods.getGateway()
.then(gateway => {
this.client.ws.connect(gateway);

View File

@@ -4,7 +4,7 @@ class ChannelCreateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.store.newChannel(data);
const channel = client.dataManager.newChannel(data);
return {
channel,

View File

@@ -13,7 +13,7 @@ class ChannelDeleteAction extends Action {
let channel = client.channels.get(data.id);
if (channel) {
client.store.killChannel(channel);
client.dataManager.killChannel(channel);
this.deleted[channel.id] = channel;
this.scheduleForDeletion(channel.id);
} else if (this.deleted[data.id]) {

View File

@@ -7,22 +7,22 @@ class UserUpdateAction extends Action {
handle(data) {
const client = this.client;
if (client.store.user) {
if (client.store.user.equals(data)) {
if (client.user) {
if (client.user.equals(data)) {
return {
old: client.store.user,
updated: client.store.user,
old: client.user,
updated: client.user,
};
}
const oldUser = cloneObject(client.store.user);
client.store.user.setup(data);
const oldUser = cloneObject(client.user);
client.user.setup(data);
client.emit(Constants.Events.USER_UPDATE, oldUser, client.store.user);
client.emit(Constants.Events.USER_UPDATE, oldUser, client.user);
return {
old: oldUser,
updated: client.store.user,
updated: client.user,
};
}

View File

@@ -16,10 +16,10 @@ class APIRequest {
}
getAuth() {
if (this.rest.client.store.token && this.rest.client.store.user && this.rest.client.store.user.bot) {
return `Bot ${this.rest.client.store.token}`;
} else if (this.rest.client.store.token) {
return this.rest.client.store.token;
if (this.rest.client.token && this.rest.client.user && this.rest.client.user.bot) {
return `Bot ${this.rest.client.token}`;
} else if (this.rest.client.token) {
return this.rest.client.token;
}
throw Constants.Errors.NO_TOKEN;
}

View File

@@ -11,8 +11,8 @@ class RESTMethods {
loginEmailPassword(email, password) {
return new Promise((resolve, reject) => {
this.rest.client.store.email = email;
this.rest.client.store.password = password;
this.rest.client.email = email;
this.rest.client.password = password;
this.rest.makeRequest('post', Constants.Endpoints.login, false, { email, password })
.then(data => {
this.rest.client.manager.connectToWebSocket(data.token, resolve, reject);
@@ -31,8 +31,8 @@ class RESTMethods {
return new Promise((resolve, reject) => {
this.rest.makeRequest('get', Constants.Endpoints.gateway, true)
.then(res => {
this.rest.client.store.gateway = `${res.url}/?encoding=json&v=${this.rest.client.options.protocol_version}`;
resolve(this.rest.client.store.gateway);
this.rest.client.ws.gateway = `${res.url}/?encoding=json&v=${this.rest.client.options.protocol_version}`;
resolve(this.rest.client.ws.gateway);
})
.catch(reject);
});
@@ -117,7 +117,7 @@ class RESTMethods {
return resolve(dmChannel);
}
return this.rest.makeRequest('post', Constants.Endpoints.userChannels(this.rest.client.store.user.id), true, {
return this.rest.makeRequest('post', Constants.Endpoints.userChannels(this.rest.client.user.id), true, {
recipient_id: recipient.id,
})
.then(data => resolve(this.rest.client.actions.ChannelCreate.handle(data).channel))
@@ -181,14 +181,14 @@ class RESTMethods {
updateCurrentUser(_data) {
return new Promise((resolve, reject) => {
const user = this.rest.client.store.user;
const user = this.rest.client.user;
const data = {};
data.username = _data.username || user.username;
data.avatar = this.rest.client.resolver.resolveBase64(_data.avatar) || user.avatar;
if (!user.bot) {
data.password = this.rest.client.store.password;
data.email = _data.email || this.rest.client.store.email;
data.password = this.rest.client.password;
data.email = _data.email || this.rest.client.email;
data.new_password = _data.newPassword;
}

View File

@@ -38,6 +38,12 @@ class WebSocketManager {
* @type {?Number}
*/
this.sequence = -1;
/**
* The gateway address for this WebSocket connection, null if not yet available.
* @type {?String}
*/
this.gateway = null;
}
/**
@@ -91,7 +97,7 @@ class WebSocketManager {
*/
_sendResume() {
const payload = {
token: this.client.store.token,
token: this.client.token,
session_id: this.sessionID,
seq: this.sequence,
};
@@ -109,7 +115,7 @@ class WebSocketManager {
_sendNewIdentify() {
this.reconnecting = false;
const payload = this.client.options.ws;
payload.token = this.client.store.token;
payload.token = this.client.token;
this.send({
op: Constants.OPCodes.IDENTIFY,
@@ -191,7 +197,7 @@ class WebSocketManager {
this.ws.close();
this.packetManager.handleQueue();
this.client.emit(Constants.Events.RECONNECTING);
this.connect(this.client.store.gateway);
this.connect(this.client.ws.gateway);
}
}

View File

@@ -16,7 +16,7 @@ class GuildCreateHandler extends AbstractHandler {
}
} else {
// a new guild
client.store.newGuild(data);
client.dataManager.newGuild(data);
}
}

View File

@@ -11,7 +11,7 @@ class PresenceUpdateHandler extends AbstractHandler {
const guild = client.guilds.get(data.guild_id);
function makeUser(newUser) {
return client.store.newUser(newUser);
return client.dataManager.newUser(newUser);
}
// step 1

View File

@@ -10,14 +10,14 @@ class ReadyHandler extends AbstractHandler {
const client = this.packetManager.client;
const clientUser = new ClientUser(client, data.user);
client.store.user = clientUser;
client.user = clientUser;
client.users.set(clientUser.id, clientUser);
for (const guild of data.guilds) {
client.store.newGuild(guild);
client.dataManager.newGuild(guild);
}
for (const privateDM of data.private_channels) {
client.store.newChannel(privateDM);
client.dataManager.newChannel(privateDM);
}
this.packetManager.ws.sessionID = data.session_id;