Store objects in a WeakMap
So comparisons like `DiscordApi.currentUser === DiscordApi.currentUser` can be true
This commit is contained in:
parent
f1e0350433
commit
33c0732c08
|
@ -5,9 +5,14 @@ import { Guild } from './guild';
|
|||
import { Message } from './message';
|
||||
import { User, GuildMember } from './user';
|
||||
|
||||
const channels = new WeakMap();
|
||||
|
||||
export class Channel {
|
||||
|
||||
constructor(data) {
|
||||
if (channels.has(data)) return channels.get(data);
|
||||
channels.set(data, this);
|
||||
|
||||
this.discordObject = data;
|
||||
}
|
||||
|
||||
|
|
|
@ -4,9 +4,14 @@ import { List } from 'structs';
|
|||
import { Channel } from './channel';
|
||||
import { GuildMember } from './user';
|
||||
|
||||
const guilds = new WeakMap();
|
||||
|
||||
export class Guild {
|
||||
|
||||
constructor(data) {
|
||||
if (guilds.has(data)) return guilds.get(data);
|
||||
guilds.set(data, this);
|
||||
|
||||
this.discordObject = data;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,9 +3,14 @@ import { DiscordApi, DiscordApiModules as Modules } from 'modules';
|
|||
import { Channel } from './channel';
|
||||
import { User } from './user';
|
||||
|
||||
const messages = new WeakMap();
|
||||
|
||||
export class Message {
|
||||
|
||||
constructor(data) {
|
||||
if (messages.has(data)) return messages.get(data);
|
||||
messages.set(data, this);
|
||||
|
||||
this.discordObject = data;
|
||||
}
|
||||
|
||||
|
@ -61,7 +66,7 @@ export class Message {
|
|||
* @param {Boolean} parse Whether to parse the message or update it as it is
|
||||
*/
|
||||
edit(content, parse = false) {
|
||||
if (this.author.id !== DiscordApi.currentUser.id)
|
||||
if (this.author !== DiscordApi.currentUser)
|
||||
throw new Error('Cannot edit messages sent by other users.');
|
||||
if (parse) Modules.MessageActions.editMessage(this.channel_id, this.id, Modules.MessageParser.parse(this.discordObject, content));
|
||||
else Modules.MessageActions.editMessage(this.channel_id, this.id, {content});
|
||||
|
@ -71,7 +76,7 @@ export class Message {
|
|||
* Start the edit mode of the UI.
|
||||
*/
|
||||
startEdit() {
|
||||
if (this.author.id !== DiscordApi.currentUser.id)
|
||||
if (this.author !== DiscordApi.currentUser)
|
||||
throw new Error('Cannot edit messages sent by other users.');
|
||||
Modules.MessageActions.startEditMessage(this.channel_id, this.id, this.content);
|
||||
}
|
||||
|
|
|
@ -4,9 +4,15 @@ import { List, InsufficientPermissions } from 'structs';
|
|||
import { Guild } from './guild';
|
||||
import { PrivateChannel } from './channel';
|
||||
|
||||
const users = new WeakMap();
|
||||
|
||||
export class User {
|
||||
|
||||
constructor(data) {
|
||||
constructor(data, _wm) {
|
||||
if (!_wm) _wm = users;
|
||||
if (_wm.has(data)) return _wm.get(data);
|
||||
_wm.set(data, this);
|
||||
|
||||
this.discordObject = data;
|
||||
}
|
||||
|
||||
|
@ -74,11 +80,13 @@ export class User {
|
|||
|
||||
}
|
||||
|
||||
const guild_members = new WeakMap();
|
||||
|
||||
// TODO: don't extend User
|
||||
export class GuildMember extends User {
|
||||
constructor(data, guild_id) {
|
||||
const user = Modules.UserStore.getUser(data.userId);
|
||||
super(user);
|
||||
super(user, guild_members);
|
||||
|
||||
this.member_data = data;
|
||||
this.guild_id = guild_id;
|
||||
|
|
Loading…
Reference in New Issue