Added awaiting internally

This commit is contained in:
hydrabolt
2015-11-21 20:50:58 +00:00
parent 20439172c1
commit ddfed4da6c
2 changed files with 72 additions and 6 deletions

View File

@@ -50,6 +50,8 @@ var InternalClient = (function () {
this.voiceConnection = null;
this.resolver = new Resolver(this);
this.readyTime = null;
this.messageAwaits = {};
}
//def leaveVoiceChannel
@@ -67,6 +69,30 @@ var InternalClient = (function () {
});
};
//def awaitResponse
InternalClient.prototype.awaitResponse = function awaitResponse(msg) {
var _this = this;
return new Promise(function (resolve, reject) {
msg = _this.resolver.resolveMessage(msg);
if (!msg) {
reject(new Error("message undefined"));
return;
}
var awaitID = msg.channel.id + msg.id;
if (!_this.messageAwaits[awaitID]) {
_this.messageAwaits[awaitID] = [];
}
_this.messageAwaits[awaitID].push(resolve);
});
};
//def joinVoiceChannel
InternalClient.prototype.joinVoiceChannel = function joinVoiceChannel(chann) {
@@ -256,7 +282,7 @@ var InternalClient = (function () {
// def logout
InternalClient.prototype.logout = function logout() {
var _this = this;
var _this2 = this;
var self = this;
return new Promise(function (resolve, reject) {
@@ -270,9 +296,9 @@ var InternalClient = (function () {
if (err) {
reject(new Error(err));
} else {
if (_this.websocket) {
_this.websocket.close();
_this.websocket = null;
if (_this2.websocket) {
_this2.websocket.close();
_this2.websocket = null;
}
self.token = null;
self.email = null;
@@ -1247,7 +1273,16 @@ var InternalClient = (function () {
var channel = self.channels.get("id", data.channel_id) || self.private_channels.get("id", data.channel_id);
if (channel) {
var msg = channel.messages.add(new Message(data, channel, client));
client.emit("message", msg);
if (self.messageAwaits[channel.id + msg.id]) {
self.messageAwaits[channel.id + msg.id].map(function (fn) {
return fn();
});
self.messageAwaits[channel.id + msg.id] = null;
client.emit("message", msg, true); //2nd param is isAwaitedMessage
} else {
client.emit("message", msg);
}
self.ack(msg);
} else {
client.emit("warn", "message created but channel is not cached");