feat: cache callback, actually disable cache when disabledCache is true (#263)

* feat: cache callback, actually disable cache when disabledCache is "true"

* fix: types
This commit is contained in:
MARCROCK22 2024-09-07 13:46:42 -04:00 committed by GitHub
parent 8cc495a4db
commit ead838637c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 4 deletions

View File

@ -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<Record<keyof Cache['disabledCache'], boolean>> = 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<string, LocaleString[]>;

View File

@ -16,6 +16,9 @@ export interface ExtendContext {}
export interface ExtraProps {}
export interface UsingClient extends BaseClient {}
export type ParseClient<T extends BaseClient> = T;
export type ParseGlobalMiddlewares<T extends Record<string, MiddlewareContext>> = {
[K in keyof T]: MetadataMiddleware<T[K]>;
};
export interface InternalOptions {}
export interface CustomStructures {}