mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
beginning of efficient caching
This commit is contained in:
91
index.js
91
index.js
@@ -58,40 +58,81 @@ exports.Client.prototype.off = function( name ) {
|
|||||||
this.events[ name ] = function() {};
|
this.events[ name ] = function() {};
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.Client.prototype.cacheServer = function( id, cb, members ) {
|
exports.Client.prototype.cacheServer = function( id, cb, members, channelInfo ) {
|
||||||
|
|
||||||
if ( this.serverList.filter( "id", id ).length > 0 ) {
|
console.log("caching!");
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var serverInput;
|
||||||
|
|
||||||
request
|
if ( typeof id === 'string' || id instanceof String ) {
|
||||||
.get( Endpoints.SERVERS + "/" + id )
|
//actually an ID
|
||||||
.set( "authorization", this.token )
|
|
||||||
.end( function( err, res ) {
|
|
||||||
var dat = res.body;
|
|
||||||
var server = new Server( dat.region, dat.owner_id, dat.name, id, members || dat.members, dat.icon, dat.afk_timeout, dat.afk_channel_id );
|
|
||||||
|
|
||||||
request
|
if ( this.serverList.filter( "id", id ).length > 0 ) {
|
||||||
.get( Endpoints.SERVERS + "/" + id + "/channels" )
|
return;
|
||||||
.set( "authorization", self.token )
|
}
|
||||||
.end( function( err, res ) {
|
|
||||||
|
|
||||||
if(err){
|
console.log("test");
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
|
|
||||||
var channelList = res.body;
|
request
|
||||||
for ( channel of channelList ) {
|
.get( Endpoints.SERVERS + "/" + id )
|
||||||
server.channels.add( new Channel( channel, server ) );
|
.set( "authorization", self.token )
|
||||||
}
|
.end( function( err, res ) {
|
||||||
|
|
||||||
self.serverList.add( server );
|
if ( err ) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
cb( server );
|
var dat = res.body;
|
||||||
} );
|
|
||||||
} );
|
makeServer( dat );
|
||||||
|
|
||||||
|
} );
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// got objects because SPEEEDDDD
|
||||||
|
|
||||||
|
if ( this.serverList.filter( "id", id.id ).length > 0 ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
serverInput = id;
|
||||||
|
id = id.id;
|
||||||
|
makeServer( serverInput );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function channelsFromHTTP() {
|
||||||
|
request
|
||||||
|
.get( Endpoints.SERVERS + "/" + id + "/channels" )
|
||||||
|
.set( "authorization", self.token )
|
||||||
|
.end( function( err, res ) {
|
||||||
|
if ( err )
|
||||||
|
throw err;
|
||||||
|
|
||||||
|
cacheChannels( res.body );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
var server;
|
||||||
|
|
||||||
|
function makeServer( dat ) {
|
||||||
|
server = new Server( dat.region, dat.owner_id, dat.name, id, members || dat.members, dat.icon, dat.afk_timeout, dat.afk_channel_id );
|
||||||
|
console.log(server.id);
|
||||||
|
if ( !channelInfo )
|
||||||
|
channelsFromHTTP();
|
||||||
|
}
|
||||||
|
|
||||||
|
function cacheChannels( dat ) {
|
||||||
|
|
||||||
|
var channelList = dat;
|
||||||
|
for ( channel of channelList ) {
|
||||||
|
server.channels.add( new Channel( channel, server ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
self.serverList.add( server );
|
||||||
|
|
||||||
|
cb( server );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user