I am trying to set a timeout in a class constructor, to turn of the emitter, but when calling new Collector(options) it doesn't wait and immediately runs the .stop() function, which it should only do after 15000 ms.
My code:
Collector.js:
module.exports = class Collector extends EventEmitter {
#counter;
#ended;
#timeout;
#timeoutTime;
/**
*
* @param {{ filter?: CollectorFilter, max?: number, type: string, time?: number}} options
*/
constructor(options, client) {
super();
this.#counter = 0;
this.collected = new Collection();
this.filter = options.filter ?? (() => true);
client.collectors.set(options.type, this);
this.max = options.max ?? 1;
this.#ended = false;
this.#timeoutTime = options.time ?? 15000;
console.log(this.#timeoutTime)
this.#timeout = setTimeout(() => {
console.log('hey')
this.stop('TIME_END')
}, this.#timeout);
}
/**
*
* @param {MessageComponentInteraction | ModalInteraction} interaction
*/
collect(interaction) {
if(this.#ended) return;
const filterBoolean = this.filter(interaction)
if(filterBoolean) {
this.#counter++
this.collected.set(interaction.id, interaction)
this.emit('collect', interaction);
if(this.#counter === this.max) {
this.emit('end', this.collected, 'MAX');
this.#ended = true;
}
}
else {
this.emit('ignore', interaction)
}
}
stop(reason = 'user') {
this.#ended = true;
clearTimeout(this.#timeout);
this.emit('end', this.collected, reason)
}
}
Calling the constructor:
const collector = new Collector({ filter: (i) => i.customId === 'hey', max: 2}, client, );