Simplified awaitResponse

This commit is contained in:
hydrabolt
2015-11-21 21:25:28 +00:00
parent d916e5719b
commit cf33df18cf
3 changed files with 91 additions and 27 deletions

View File

@@ -807,19 +807,54 @@ var Client = (function (_EventEmitter) {
// def awaitResponse // def awaitResponse
Client.prototype.awaitResponse = function awaitResponse(msg) { Client.prototype.awaitResponse = function awaitResponse(msg) {
var toSend = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var _this = this; 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) { return new Promise(function (resolve, reject) {
_this.internal.awaitResponse(msg).then(function (newMsg) { function error(e) {
resolve(newMsg);
callback(null, newMsg);
})["catch"](function (e) {
callback(e); callback(e);
reject(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);
}
}); });
}; };

View File

@@ -798,24 +798,56 @@ class Client extends EventEmitter {
} }
// def awaitResponse // 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) => { return new Promise((resolve, reject) => {
this.internal.awaitResponse(msg) function error(e) {
.then(newMsg => { callback(e);
resolve(newMsg); reject(e);
callback(null, newMsg); }
})
.catch(e => { if (toSend) {
callback(e); if (typeof toSend === "function") {
reject(e); // (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() { setStatusIdle() {
this.setStatus("idle"); this.setStatus("idle");
} }
@@ -835,11 +867,11 @@ class Client extends EventEmitter {
setStatusAvailable() { setStatusAvailable() {
this.setStatusOnline(); this.setStatusOnline();
} }
setStatusAway() { setStatusAway() {
this.setStatusIdle(); this.setStatusIdle();
} }
setPlayingGame(game) { setPlayingGame(game) {
this.setStatus(null, game); this.setStatus(null, game);
} }

View File

@@ -25,10 +25,7 @@ client.on("message", m => {
} }
if (m.content === "ask me a question") { if (m.content === "ask me a question") {
client.awaitResponse(m, "do you like polar bears?").then(newMsg => {
m.reply("do you like polar bears?");
client.awaitResponse(m).then(newMsg => {
newMsg.reply("I see! you said " + newMsg.content); newMsg.reply("I see! you said " + newMsg.content);
}); });