More rewrites

This commit is contained in:
hydrabolt
2015-10-31 18:03:35 +00:00
parent beab032811
commit 6064888f21
26 changed files with 737 additions and 24 deletions

View File

@@ -4,9 +4,21 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var EventEmitter = require("events");
var request = require("superagent");
var WebSocket = require("ws");
var ConnectionState = require("./ConnectionState.js");
var Constants = require("../Constants.js"),
Endpoints = Constants.Endpoints;
Endpoints = Constants.Endpoints,
PacketType = Constants.PacketType;
var Cache = require("../Util/Cache.js");
var User = require("../Structures/User.js"),
Channel = require("../Structures/Channel.js"),
TextChannel = require("../Structures/TextChannel.js"),
VoiceChannel = require("../Structures/VoiceChannel.js"),
PMChannel = require("../Structures/PMChannel.js"),
Server = require("../Structures/Server.js");
var zlib;
@@ -21,6 +33,12 @@ var InternalClient = (function () {
if (this.client.options.compress) {
zlib = require("zlib");
}
// creates 4 caches with discriminators based on ID
this.users = new Cache();
this.channels = new Cache();
this.servers = new Cache();
this.private_channels = new Cache();
}
InternalClient.prototype.login = function login(email, password) {
@@ -76,6 +94,7 @@ var InternalClient = (function () {
InternalClient.prototype.createWS = function createWS(url) {
var self = this;
var client = self.client;
if (this.websocket) return false;
@@ -103,12 +122,13 @@ var InternalClient = (function () {
this.websocket.onclose = function () {
self.websocket = null;
self.state = ConnectionState.DISCONNECTED;
self.client.emit("disconnected");
client.emit("disconnected");
};
this.websocket.onmessage = function (e) {
if (e.type === "Binary") {
if (!zlib) zlib = require("zlib");
e.data = zlib.inflateSync(e.data).toString();
}
@@ -117,12 +137,27 @@ var InternalClient = (function () {
packet = JSON.parse(e.data);
data = packet.d;
} catch (e) {
self.client.emit("error", e);
client.emit("error", e);
return;
}
self.emit("raw", packet);
client.emit("raw", packet);
switch (packet.t) {}
switch (packet.t) {
case PacketType.READY:
self.users.add(new User(data.user, client));
data.guilds.forEach(function (server) {
self.servers.add(new Server(server, client));
});
console.log(self.servers);
break;
}
};
};