Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(Options): add types for cacheWithLimits #6095

Merged
merged 8 commits into from
Jul 16, 2021
5 changes: 2 additions & 3 deletions src/util/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @typedef {Function} CacheFactory
* @param {Function} manager The manager class the cache is being requested from.
* @param {Function} holds The class that the cache will hold.
* @returns {Collection} Cache instance that follows collection interface.
* @returns {Collection} A Collection used to store the cache of the manager.
*/

/**
Expand All @@ -34,8 +34,7 @@
* @property {number} [shardCount=1] The total amount of shards used by all processes of this bot
* (e.g. recommended shard count, shard count of the ShardingManager)
* @property {CacheFactory} [makeCache] Function to create a cache.
* (-1 or Infinity for unlimited - don't do this without message sweeping, otherwise memory usage will climb
* indefinitely)
* You can use your own function, or the {@link Options} class to customize the Collection used for the cache.
* @property {number} [messageCacheLifetime=0] How long a message should stay in the cache until it is considered
* sweepable (in seconds, 0 for forever)
* @property {number} [messageSweepInterval=0] How frequently to remove messages from the cache that are older than
Expand Down
51 changes: 49 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export class ClientUser extends User {
export class Options extends null {
private constructor();
public static createDefaultOptions(): ClientOptions;
public static cacheWithLimits(limits?: Record<string, number>): CacheFactory;
public static cacheWithLimits(limits?: CacheWithLimitOptions): CacheFactory;
public static cacheEverything(): CacheFactory;
}

Expand Down Expand Up @@ -2725,7 +2725,54 @@ export type BitFieldResolvable<T extends string, N extends number | bigint> =

export type BufferResolvable = Buffer | string;

export type CacheFactory = <T>(manager: { name: string }, holds: { name: string }) => Collection<Snowflake, T>;
export type CachedManagerTypes =
DTrombett marked this conversation as resolved.
Show resolved Hide resolved
| 'ApplicationCommandManager'
| 'BaseGuildEmojiManager'
| 'ChannelManager'
| 'GuildChannelManager'
| 'GuildManager'
| 'GuildMemberManager'
| 'GuildBanManager'
| 'MessageManager'
| 'PermissionOverwriteManager'
| 'PresenceManager'
| 'ReactionManager'
| 'ReactionUserManager'
| 'RoleManager'
| 'StageInstanceManager'
| 'ThreadManager'
| 'ThreadMemberManager'
| 'UserManager'
| 'VoiceStateManager';

export type CacheFactory = <T extends keyof CacheFactoryArgs>(
...args: CacheFactoryArgs[T]
) => Collection<Snowflake, CacheFactoryArgs[T][1]>;

export interface CacheFactoryArgs {
ApplicationCommandManager: [manager: typeof ApplicationCommandManager, holds: typeof ApplicationCommand];
BaseGuildEmojiManager: [manager: typeof BaseGuildEmojiManager, holds: typeof GuildEmoji];
ChannelManager: [manager: typeof ChannelManager, holds: typeof Channel];
GuildChannelManager: [manager: typeof GuildChannelManager, holds: typeof GuildChannel];
GuildManager: [manager: typeof GuildManager, holds: typeof Guild];
GuildMemberManager: [manager: typeof GuildMemberManager, holds: typeof GuildMember];
GuildBanManager: [manager: typeof GuildBanManager, holds: typeof GuildBan];
MessageManager: [manager: typeof MessageManager, holds: typeof Message];
PermissionOverwriteManager: [manager: typeof PermissionOverwriteManager, holds: typeof PermissionOverwrites];
PresenceManager: [manager: typeof PresenceManager, holds: typeof Presence];
ReactionManager: [manager: typeof ReactionManager, holds: typeof MessageReaction];
ReactionUserManager: [manager: typeof ReactionUserManager, holds: typeof User];
RoleManager: [manager: typeof RoleManager, holds: typeof Role];
StageInstanceManager: [manager: typeof StageInstanceManager, holds: typeof StageInstance];
ThreadManager: [manager: typeof ThreadManager, holds: typeof ThreadChannel];
ThreadMemberManager: [manager: typeof ThreadMemberManager, holds: typeof ThreadMember];
UserManager: [manager: typeof UserManager, holds: typeof User];
VoiceStateManager: [manager: typeof VoiceStateManager, holds: typeof VoiceState];
}

export type CacheWithLimitOptions = {
[K in keyof CacheFactoryArgs]?: number;
};

export interface ChannelCreationOverwrites {
allow?: PermissionResolvable;
Expand Down
2 changes: 2 additions & 0 deletions typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const client: Client = new Client({
intents: Intents.FLAGS.GUILDS,
makeCache: Options.cacheWithLimits({
MessageManager: 200,
// @ts-expect-error
Message: 100,
}),
});

Expand Down