Added message queue handling

This commit is contained in:
hydrabolt
2015-08-29 17:22:17 +01:00
parent 7b124901c6
commit b45d60eee8
4 changed files with 131 additions and 5 deletions

View File

@@ -61,7 +61,8 @@ var Client = (function () {
this.serverCache = [];
this.pmChannelCache = [];
this.readyTime = null;
this.optionsQueue = {};
this.checkingQueue = {};
this.messageQueue = {};
}
_createClass(Client, [{
@@ -607,7 +608,18 @@ var Client = (function () {
if (self.options.queue) {
//we're QUEUEING messages, so sending them sequentially based on servers.
self.addMessageQueue(destination);
if (!self.messageQueue[destination]) {
self.messageQueue[destination] = [];
}
self.messageQueue[destination].push({
content: message,
mentions: mentions,
then: [resolve, callback],
error: [reject, callback]
});
self.checkQueue(destination);
} else {
self._sendMessage(destination, message, mentions).then(mgood)["catch"](mbad);
}
@@ -1379,6 +1391,45 @@ var Client = (function () {
});
});
}
}, {
key: "checkQueue",
value: function checkQueue(channelID) {
var _this = this;
var self = this;
if (!this.checkingQueue[channelID]) {
(function () {
var doNext = function doNext() {
if (self.messageQueue[channelID].length === 0) {
done();
return;
}
var msgToSend = self.messageQueue[channelID][0];
self._sendMessage(channelID, msgToSend.content, msgToSend.mentions).then(function (msg) {
msgToSend.then[0](msg);
msgToSend.then[1](null, msg);
self.messageQueue[channelID].shift();
doNext();
})["catch"](function (err) {
msgToSend["catch"][0](err);
msgToSend["catch"][1](err);
self.messageQueue[channelID].shift();
doNext();
});
};
var done = function done() {
self.checkingQueue[channelID] = false;
return;
};
//if we aren't already checking this queue.
_this.checkingQueue[channelID] = true;
doNext();
})();
}
}
}, {
key: "uptime",
get: function get() {