Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/discord.js/src/managers/CachedManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class CachedManager extends DataManager {
* @name CachedManager#_cache
*/
Object.defineProperty(this, '_cache', {
value: this.client.options.makeCache(
this.constructor[MakeCacheOverrideSymbol] ?? this.constructor,
this.holds,
this.constructor,
),
value: this.client.options.makeCache({
holds: this.holds,
manager: this.constructor,
managerType: this.constructor[MakeCacheOverrideSymbol] ?? this.constructor,
}),
});

if (iterable) {
Expand Down
14 changes: 9 additions & 5 deletions packages/discord.js/src/util/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ const { DefaultWebSocketManagerOptions } = require('@discordjs/ws');
const { version } = require('../../package.json');
const { toSnakeCase } = require('./Transformers.js');

// TODO(ckohen): switch order of params so full manager is first and "type" is optional
/**
* @typedef {Object} CacheFactoryParams
* @property {Function} holds The class that the cache will hold.
* @property {Function} manager The fully extended manager class the cache is being requested from.
* @property {Function} managerType The base manager class the cache is being requested from.
*/

/**
* @typedef {Function} CacheFactory
* @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.
* @param {CacheFactoryParams} params The parameters
* @returns {Collection} A Collection used to store the cache of the manager.
*/

Expand Down Expand Up @@ -121,7 +125,7 @@ class Options extends null {
const { Collection } = require('@discordjs/collection');
const { LimitedCollection } = require('./LimitedCollection.js');

return (managerType, _, manager) => {
return ({ managerType, manager }) => {
const setting = settings[manager.name] ?? settings[managerType.name];
/* eslint-disable-next-line eqeqeq */
if (setting == null) {
Expand Down
18 changes: 13 additions & 5 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5359,13 +5359,21 @@ export type OverriddenCaches =
| 'GuildMessageManager'
| 'GuildTextThreadManager';

export interface CacheFactoryParams<Manager extends keyof Caches> {
holds: Caches[Manager][1];
manager: CacheConstructors[keyof Caches];
managerType: CacheConstructors[Exclude<keyof Caches, OverriddenCaches>];
}

// 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 = (
managerType: CacheConstructors[Exclude<keyof Caches, OverriddenCaches>],
holds: Caches[(typeof manager)['name']][1],
manager: CacheConstructors[keyof Caches],
) => (typeof manager)['prototype'] extends DataManager<infer Key, infer Value, any> ? Collection<Key, Value> : never;
export type CacheFactory = ({
holds,
manager,
managerType,
}: CacheFactoryParams<keyof Caches>) => (typeof manager)['prototype'] extends DataManager<infer Key, infer Value, any>
? Collection<Key, Value>
: never;

export type CacheWithLimitsOptions = {
[K in keyof Caches]?: Caches[K][0]['prototype'] extends DataManager<infer Key, infer Value, any>
Expand Down