Skip to content

Commit

Permalink
Add timeout/interval methods to WebhookClient (fixes discordjs#1181),…
Browse files Browse the repository at this point in the history
… and clean up docs (discordjs#1204)

* fix up 1181

* Clean up documentation

* Update WebhookClient.js
  • Loading branch information
devsnek authored and Gawdl3y committed Feb 21, 2017
1 parent 6e93b89 commit f04b2bd
Showing 1 changed file with 77 additions and 5 deletions.
82 changes: 77 additions & 5 deletions src/client/WebhookClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const Util = require('../util/Util');
*/
class WebhookClient extends Webhook {
/**
* @param {string} id The id of the webhook.
* @param {string} token the token of the webhook.
* @param {string} id ID of the webhook
* @param {string} token Token of the webhook
* @param {ClientOptions} [options] Options for the client
* @example
* // create a new webhook and send a message
* let hook = new Discord.WebhookClient('1234', 'abcdef')
* hook.sendMessage('This will send a message').catch(console.error)
* const hook = new Discord.WebhookClient('1234', 'abcdef');
* hook.sendMessage('This will send a message').catch(console.error);
*/
constructor(id, token, options) {
super(null, id, token);
Expand All @@ -35,11 +35,83 @@ class WebhookClient extends Webhook {
this.rest = new RESTManager(this);

/**
* The Data Resolver of the Client
* The data resolver of the client
* @type {ClientDataResolver}
* @private
*/
this.resolver = new ClientDataResolver(this);

/**
* Timeouts set by {@link WebhookClient#setTimeout} that are still active
* @type {Set<Timeout>}
* @private
*/
this._timeouts = new Set();

/**
* Intervals set by {@link WebhookClient#setInterval} that are still active
* @type {Set<Timeout>}
* @private
*/
this._intervals = new Set();
}

/**
* Sets a timeout that will be automatically cancelled if the client is destroyed.
* @param {Function} fn Function to execute
* @param {number} delay Time to wait before executing (in milliseconds)
* @param {...*} args Arguments for the function
* @returns {Timeout}
*/
setTimeout(fn, delay, ...args) {
const timeout = setTimeout(() => {
fn();
this._timeouts.delete(timeout);
}, delay, ...args);
this._timeouts.add(timeout);
return timeout;
}

/**
* Clears a timeout.
* @param {Timeout} timeout Timeout to cancel
*/
clearTimeout(timeout) {
clearTimeout(timeout);
this._timeouts.delete(timeout);
}

/**
* Sets an interval that will be automatically cancelled if the client is destroyed.
* @param {Function} fn Function to execute
* @param {number} delay Time to wait before executing (in milliseconds)
* @param {...*} args Arguments for the function
* @returns {Timeout}
*/
setInterval(fn, delay, ...args) {
const interval = setInterval(fn, delay, ...args);
this._intervals.add(interval);
return interval;
}

/**
* Clears an interval.
* @param {Timeout} interval Interval to cancel
*/
clearInterval(interval) {
clearInterval(interval);
this._intervals.delete(interval);
}


/**
* Destroys the client.
*/
destroy() {
for (const t of this._timeouts) clearTimeout(t);
for (const i of this._intervals) clearInterval(i);
this._timeouts.clear();
this._intervals.clear();
}
}

Expand Down

0 comments on commit f04b2bd

Please sign in to comment.