mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 08:03:30 +01:00
refactor(makeCache)!: make params an object (#11172)
BREAKING CHANGE: `Options.makeCache` now sends all of its parameters in an object rather than 3 individual parameters Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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) {
|
||||
|
||||
18
packages/discord.js/typings/index.d.ts
vendored
18
packages/discord.js/typings/index.d.ts
vendored
@@ -5368,13 +5368,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>
|
||||
|
||||
Reference in New Issue
Block a user