fixed how channels and users are cached

This commit is contained in:
hydrabolt
2015-08-24 17:30:20 +01:00
parent ff0187a4b1
commit 97a6ff2772
6 changed files with 197 additions and 143 deletions

View File

@@ -175,14 +175,16 @@ class Client {
for(var _server of data.guilds){
self.addServer(_server);
var server = self.addServer(_server);
for(var channel of _server.channels){
server.channels.push( self.addChannel(channel, server.id) );
}
}
self.trigger("ready");
self.debug(`cached ${self.serverCache.length} servers, ${self.channelCache.length} channels and ${self.userCache.length} users.`);
console.log(self.channelCache[0]);
setInterval(function () {
self.keepAlive.apply(self);
}, data.heartbeat_interval);

View File

@@ -5,6 +5,7 @@ class Channel {
this.name = data.name;
this.type = data.type;
this.id = data.id;
this.messages = [];
//this.isPrivate = isPrivate; //not sure about the implementation of this...
}
@@ -15,6 +16,22 @@ class Channel {
equals(object) {
return object.id === this.id;
}
addMessage(data){
if(!this.getMessage("id", data.id)){
this.messages.push(data);
}
return this.getMessage("id", data.id);
}
getMessage(key, value){
for(var message of this.messages){
if(message[key] === value){
return message;
}
}
return null;
}
}

View File

@@ -5,8 +5,8 @@ class Server {
this.ownerID = data.owner_id;
this.name = data.name;
this.id = data.id;
this.members = new Set();
this.channels = new Set();
this.members = [];
this.channels = [];
this.icon = data.icon;
this.afkTimeout = data.afk_timeout;
this.afkChannelId = data.afk_channel_id;
@@ -18,13 +18,9 @@ class Server {
// get a user from this server's member list,
// it will be identical (unless an async change occurred)
// to the client's cache.
this.members.add(client.addUser(member.user));
this.members.push(client.addUser(member.user));
}
for (var channel of data.channels) {
this.channels.add(client.addChannel(channel, this.id));
}
}
get iconURL() {