mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +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:
@@ -16,6 +16,7 @@ class ApplicationCommandPermissionsManager extends BaseManager {
|
||||
/**
|
||||
* The manager or command that this manager belongs to
|
||||
* @type {ApplicationCommandManager|ApplicationCommand}
|
||||
* @private
|
||||
*/
|
||||
this.manager = manager;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const DataManager = require('./DataManager');
|
||||
const { _cleanupSymbol } = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* Manages the API methods of a data model with a mutable cache of instances.
|
||||
@@ -13,6 +14,19 @@ class CachedManager extends DataManager {
|
||||
|
||||
Object.defineProperty(this, '_cache', { value: this.client.options.makeCache(this.constructor, this.holds) });
|
||||
|
||||
let cleanup = this._cache[_cleanupSymbol];
|
||||
if (cleanup) {
|
||||
cleanup = cleanup.bind(this._cache);
|
||||
client._cleanups.add(cleanup);
|
||||
client._finalizers.register(this, {
|
||||
cleanup,
|
||||
message:
|
||||
`Garbage Collection completed on ${this.constructor.name}, ` +
|
||||
`which had a ${this._cache.constructor.name} of ${this.holds.name}.`,
|
||||
name: this.constructor.name,
|
||||
});
|
||||
}
|
||||
|
||||
if (iterable) {
|
||||
for (const item of iterable) {
|
||||
this._add(item);
|
||||
|
||||
@@ -72,7 +72,7 @@ class GuildEmojiRoleManager extends DataManager {
|
||||
resolvedRoleIds.push(roleId);
|
||||
}
|
||||
|
||||
const newRoles = this.cache.keyArray().filter(id => !resolvedRoleIds.includes(id));
|
||||
const newRoles = [...this.cache.keys()].filter(id => !resolvedRoleIds.includes(id));
|
||||
return this.set(newRoles);
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ class GuildEmojiRoleManager extends DataManager {
|
||||
|
||||
clone() {
|
||||
const clone = new this.constructor(this.emoji);
|
||||
clone._patch(this.cache.keyArray().slice());
|
||||
clone._patch([...this.cache.keys()]);
|
||||
return clone;
|
||||
}
|
||||
|
||||
|
||||
@@ -172,7 +172,7 @@ class GuildMemberRoleManager extends DataManager {
|
||||
|
||||
clone() {
|
||||
const clone = new this.constructor(this.member);
|
||||
clone.member._roles = [...this.cache.keyArray()];
|
||||
clone.member._roles = [...this.cache.keys()];
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user