Skip to content

Commit

Permalink
Store and Model Refactor (#1618)
Browse files Browse the repository at this point in the history
* UserStore refactor

* Create ChannelStore, remove redundant methods in ClientDataManager

* Create GuildStore

* Emoji stuff

* Use a Base class where possible to reduce code duplication

* Remove unnecessary comments from ChannelStore

* Add Base._clone();

* Remove unused ClientDataManager methods

* Refactor some more stuff

* ESLint

* Move Client#fetchUser to client.users.fetch

* Remove .has checks and just see if .get is truthy

* Fix guild member chunk error

* ESLint

* Fix typo

* Fix channel storing for user bots

* Remove ClientDataManager

* GuildChannelStore

* Reduce use of Util.cloneObject

* and this one too

* update typings

* Fix MessageUpdate handling (#1507)

* Fix role updates (probably fixes #1525)

* fix for eslint

* Address some of appell's comments

* Use debug constant

* start message store crap if it's ugly tell me later k

* fix that

* message store but works:tm:

* clean up guild stuff

* clean up channel store stuff

* clean up channel event handling

* does this message stuff work? find out soon in the next episode of dIsCoRd.Js

* eslint

* emojis

* emojis and reactions

* hi my name is eslint and im A LIL SHIT

* so i forgot this huh

* user stuff

* Fix @Class

* Fix message stuff

* Fix user store docs

* Document all the bases

* fix the super things

* tidy up remove

* fix textbasedchannel

* fix that too

* fix emoji store

* make voice state stuff less ugly

* make voice states even less ugly

* make members less bad

* fix bug

* fix that too

* fix reactions

* how was this broken for so long

* role store

* remove super._patch from UserConnection

* Rename UserProfile#setup to _patch

* remove unnecessary super calls

* update docgen dep (pls fix travis thx)

* doc messagestore

* fix docs

* message store docs

* things

* DOCS PLS

* more things

* Document TextBasedChannel#messages as a MessageStore

* Rebase

* All the stores!
amishshah authored Aug 25, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 243ff48 commit b8315b7
Showing 80 changed files with 837 additions and 840 deletions.
3 changes: 3 additions & 0 deletions jsdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["node_modules/jsdoc-strip-async-await"]
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -6,8 +6,8 @@
"types": "./typings/index.d.ts",
"scripts": {
"test": "npm run lint && npm run docs:test",
"docs": "docgen --source src --custom docs/index.yml --output docs/docs.json",
"docs:test": "docgen --source src --custom docs/index.yml",
"docs": "docgen --source src --custom docs/index.yml --output docs/docs.json --jsdoc jsdoc.json",
"docs:test": "docgen --source src --custom docs/index.yml --jsdoc jsdoc.json",
"lint": "eslint src",
"lint:fix": "eslint --fix src",
"webpack": "parallel-webpack"
@@ -51,6 +51,7 @@
"@types/node": "^8.0.0",
"discord.js-docgen": "hydrabolt/discord.js-docgen",
"eslint": "^4.0.0",
"jsdoc-strip-async-await": "^0.1.0",
"parallel-webpack": "^2.0.0",
"uglifyjs-webpack-plugin": "^1.0.0-beta.1",
"webpack": "^3.0.0"
38 changes: 9 additions & 29 deletions src/client/Client.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ const Constants = require('../util/Constants');
const Permissions = require('../util/Permissions');
const Util = require('../util/Util');
const RESTManager = require('./rest/RESTManager');
const ClientDataManager = require('./ClientDataManager');
const ClientManager = require('./ClientManager');
const ClientDataResolver = require('./ClientDataResolver');
const ClientVoiceManager = require('./voice/ClientVoiceManager');
@@ -13,11 +12,13 @@ const Collection = require('../util/Collection');
const { Presence } = require('../structures/Presence');
const VoiceRegion = require('../structures/VoiceRegion');
const Webhook = require('../structures/Webhook');
const User = require('../structures/User');
const Invite = require('../structures/Invite');
const ClientApplication = require('../structures/ClientApplication');
const ShardClientUtil = require('../sharding/ShardClientUtil');
const VoiceBroadcast = require('./voice/VoiceBroadcast');
const UserStore = require('../stores/UserStore');
const ChannelStore = require('../stores/ChannelStore');
const GuildStore = require('../stores/GuildStore');
const { Error, TypeError, RangeError } = require('../errors');

/**
@@ -49,13 +50,6 @@ class Client extends EventEmitter {
*/
this.rest = new RESTManager(this);

/**
* The data manager of the client
* @type {ClientDataManager}
* @private
*/
this.dataManager = new ClientDataManager(this);

/**
* The manager of the client
* @type {ClientManager}
@@ -100,23 +94,23 @@ class Client extends EventEmitter {

/**
* All of the {@link User} objects that have been cached at any point, mapped by their IDs
* @type {Collection<Snowflake, User>}
* @type {UserStore<Snowflake, User>}
*/
this.users = new Collection();
this.users = new UserStore(this);

/**
* All of the guilds the client is currently handling, mapped by their IDs -
* as long as sharding isn't being used, this will be *every* guild the bot is a member of
* @type {Collection<Snowflake, Guild>}
* @type {GuildStore<Snowflake, Guild>}
*/
this.guilds = new Collection();
this.guilds = new GuildStore(this);

/**
* All of the {@link Channel}s that the client is currently handling, mapped by their IDs -
* as long as sharding isn't being used, this will be *every* channel in *every* guild, and all DM channels
* @type {Collection<Snowflake, Channel>}
* @type {ChannelStore<Snowflake, Channel>}
*/
this.channels = new Collection();
this.channels = new ChannelStore(this);

/**
* Presences that have been received for the client user's friends, mapped by user IDs
@@ -322,20 +316,6 @@ class Client extends EventEmitter {
});
}

/**
* Obtains a user from Discord, or the user cache if it's already available.
* <warn>This is only available when using a bot account.</warn>
* @param {Snowflake} id ID of the user
* @param {boolean} [cache=true] Whether to cache the new user object if it isn't already
* @returns {Promise<User>}
*/
fetchUser(id, cache = true) {
if (this.users.has(id)) return Promise.resolve(this.users.get(id));
return this.api.users(id).get().then(data =>
cache ? this.dataManager.newUser(data) : new User(this, data)
);
}

/**
* Obtains an invite from Discord.
* @param {InviteResolvable} invite Invite code or URL
112 changes: 0 additions & 112 deletions src/client/ClientDataManager.js

This file was deleted.

7 changes: 6 additions & 1 deletion src/client/actions/ChannelCreate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');

class ChannelCreateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.dataManager.newChannel(data);
const existing = client.channels.has(data.id);
const channel = client.channels.create(data);
if (!existing && channel) {
client.emit(Constants.Events.CHANNEL_CREATE, channel);
}
return { channel };
}
}
20 changes: 10 additions & 10 deletions src/client/actions/ChannelDelete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');

class ChannelDeleteAction extends Action {
constructor(client) {
@@ -8,22 +9,21 @@ class ChannelDeleteAction extends Action {

handle(data) {
const client = this.client;

let channel = client.channels.get(data.id);

if (channel) {
client.dataManager.killChannel(channel);
this.deleted.set(channel.id, channel);
this.scheduleForDeletion(channel.id);
} else {
channel = this.deleted.get(data.id) || null;
client.channels.remove(channel.id);
client.emit(Constants.Events.CHANNEL_DELETE, channel);
}

return { channel };
}

scheduleForDeletion(id) {
this.client.setTimeout(() => this.deleted.delete(id), this.client.options.restWsBridgeTimeout);
}
}

/**
* Emitted whenever a channel is deleted.
* @event Client#channelDelete
* @param {Channel} channel The channel that was deleted
*/

module.exports = ChannelDeleteAction;
20 changes: 3 additions & 17 deletions src/client/actions/ChannelUpdate.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Util = require('../../util/Util');

class ChannelUpdateAction extends Action {
handle(data) {
const client = this.client;

const channel = client.channels.get(data.id);
if (channel) {
const oldChannel = Util.cloneObject(channel);
channel.setup(data);
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
const old = channel._update(data);
return {
old: oldChannel,
old,
updated: channel,
};
}

return {
old: null,
updated: null,
};
return {};
}
}

/**
* Emitted whenever a channel is updated - e.g. name change, topic change.
* @event Client#channelUpdate
* @param {Channel} oldChannel The channel before the update
* @param {Channel} newChannel The channel after the update
*/

module.exports = ChannelUpdateAction;
2 changes: 1 addition & 1 deletion src/client/actions/GuildBanRemove.js
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ class GuildBanRemove extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.guild_id);
const user = client.dataManager.newUser(data.user);
const user = client.users.create(data.user);
if (guild && user) client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user);
}
}
3 changes: 2 additions & 1 deletion src/client/actions/GuildDelete.js
Original file line number Diff line number Diff line change
@@ -29,7 +29,8 @@ class GuildDeleteAction extends Action {
}

// Delete guild
client.guilds.delete(guild.id);
client.guilds.remove(guild.id);
client.emit(Constants.Events.GUILD_DELETE, guild);
this.deleted.set(guild.id, guild);
this.scheduleForDeletion(guild.id);
} else {
5 changes: 3 additions & 2 deletions src/client/actions/GuildEmojiCreate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');

class GuildEmojiCreateAction extends Action {
handle(guild, createdEmoji) {
const client = this.client;
const emoji = client.dataManager.newEmoji(createdEmoji, guild);
const emoji = guild.emojis.create(createdEmoji);
this.client.emit(Constants.Events.GUILD_EMOJI_CREATE, emoji);
return { emoji };
}
}
5 changes: 3 additions & 2 deletions src/client/actions/GuildEmojiDelete.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');

class GuildEmojiDeleteAction extends Action {
handle(emoji) {
const client = this.client;
client.dataManager.killEmoji(emoji);
emoji.guild.emojis.remove(emoji.id);
this.client.emit(Constants.Events.GUILD_EMOJI_DELETE, emoji);
return { emoji };
}
}
8 changes: 5 additions & 3 deletions src/client/actions/GuildEmojiUpdate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');

class GuildEmojiUpdateAction extends Action {
handle(oldEmoji, newEmoji) {
const emoji = this.client.dataManager.updateEmoji(oldEmoji, newEmoji);
return { emoji };
handle(current, data) {
const old = current._update(data);
this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, old, current);
return { emoji: current };
}
}

2 changes: 1 addition & 1 deletion src/client/actions/GuildMemberGet.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ const Action = require('./Action');

class GuildMemberGetAction extends Action {
handle(guild, data) {
const member = guild._addMember(data, false);
const member = guild.members.create(data);
return { member };
}
}
Loading

0 comments on commit b8315b7

Please sign in to comment.