mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
fix(CachedManager): allow overriding constructor for makeCache (#9763)
* fix(CachedManager): allow overriding constructor for makeCache * feat: allow determining makeCache based on non-overriden constructor * types: cleanup leftovers --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const DataManager = require('./DataManager');
|
||||
const { MakeCacheOverrideSymbol } = require('../util/Symbols');
|
||||
|
||||
/**
|
||||
* Manages the API methods of a data model with a mutable cache of instances.
|
||||
@@ -18,7 +19,13 @@ class CachedManager extends DataManager {
|
||||
* @readonly
|
||||
* @name CachedManager#_cache
|
||||
*/
|
||||
Object.defineProperty(this, '_cache', { value: this.client.options.makeCache(this.constructor, this.holds) });
|
||||
Object.defineProperty(this, '_cache', {
|
||||
value: this.client.options.makeCache(
|
||||
this.constructor[MakeCacheOverrideSymbol] ?? this.constructor,
|
||||
this.holds,
|
||||
this.constructor,
|
||||
),
|
||||
});
|
||||
|
||||
if (iterable) {
|
||||
for (const item of iterable) {
|
||||
|
||||
@@ -7,6 +7,7 @@ const CachedManager = require('./CachedManager');
|
||||
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
|
||||
const { Message } = require('../structures/Message');
|
||||
const MessagePayload = require('../structures/MessagePayload');
|
||||
const { MakeCacheOverrideSymbol } = require('../util/Symbols');
|
||||
const { resolvePartialEmoji } = require('../util/Util');
|
||||
|
||||
/**
|
||||
@@ -15,6 +16,8 @@ const { resolvePartialEmoji } = require('../util/Util');
|
||||
* @abstract
|
||||
*/
|
||||
class MessageManager extends CachedManager {
|
||||
static [MakeCacheOverrideSymbol] = MessageManager;
|
||||
|
||||
constructor(channel, iterable) {
|
||||
super(channel.client, Message, iterable);
|
||||
|
||||
|
||||
@@ -6,12 +6,15 @@ const { Routes } = require('discord-api-types/v10');
|
||||
const CachedManager = require('./CachedManager');
|
||||
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
|
||||
const ThreadChannel = require('../structures/ThreadChannel');
|
||||
const { MakeCacheOverrideSymbol } = require('../util/Symbols');
|
||||
|
||||
/**
|
||||
* Manages API methods for thread-based channels and stores their cache.
|
||||
* @extends {CachedManager}
|
||||
*/
|
||||
class ThreadManager extends CachedManager {
|
||||
static [MakeCacheOverrideSymbol] = ThreadManager;
|
||||
|
||||
constructor(channel, iterable) {
|
||||
super(channel.client, ThreadChannel, iterable);
|
||||
|
||||
|
||||
@@ -4,10 +4,12 @@ const { DefaultRestOptions, DefaultUserAgentAppendix } = require('@discordjs/res
|
||||
const { toSnakeCase } = require('./Transformers');
|
||||
const { version } = require('../../package.json');
|
||||
|
||||
// TODO(ckohen): switch order of params so full manager is first and "type" is optional
|
||||
/**
|
||||
* @typedef {Function} CacheFactory
|
||||
* @param {Function} manager The manager class the cache is being requested from.
|
||||
* @param {Function} managerType The base manager class the cache is being requested from.
|
||||
* @param {Function} holds The class that the cache will hold.
|
||||
* @param {Function} manager The fully extended manager class the cache is being requested from.
|
||||
* @returns {Collection} A Collection used to store the cache of the manager.
|
||||
*/
|
||||
|
||||
@@ -150,8 +152,8 @@ class Options extends null {
|
||||
const { Collection } = require('@discordjs/collection');
|
||||
const LimitedCollection = require('./LimitedCollection');
|
||||
|
||||
return manager => {
|
||||
const setting = settings[manager.name];
|
||||
return (managerType, _, manager) => {
|
||||
const setting = settings[manager.name] ?? settings[managerType.name];
|
||||
/* eslint-disable-next-line eqeqeq */
|
||||
if (setting == null) {
|
||||
return new Collection();
|
||||
|
||||
3
packages/discord.js/src/util/Symbols.js
Normal file
3
packages/discord.js/src/util/Symbols.js
Normal file
@@ -0,0 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
exports.MakeCacheOverrideSymbol = Symbol('djs.managers.makeCacheOverride');
|
||||
15
packages/discord.js/typings/index.d.ts
vendored
15
packages/discord.js/typings/index.d.ts
vendored
@@ -4720,17 +4720,19 @@ export interface Caches {
|
||||
AutoModerationRuleManager: [manager: typeof AutoModerationRuleManager, holds: typeof AutoModerationRule];
|
||||
ApplicationCommandManager: [manager: typeof ApplicationCommandManager, holds: typeof ApplicationCommand];
|
||||
BaseGuildEmojiManager: [manager: typeof BaseGuildEmojiManager, holds: typeof GuildEmoji];
|
||||
DMMessageManager: [manager: typeof MessageManager, holds: typeof Message<false>];
|
||||
GuildEmojiManager: [manager: typeof GuildEmojiManager, holds: typeof GuildEmoji];
|
||||
// TODO: ChannelManager: [manager: typeof ChannelManager, holds: typeof Channel];
|
||||
// TODO: GuildChannelManager: [manager: typeof GuildChannelManager, holds: typeof GuildChannel];
|
||||
// TODO: GuildManager: [manager: typeof GuildManager, holds: typeof Guild];
|
||||
GuildMemberManager: [manager: typeof GuildMemberManager, holds: typeof GuildMember];
|
||||
GuildBanManager: [manager: typeof GuildBanManager, holds: typeof GuildBan];
|
||||
GuildForumThreadManager: [manager: typeof GuildForumThreadManager, holds: typeof ThreadChannel];
|
||||
GuildForumThreadManager: [manager: typeof GuildForumThreadManager, holds: typeof ThreadChannel<true>];
|
||||
GuildInviteManager: [manager: typeof GuildInviteManager, holds: typeof Invite];
|
||||
GuildMessageManager: [manager: typeof GuildMessageManager, holds: typeof Message<true>];
|
||||
GuildScheduledEventManager: [manager: typeof GuildScheduledEventManager, holds: typeof GuildScheduledEvent];
|
||||
GuildStickerManager: [manager: typeof GuildStickerManager, holds: typeof Sticker];
|
||||
GuildTextThreadManager: [manager: typeof GuildTextThreadManager, holds: typeof ThreadChannel];
|
||||
GuildTextThreadManager: [manager: typeof GuildTextThreadManager, holds: typeof ThreadChannel<false>];
|
||||
MessageManager: [manager: typeof MessageManager, holds: typeof Message];
|
||||
// TODO: PermissionOverwriteManager: [manager: typeof PermissionOverwriteManager, holds: typeof PermissionOverwrites];
|
||||
PresenceManager: [manager: typeof PresenceManager, holds: typeof Presence];
|
||||
@@ -4748,11 +4750,18 @@ export type CacheConstructors = {
|
||||
[K in keyof Caches]: Caches[K][0] & { name: K };
|
||||
};
|
||||
|
||||
type OverriddenCaches =
|
||||
| 'DMMessageManager'
|
||||
| 'GuildForumThreadManager'
|
||||
| 'GuildMessageManager'
|
||||
| 'GuildTextThreadManager';
|
||||
|
||||
// This doesn't actually work the way it looks 😢.
|
||||
// Narrowing the type of `manager.name` doesn't propagate type information to `holds` and the return type.
|
||||
export type CacheFactory = (
|
||||
manager: CacheConstructors[keyof Caches],
|
||||
managerType: CacheConstructors[Exclude<keyof Caches, OverriddenCaches>],
|
||||
holds: Caches[(typeof manager)['name']][1],
|
||||
manager: CacheConstructors[keyof Caches],
|
||||
) => (typeof manager)['prototype'] extends DataManager<infer K, infer V, any> ? Collection<K, V> : never;
|
||||
|
||||
export type CacheWithLimitsOptions = {
|
||||
|
||||
Reference in New Issue
Block a user