BaseClient.js (3680B)
1 'use strict'; 2 3 require('setimmediate'); 4 const EventEmitter = require('events'); 5 const RESTManager = require('../rest/RESTManager'); 6 const { DefaultOptions } = require('../util/Constants'); 7 const Util = require('../util/Util'); 8 9 /** 10 * The base class for all clients. 11 * @extends {EventEmitter} 12 */ 13 class BaseClient extends EventEmitter { 14 constructor(options = {}) { 15 super(); 16 17 /** 18 * Timeouts set by {@link BaseClient#setTimeout} that are still active 19 * @type {Set<Timeout>} 20 * @private 21 */ 22 this._timeouts = new Set(); 23 24 /** 25 * Intervals set by {@link BaseClient#setInterval} that are still active 26 * @type {Set<Timeout>} 27 * @private 28 */ 29 this._intervals = new Set(); 30 31 /** 32 * Intervals set by {@link BaseClient#setImmediate} that are still active 33 * @type {Set<Immediate>} 34 * @private 35 */ 36 this._immediates = new Set(); 37 38 /** 39 * The options the client was instantiated with 40 * @type {ClientOptions} 41 */ 42 this.options = Util.mergeDefault(DefaultOptions, options); 43 44 /** 45 * The REST manager of the client 46 * @type {RESTManager} 47 * @private 48 */ 49 this.rest = new RESTManager(this, options._tokenType); 50 } 51 52 /** 53 * API shortcut 54 * @type {Object} 55 * @readonly 56 * @private 57 */ 58 get api() { 59 return this.rest.api; 60 } 61 62 /** 63 * Destroys all assets used by the base client. 64 */ 65 destroy() { 66 for (const t of this._timeouts) this.clearTimeout(t); 67 for (const i of this._intervals) this.clearInterval(i); 68 for (const i of this._immediates) this.clearImmediate(i); 69 this._timeouts.clear(); 70 this._intervals.clear(); 71 this._immediates.clear(); 72 } 73 74 /** 75 * Sets a timeout that will be automatically cancelled if the client is destroyed. 76 * @param {Function} fn Function to execute 77 * @param {number} delay Time to wait before executing (in milliseconds) 78 * @param {...*} args Arguments for the function 79 * @returns {Timeout} 80 */ 81 setTimeout(fn, delay, ...args) { 82 const timeout = setTimeout(() => { 83 fn(...args); 84 this._timeouts.delete(timeout); 85 }, delay); 86 this._timeouts.add(timeout); 87 return timeout; 88 } 89 90 /** 91 * Clears a timeout. 92 * @param {Timeout} timeout Timeout to cancel 93 */ 94 clearTimeout(timeout) { 95 clearTimeout(timeout); 96 this._timeouts.delete(timeout); 97 } 98 99 /** 100 * Sets an interval that will be automatically cancelled if the client is destroyed. 101 * @param {Function} fn Function to execute 102 * @param {number} delay Time to wait between executions (in milliseconds) 103 * @param {...*} args Arguments for the function 104 * @returns {Timeout} 105 */ 106 setInterval(fn, delay, ...args) { 107 const interval = setInterval(fn, delay, ...args); 108 this._intervals.add(interval); 109 return interval; 110 } 111 112 /** 113 * Clears an interval. 114 * @param {Timeout} interval Interval to cancel 115 */ 116 clearInterval(interval) { 117 clearInterval(interval); 118 this._intervals.delete(interval); 119 } 120 121 /** 122 * Sets an immediate that will be automatically cancelled if the client is destroyed. 123 * @param {Function} fn Function to execute 124 * @param {...*} args Arguments for the function 125 * @returns {Immediate} 126 */ 127 setImmediate(fn, ...args) { 128 const immediate = setImmediate(fn, ...args); 129 this._immediates.add(immediate); 130 return immediate; 131 } 132 133 /** 134 * Clears an immediate. 135 * @param {Immediate} immediate Immediate to cancel 136 */ 137 clearImmediate(immediate) { 138 clearImmediate(immediate); 139 this._immediates.delete(immediate); 140 } 141 142 toJSON(...props) { 143 return Util.flatten(this, { domain: false }, ...props); 144 } 145 } 146 147 module.exports = BaseClient;