From cf33df18cf4e70f1904256eb7a67ce0ee98b259a Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sat, 21 Nov 2015 21:25:28 +0000 Subject: [PATCH] Simplified awaitResponse --- lib/Client/Client.js | 47 +++++++++++++++++++++++++++---- src/Client/Client.js | 66 ++++++++++++++++++++++++++++++++------------ test/bot.1.js | 5 +--- 3 files changed, 91 insertions(+), 27 deletions(-) diff --git a/lib/Client/Client.js b/lib/Client/Client.js index 71a2bb11f..3d407da7f 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -807,19 +807,54 @@ var Client = (function (_EventEmitter) { // def awaitResponse Client.prototype.awaitResponse = function awaitResponse(msg) { + var toSend = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; + var _this = this; - var callback = arguments.length <= 1 || arguments[1] === undefined ? function (e, newMsg) {} : arguments[1]; + var options = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2]; + var callback = arguments.length <= 3 || arguments[3] === undefined ? function (e, newMsg) {} : arguments[3]; + + var self = this; return new Promise(function (resolve, reject) { - _this.internal.awaitResponse(msg).then(function (newMsg) { - resolve(newMsg); - callback(null, newMsg); - })["catch"](function (e) { + function error(e) { callback(e); reject(e); - }); + } + + if (toSend) { + if (typeof toSend === "function") { + // (msg, callback) + callback = toSend; + final(); + } else { + // (msg, toSend, ...) + if (options) { + if (typeof options === "function") { + //(msg, toSend, callback) + callback = options; + _this.sendMessage(msg, toSend).then(final)["catch"](error); + } else { + //(msg, toSend, options, callback) + _this.sendMessage(msg, toSend, options).then(final)["catch"](error); + } + } else { + // (msg, toSend) promise + _this.sendMessage(msg, toSend).then(final)["catch"](error); + } + } + } else { + // (msg) promise + final(); + } + + function final() { + self.internal.awaitResponse(msg).then(function (newMsg) { + resolve(newMsg); + callback(null, newMsg); + })["catch"](error); + } }); }; diff --git a/src/Client/Client.js b/src/Client/Client.js index 85b8c45fc..7a7d82399 100644 --- a/src/Client/Client.js +++ b/src/Client/Client.js @@ -798,24 +798,56 @@ class Client extends EventEmitter { } // def awaitResponse - awaitResponse(msg, callback = function (e, newMsg) { }) { + awaitResponse(msg, toSend = null, options = null, callback = function (e, newMsg) { }) { + + var self = this; return new Promise((resolve, reject) => { - - this.internal.awaitResponse(msg) - .then(newMsg => { - resolve(newMsg); - callback(null, newMsg); - }) - .catch(e => { - callback(e); - reject(e); - }); - - }) - + + function error(e) { + callback(e); + reject(e); + } + + if (toSend) { + if (typeof toSend === "function") { + // (msg, callback) + callback = toSend; + final(); + } else { + // (msg, toSend, ...) + if (options) { + if (typeof options === "function") { + //(msg, toSend, callback) + callback = options; + this.sendMessage(msg, toSend).then(final).catch(error); + } else { + //(msg, toSend, options, callback) + this.sendMessage(msg, toSend, options).then(final).catch(error); + } + } else { + // (msg, toSend) promise + this.sendMessage(msg, toSend).then(final).catch(error); + } + } + } else { + // (msg) promise + final(); + } + + function final() { + self.internal.awaitResponse(msg) + .then(newMsg => { + resolve(newMsg); + callback(null, newMsg); + }) + .catch(error); + } + + }); + } - + setStatusIdle() { this.setStatus("idle"); } @@ -835,11 +867,11 @@ class Client extends EventEmitter { setStatusAvailable() { this.setStatusOnline(); } - + setStatusAway() { this.setStatusIdle(); } - + setPlayingGame(game) { this.setStatus(null, game); } diff --git a/test/bot.1.js b/test/bot.1.js index 5b812bcab..3a9117ae3 100644 --- a/test/bot.1.js +++ b/test/bot.1.js @@ -25,10 +25,7 @@ client.on("message", m => { } if (m.content === "ask me a question") { - - m.reply("do you like polar bears?"); - - client.awaitResponse(m).then(newMsg => { + client.awaitResponse(m, "do you like polar bears?").then(newMsg => { newMsg.reply("I see! you said " + newMsg.content); });