mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +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
|
* @name CachedManager#_cache
|
||||||
*/
|
*/
|
||||||
Object.defineProperty(this, '_cache', {
|
Object.defineProperty(this, '_cache', {
|
||||||
value: this.client.options.makeCache(
|
value: this.client.options.makeCache({
|
||||||
this.constructor[MakeCacheOverrideSymbol] ?? this.constructor,
|
holds: this.holds,
|
||||||
this.holds,
|
manager: this.constructor,
|
||||||
this.constructor,
|
managerType: this.constructor[MakeCacheOverrideSymbol] ?? this.constructor,
|
||||||
),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (iterable) {
|
if (iterable) {
|
||||||
|
|||||||
@@ -5,12 +5,16 @@ const { DefaultWebSocketManagerOptions } = require('@discordjs/ws');
|
|||||||
const { version } = require('../../package.json');
|
const { version } = require('../../package.json');
|
||||||
const { toSnakeCase } = require('./Transformers.js');
|
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
|
* @typedef {Function} CacheFactory
|
||||||
* @param {Function} managerType The base manager class the cache is being requested from.
|
* @param {CacheFactoryParams} params The parameters
|
||||||
* @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.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -121,7 +125,7 @@ class Options extends null {
|
|||||||
const { Collection } = require('@discordjs/collection');
|
const { Collection } = require('@discordjs/collection');
|
||||||
const { LimitedCollection } = require('./LimitedCollection.js');
|
const { LimitedCollection } = require('./LimitedCollection.js');
|
||||||
|
|
||||||
return (managerType, _, manager) => {
|
return ({ managerType, manager }) => {
|
||||||
const setting = settings[manager.name] ?? settings[managerType.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) {
|
||||||
|
|||||||
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'
|
| 'GuildMessageManager'
|
||||||
| 'GuildTextThreadManager';
|
| '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 😢.
|
// 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 = ({
|
||||||
managerType: CacheConstructors[Exclude<keyof Caches, OverriddenCaches>],
|
holds,
|
||||||
holds: Caches[(typeof manager)['name']][1],
|
manager,
|
||||||
manager: CacheConstructors[keyof Caches],
|
managerType,
|
||||||
) => (typeof manager)['prototype'] extends DataManager<infer Key, infer Value, any> ? Collection<Key, Value> : never;
|
}: CacheFactoryParams<keyof Caches>) => (typeof manager)['prototype'] extends DataManager<infer Key, infer Value, any>
|
||||||
|
? Collection<Key, Value>
|
||||||
|
: never;
|
||||||
|
|
||||||
export type CacheWithLimitsOptions = {
|
export type CacheWithLimitsOptions = {
|
||||||
[K in keyof Caches]?: Caches[K][0]['prototype'] extends DataManager<infer Key, infer Value, any>
|
[K in keyof Caches]?: Caches[K][0]['prototype'] extends DataManager<infer Key, infer Value, any>
|
||||||
|
|||||||
Reference in New Issue
Block a user