Skip to content

Commit

Permalink
feat(Collector): add idle option (#3746)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceEEC authored Feb 1, 2020
1 parent fbcd363 commit ccd6043
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/structures/interfaces/Collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const EventEmitter = require('events').EventEmitter;
* Options to be applied to the collector.
* @typedef {Object} CollectorOptions
* @property {number} [time] How long to run the collector for
* @property {number} [idle] How long to stop the collector after inactivity in milliseconds
*/

/**
Expand Down Expand Up @@ -62,6 +63,13 @@ class Collector extends EventEmitter {
*/
this._timeout = null;

/**
* Timeout for cleanup due to inactivity
* @type {?Timeout}
* @private
*/
this._idletimeout = null;

/**
* Call this to handle an event as a collectable element
* Accepts any event data as parameters
Expand All @@ -70,6 +78,7 @@ class Collector extends EventEmitter {
*/
this.listener = this._handle.bind(this);
if (options.time) this._timeout = this.client.setTimeout(() => this.stop('time'), options.time);
if (options.idle) this._idletimeout = this.client.setTimeout(() => this.stop('idle'), options.idle);
}

/**
Expand All @@ -89,6 +98,11 @@ class Collector extends EventEmitter {
* @param {Collector} collector The collector
*/
this.emit('collect', collect.value, this);

if (this._idletimeout) {
this.client.clearTimeout(this._idletimeout);
this._idletimeout = this.client.setTimeout(() => this.stop('idle'), this.options.idle);
}
}

const post = this.postCheck(...args);
Expand Down Expand Up @@ -136,7 +150,14 @@ class Collector extends EventEmitter {
stop(reason = 'user') {
if (this.ended) return;

if (this._timeout) this.client.clearTimeout(this._timeout);
if (this._timeout) {
this.client.clearTimeout(this._timeout);
this._timeout = null;
}
if (this._idletimeout) {
this.client.clearTimeout(this._idletimeout);
this._idletimeout = null;
}
this.ended = true;
this.cleanup();

Expand Down
8 changes: 6 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ declare module 'discord.js' {

abstract class Collector<K, V> extends EventEmitter {
constructor(client: Client, filter: CollectorFilter, options?: CollectorOptions);
private _timeout: NodeJS.Timer;
private _timeout: NodeJS.Timer | null;
private _idletimeout: NodeJS.Timer | null;
private _handle(...args: any[]): void;

public readonly client: Client;
Expand Down Expand Up @@ -1762,7 +1763,10 @@ declare module 'discord.js' {

type CollectorHandler<K, V> = { key: K, value: V };
type CollectorFilter = (...args: any[]) => boolean;
type CollectorOptions = { time?: number };
type CollectorOptions = {
time?: number;
idle?: number;
};

type ColorResolvable = ('DEFAULT'
| 'WHITE'
Expand Down

0 comments on commit ccd6043

Please # to comment.