feature: add event listeners to get answers from Base.send method only when explicitly asked #47

Merged
laurynas-karvelis merged 3 commits from feature/prevent-event-listener-overflow into master 2018-12-24 12:00:50 +00:00
2 changed files with 11 additions and 2 deletions
Showing only changes of commit 896ef4e84c - Show all commits

View File

@ -25,9 +25,10 @@ export default class Base extends EventEmitter {
* gets the Update object containing message
*
* @param {object} bot
* @param {boolean} expectAnswer whether a sent message expects an answer from a contact(s)
* @return {Promise} returns a promise, resolved with message:answer
*/
send(bot) {
send(bot, expectAnswer = false) {
if (this._keyboard) {
const replyMarkup = JSON.stringify(this._keyboard.getProperties());
this.properties.reply_markup = replyMarkup;
@ -40,6 +41,11 @@ export default class Base extends EventEmitter {
return response.result.message_id;
})
.then(messageId => {
if (!expectAnswer) {
// no need to add more event callbacks for the messages that don't need expect an answer
return resolve();
}
const chat = this.properties.chat_id;
let answers = 0;
@ -67,6 +73,8 @@ export default class Base extends EventEmitter {
bot.removeListener('update', listener);
}
});
return void 0;
})
.catch(reject)
.finally(() => {

View File

@ -44,8 +44,9 @@ export default class Question extends Message {
*/
send(bot) {
const answers = this._answers;
const expectAnswer = true;
return super.send(bot).then(message => {
return super.send(bot, expectAnswer).then(message => {
let answer;
answers.forEach(function find(a) {