mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +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';
|
'use strict';
|
||||||
|
|
||||||
const DataManager = require('./DataManager');
|
const DataManager = require('./DataManager');
|
||||||
|
const { MakeCacheOverrideSymbol } = require('../util/Symbols');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages the API methods of a data model with a mutable cache of instances.
|
* Manages the API methods of a data model with a mutable cache of instances.
|
||||||
@@ -18,7 +19,13 @@ class CachedManager extends DataManager {
|
|||||||
* @readonly
|
* @readonly
|
||||||
* @name CachedManager#_cache
|
* @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) {
|
if (iterable) {
|
||||||
for (const item of iterable) {
|
for (const item of iterable) {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ const CachedManager = require('./CachedManager');
|
|||||||
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
|
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
|
||||||
const { Message } = require('../structures/Message');
|
const { Message } = require('../structures/Message');
|
||||||
const MessagePayload = require('../structures/MessagePayload');
|
const MessagePayload = require('../structures/MessagePayload');
|
||||||
|
const { MakeCacheOverrideSymbol } = require('../util/Symbols');
|
||||||
const { resolvePartialEmoji } = require('../util/Util');
|
const { resolvePartialEmoji } = require('../util/Util');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -15,6 +16,8 @@ const { resolvePartialEmoji } = require('../util/Util');
|
|||||||
* @abstract
|
* @abstract
|
||||||
*/
|
*/
|
||||||
class MessageManager extends CachedManager {
|
class MessageManager extends CachedManager {
|
||||||
|
static [MakeCacheOverrideSymbol] = MessageManager;
|
||||||
|
|
||||||
constructor(channel, iterable) {
|
constructor(channel, iterable) {
|
||||||
super(channel.client, Message, iterable);
|
super(channel.client, Message, iterable);
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,15 @@ const { Routes } = require('discord-api-types/v10');
|
|||||||
const CachedManager = require('./CachedManager');
|
const CachedManager = require('./CachedManager');
|
||||||
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
|
const { DiscordjsTypeError, ErrorCodes } = require('../errors');
|
||||||
const ThreadChannel = require('../structures/ThreadChannel');
|
const ThreadChannel = require('../structures/ThreadChannel');
|
||||||
|
const { MakeCacheOverrideSymbol } = require('../util/Symbols');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages API methods for thread-based channels and stores their cache.
|
* Manages API methods for thread-based channels and stores their cache.
|
||||||
* @extends {CachedManager}
|
* @extends {CachedManager}
|
||||||
*/
|
*/
|
||||||
class ThreadManager extends CachedManager {
|
class ThreadManager extends CachedManager {
|
||||||
|
static [MakeCacheOverrideSymbol] = ThreadManager;
|
||||||
|
|
||||||
constructor(channel, iterable) {
|
constructor(channel, iterable) {
|
||||||
super(channel.client, ThreadChannel, iterable);
|
super(channel.client, ThreadChannel, iterable);
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ const { DefaultRestOptions, DefaultUserAgentAppendix } = require('@discordjs/res
|
|||||||
const { toSnakeCase } = require('./Transformers');
|
const { toSnakeCase } = require('./Transformers');
|
||||||
const { version } = require('../../package.json');
|
const { version } = require('../../package.json');
|
||||||
|
|
||||||
|
// TODO(ckohen): switch order of params so full manager is first and "type" is optional
|
||||||
/**
|
/**
|
||||||
* @typedef {Function} CacheFactory
|
* @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} 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.
|
* @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 { Collection } = require('@discordjs/collection');
|
||||||
const LimitedCollection = require('./LimitedCollection');
|
const LimitedCollection = require('./LimitedCollection');
|
||||||
|
|
||||||
return manager => {
|
return (managerType, _, manager) => {
|
||||||
const setting = settings[manager.name];
|
const setting = settings[manager.name] ?? settings[managerType.name];
|
||||||
/* eslint-disable-next-line eqeqeq */
|
/* eslint-disable-next-line eqeqeq */
|
||||||
if (setting == null) {
|
if (setting == null) {
|
||||||
return new Collection();
|
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];
|
AutoModerationRuleManager: [manager: typeof AutoModerationRuleManager, holds: typeof AutoModerationRule];
|
||||||
ApplicationCommandManager: [manager: typeof ApplicationCommandManager, holds: typeof ApplicationCommand];
|
ApplicationCommandManager: [manager: typeof ApplicationCommandManager, holds: typeof ApplicationCommand];
|
||||||
BaseGuildEmojiManager: [manager: typeof BaseGuildEmojiManager, holds: typeof GuildEmoji];
|
BaseGuildEmojiManager: [manager: typeof BaseGuildEmojiManager, holds: typeof GuildEmoji];
|
||||||
|
DMMessageManager: [manager: typeof MessageManager, holds: typeof Message<false>];
|
||||||
GuildEmojiManager: [manager: typeof GuildEmojiManager, holds: typeof GuildEmoji];
|
GuildEmojiManager: [manager: typeof GuildEmojiManager, holds: typeof GuildEmoji];
|
||||||
// TODO: ChannelManager: [manager: typeof ChannelManager, holds: typeof Channel];
|
// TODO: ChannelManager: [manager: typeof ChannelManager, holds: typeof Channel];
|
||||||
// TODO: GuildChannelManager: [manager: typeof GuildChannelManager, holds: typeof GuildChannel];
|
// TODO: GuildChannelManager: [manager: typeof GuildChannelManager, holds: typeof GuildChannel];
|
||||||
// TODO: GuildManager: [manager: typeof GuildManager, holds: typeof Guild];
|
// TODO: GuildManager: [manager: typeof GuildManager, holds: typeof Guild];
|
||||||
GuildMemberManager: [manager: typeof GuildMemberManager, holds: typeof GuildMember];
|
GuildMemberManager: [manager: typeof GuildMemberManager, holds: typeof GuildMember];
|
||||||
GuildBanManager: [manager: typeof GuildBanManager, holds: typeof GuildBan];
|
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];
|
GuildInviteManager: [manager: typeof GuildInviteManager, holds: typeof Invite];
|
||||||
|
GuildMessageManager: [manager: typeof GuildMessageManager, holds: typeof Message<true>];
|
||||||
GuildScheduledEventManager: [manager: typeof GuildScheduledEventManager, holds: typeof GuildScheduledEvent];
|
GuildScheduledEventManager: [manager: typeof GuildScheduledEventManager, holds: typeof GuildScheduledEvent];
|
||||||
GuildStickerManager: [manager: typeof GuildStickerManager, holds: typeof Sticker];
|
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];
|
MessageManager: [manager: typeof MessageManager, holds: typeof Message];
|
||||||
// TODO: PermissionOverwriteManager: [manager: typeof PermissionOverwriteManager, holds: typeof PermissionOverwrites];
|
// TODO: PermissionOverwriteManager: [manager: typeof PermissionOverwriteManager, holds: typeof PermissionOverwrites];
|
||||||
PresenceManager: [manager: typeof PresenceManager, holds: typeof Presence];
|
PresenceManager: [manager: typeof PresenceManager, holds: typeof Presence];
|
||||||
@@ -4748,11 +4750,18 @@ export type CacheConstructors = {
|
|||||||
[K in keyof Caches]: Caches[K][0] & { name: K };
|
[K in keyof Caches]: Caches[K][0] & { name: K };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type OverriddenCaches =
|
||||||
|
| 'DMMessageManager'
|
||||||
|
| 'GuildForumThreadManager'
|
||||||
|
| 'GuildMessageManager'
|
||||||
|
| 'GuildTextThreadManager';
|
||||||
|
|
||||||
// This doesn't actually work the way it looks 😢.
|
// 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.
|
// Narrowing the type of `manager.name` doesn't propagate type information to `holds` and the return type.
|
||||||
export type CacheFactory = (
|
export type CacheFactory = (
|
||||||
manager: CacheConstructors[keyof Caches],
|
managerType: CacheConstructors[Exclude<keyof Caches, OverriddenCaches>],
|
||||||
holds: Caches[(typeof manager)['name']][1],
|
holds: Caches[(typeof manager)['name']][1],
|
||||||
|
manager: CacheConstructors[keyof Caches],
|
||||||
) => (typeof manager)['prototype'] extends DataManager<infer K, infer V, any> ? Collection<K, V> : never;
|
) => (typeof manager)['prototype'] extends DataManager<infer K, infer V, any> ? Collection<K, V> : never;
|
||||||
|
|
||||||
export type CacheWithLimitsOptions = {
|
export type CacheWithLimitsOptions = {
|
||||||
|
|||||||
Reference in New Issue
Block a user