Made it better! Much better caching

This commit is contained in:
hydrabolt
2015-08-11 16:55:32 +01:00
parent 4edaba5a1f
commit 6887e0d6bc
6 changed files with 214 additions and 31 deletions

View File

@@ -1,7 +1,16 @@
exports.Channel = function(name, serverId, type, id, isPrivate){
exports.Channel = function(name, server, type, id, isPrivate){
if(!type){ //there's no second argument
var channel = name;
name = channel.name;
server = server;
type = channel.type;
id = channel.id;
isPrivate = channel.is_private;
}
this.name = name;
this.serverId = serverId;
this.server = server;
this.type = type;
this.id = id;
this.isPrivate = isPrivate;
@@ -9,11 +18,9 @@ exports.Channel = function(name, serverId, type, id, isPrivate){
}
exports.Channel.equals = function(otherChannel){
if(otherChannel.id === this.id){
return true;
} else {
return false;
}
}

74
lib/list.js Normal file
View File

@@ -0,0 +1,74 @@
exports.List = function(discriminator) {
this.discriminator = discriminator;
this.contents = [];
}
exports.List.prototype.add = function(child){
if(child.constructor === Array){
children = child;
for(child of children){
if( this.filter( this.discriminator, child[this.discriminator] ).length === 0 )
this.contents.push(child);
}
}else{
if( this.filter( this.discriminator, child[this.discriminator] ).length === 0 )
this.contents.push(child);
}
}
exports.List.prototype.length = function(){
return this.contents.length;
}
exports.List.prototype.removeIndex = function(index){
this.contents.splice(index, 1);
}
exports.List.prototype.removeChild = function(child){
var index = this.contents.indexOf(child);
if( index === -1 ){
return false;
}
this.removeIndex(index);
}
exports.List.prototype.concatSublists = function(whereList, discriminator){
//this is meant to look at the contents, and assuming the contents are all lists, concatenate their values.
var concatList = new exports.List(discriminator);
for(item of this.contents){
var itemList = item[whereList];
concatList.add(itemList.contents);
}
return concatList;
}
exports.List.prototype.filter = function(key, value, onlyOne) {
var results = [];
for (index in this.contents) {
var child = this.contents[index];
if (child[key] == value) {
if (onlyOne) {
return child;
} else {
results.push(child);
}
}
}
if(onlyOne){
return false;
}
return results;
}

View File

@@ -1,12 +1,23 @@
var User = require("./user.js").User;
exports.Message = function(time, author, content, channel, id, mentions){
if(!content){
message = time;
channel = author;
time = message.timestamp;
author = message.author;
content = message.content;
id = message.id;
mentions = message.mentions;
}
this.time = Date.parse(time);
this.author = new User(author.username, author.id, author.discriminator, author.avatar);
this.author = new User(author);
this.content = content.replace(/<[^>]*>/g, "").replace(/\s+/g, ' ').trim();
this.channel = channel;
this.id = id;
this.mentions = [];
this.mentions = mentions || [];
for(x in mentions){
var _mention = mentions[x];
this.mentions.push( new User(_mention.username, _mention.id, _mention.discriminator, _mention.avatar) );

View File

@@ -1,4 +1,5 @@
var User = require("./user.js").User;
var List = require("./list.js").List;
exports.Server = function(region, ownerID, name, id, members){
@@ -6,10 +7,12 @@ exports.Server = function(region, ownerID, name, id, members){
this.ownerID = ownerID;
this.name = name;
this.id = id;
this.members = [];
this.members = new List("id");
this.channels = new List("id");
for(x in members){
var _member = members[x].user;
this.members.push( new User(_member.username, _member.id, _member.discriminator, _member.avatar) );
var member = members[x].user;
this.members.add( new User(member) );
}
}

View File

@@ -1,4 +1,13 @@
exports.User = function(username, id, discriminator, avatar){
if(!id){ //there's no second argument
var user = username;
username = user.username;
id = user.id;
discriminator = user.discriminator;
avatar = user.avatar;
}
this.username = username;
this.discriminator = discriminator;
this.id = id;