From ead838637c5b285e1b07e2db0646b53bd7a5a605 Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Sat, 7 Sep 2024 13:46:42 -0400 Subject: [PATCH] feat: cache callback, actually disable cache when disabledCache is `true` (#263) * feat: cache callback, actually disable cache when disabledCache is "true" * fix: types --- src/client/base.ts | 40 ++++++++++++++++++++++++++--- src/commands/applications/shared.ts | 3 +++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/client/base.ts b/src/client/base.ts index 0a75d76..a379b52 100644 --- a/src/client/base.ts +++ b/src/client/base.ts @@ -193,12 +193,41 @@ export class BaseClient { this.rest = rest; } if (cache) { + const caches: (keyof Cache['disabledCache'])[] = [ + 'bans', + 'channels', + 'emojis', + 'guilds', + 'members', + 'messages', + 'onPacket', + 'overwrites', + 'presences', + 'roles', + 'stageInstances', + 'stickers', + 'threads', + 'users', + 'voiceStates', + ]; + let disabledCache: Partial> = this.cache?.disabledCache ?? {}; + + if (typeof cache.disabledCache === 'boolean') { + for (const i of caches) { + disabledCache[i] = cache.disabledCache; + } + } else if (typeof cache.disabledCache === 'function') { + for (const i of caches) { + disabledCache[i] = cache.disabledCache(i); + } + } else if (typeof cache.disabledCache === 'object') { + disabledCache = cache.disabledCache; + } + this.cache = new Cache( this.cache?.intents ?? 0, cache?.adapter ?? this.cache?.adapter ?? new MemoryAdapter(), - (typeof cache.disabledCache === 'boolean' ? { onPacket: cache.disabledCache } : cache.disabledCache) ?? - this.cache?.disabledCache ?? - [], + disabledCache, this, ); } @@ -527,7 +556,10 @@ export type RuntimeConfig = OmitInsert< export interface ServicesOptions { rest?: ApiHandler; - cache?: { adapter?: Adapter; disabledCache?: true | Cache['disabledCache'] }; + cache?: { + adapter?: Adapter; + disabledCache?: boolean | Cache['disabledCache'] | ((cacheType: keyof Cache['disabledCache']) => boolean); + }; langs?: { default?: string; aliases?: Record; diff --git a/src/commands/applications/shared.ts b/src/commands/applications/shared.ts index c049db8..f8766bc 100644 --- a/src/commands/applications/shared.ts +++ b/src/commands/applications/shared.ts @@ -16,6 +16,9 @@ export interface ExtendContext {} export interface ExtraProps {} export interface UsingClient extends BaseClient {} export type ParseClient = T; +export type ParseGlobalMiddlewares> = { + [K in keyof T]: MetadataMiddleware; +}; export interface InternalOptions {} export interface CustomStructures {}