Avoid Listener leaks by setting a max answer threshold
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import {EventEmitter} from 'events';
|
||||
|
||||
const ANSWER_THRESHOLD = 10;
|
||||
|
||||
/**
|
||||
* Base class of all classes
|
||||
*/
|
||||
@ -43,7 +45,10 @@ export default class Base extends EventEmitter {
|
||||
}
|
||||
|
||||
const chat = this.properties.chat_id;
|
||||
let answers = 0;
|
||||
bot.on('update', function listener(result) {
|
||||
answers += result.length;
|
||||
|
||||
const update = result.find(({message}) => {
|
||||
// if in a group, there will be a reply to this message
|
||||
if (chat < 0) {
|
||||
@ -62,6 +67,10 @@ export default class Base extends EventEmitter {
|
||||
|
||||
bot.removeListener('update', listener);
|
||||
}
|
||||
|
||||
if (answers >= ANSWER_THRESHOLD) {
|
||||
bot.removeListener('update', listener);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user