mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
feat(Util): add SweptCollection for auto sweeping of caches (#6110)
Co-authored-by: DTrombett <73136330+DTrombett@users.noreply.github.com> Co-authored-by: 1Computer1 <22125769+1Computer1@users.noreply.github.com> Co-authored-by: Antonio Román <kyradiscord@gmail.com> Co-authored-by: NotSugden <28943913+NotSugden@users.noreply.github.com> Co-authored-by: Shino <shinotheshino@gmail.com> Co-authored-by: SpaceEEC <24881032+SpaceEEC@users.noreply.github.com> Co-authored-by: Noel <icrawltogo@gmail.com> Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
@@ -72,6 +72,20 @@ class Client extends BaseClient {
|
||||
|
||||
this._validateOptions();
|
||||
|
||||
/**
|
||||
* Functions called when a cache is garbage collected or the Client is destroyed
|
||||
* @type {Set<Function>}
|
||||
* @private
|
||||
*/
|
||||
this._cleanups = new Set();
|
||||
|
||||
/**
|
||||
* The finalizers used to cleanup items.
|
||||
* @type {FinalizationRegistry}
|
||||
* @private
|
||||
*/
|
||||
this._finalizers = new FinalizationRegistry(this._finalize.bind(this));
|
||||
|
||||
/**
|
||||
* The WebSocket manager of the client
|
||||
* @type {WebSocketManager}
|
||||
@@ -161,6 +175,10 @@ class Client extends BaseClient {
|
||||
this.readyAt = null;
|
||||
|
||||
if (this.options.messageSweepInterval > 0) {
|
||||
process.emitWarning(
|
||||
'The message sweeping client options are deprecated, use the makeCache option with a SweptCollection instead.',
|
||||
'DeprecationWarning',
|
||||
);
|
||||
this.sweepMessageInterval = setInterval(
|
||||
this.sweepMessages.bind(this),
|
||||
this.options.messageSweepInterval * 1000,
|
||||
@@ -247,6 +265,10 @@ class Client extends BaseClient {
|
||||
*/
|
||||
destroy() {
|
||||
super.destroy();
|
||||
|
||||
for (const fn of this._cleanups) fn();
|
||||
this._cleanups.clear();
|
||||
|
||||
if (this.sweepMessageInterval) clearInterval(this.sweepMessageInterval);
|
||||
|
||||
this.ws.destroy();
|
||||
@@ -346,6 +368,23 @@ class Client extends BaseClient {
|
||||
const data = await this.api('sticker-packs').get();
|
||||
return new Collection(data.sticker_packs.map(p => [p.id, new StickerPack(this, p)]));
|
||||
}
|
||||
/**
|
||||
* A last ditch cleanup function for garbage collection.
|
||||
* @param {Function} options.cleanup The function called to GC
|
||||
* @param {string} [options.message] The message to send after a successful GC
|
||||
* @param {string} [options.name] The name of the item being GCed
|
||||
*/
|
||||
_finalize({ cleanup, message, name }) {
|
||||
try {
|
||||
cleanup();
|
||||
this._cleanups.delete(cleanup);
|
||||
if (message) {
|
||||
this.emit(Events.DEBUG, message);
|
||||
}
|
||||
} catch {
|
||||
this.emit(Events.DEBUG, `Garbage collection failed on ${name ?? 'an unknown item'}.`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sweeps all text-based channels' messages and removes the ones older than the max message lifetime.
|
||||
|
||||
Reference in New Issue
Block a user