diff --git a/src/cache/adapters/default.ts b/src/cache/adapters/default.ts index 3db806b..9fab9e4 100644 --- a/src/cache/adapters/default.ts +++ b/src/cache/adapters/default.ts @@ -1,6 +1,8 @@ import type { Adapter } from './types'; export class MemoryAdapter implements Adapter { + isAsync = false; + readonly storage = new Map(); readonly relationships = new Map(); diff --git a/src/cache/adapters/redis.ts b/src/cache/adapters/redis.ts index a242580..c9a54b1 100644 --- a/src/cache/adapters/redis.ts +++ b/src/cache/adapters/redis.ts @@ -14,6 +14,8 @@ interface RedisAdapterOptions { } export class RedisAdapter implements Adapter { + isAsync = true; + client: import('ioredis').Redis; namespace: string; diff --git a/src/cache/adapters/types.ts b/src/cache/adapters/types.ts index 637ff7f..137bfa0 100644 --- a/src/cache/adapters/types.ts +++ b/src/cache/adapters/types.ts @@ -1,4 +1,6 @@ export interface Adapter { + isAsync: boolean; + scan(query: string, keys?: false): RPV; scan(query: string, keys: true): RPV; scan(query: string, keys?: boolean): RPV<(any | string)[]>; diff --git a/src/cache/adapters/workeradapter.ts b/src/cache/adapters/workeradapter.ts index 09f60c2..8ee1745 100644 --- a/src/cache/adapters/workeradapter.ts +++ b/src/cache/adapters/workeradapter.ts @@ -5,6 +5,7 @@ import type { WorkerSendCacheRequest } from '../../websocket/discord/worker'; import type { Adapter } from './types'; export class WorkerAdapter implements Adapter { + isAsync = true; promises = new Map void; timeout: NodeJS.Timeout }>(); constructor(readonly parent: MessagePort) {} diff --git a/src/cache/index.ts b/src/cache/index.ts index 086dd79..2a165e8 100644 --- a/src/cache/index.ts +++ b/src/cache/index.ts @@ -102,7 +102,6 @@ export class Cache { constructor( public intents: number, public adapter: Adapter, - public asyncCache = false, readonly disabledCache: (NonGuildBased | GuildBased | GuildRelated)[] = [], client?: BaseClient, ) { diff --git a/src/client/base.ts b/src/client/base.ts index 4f6cc68..fdb3c28 100644 --- a/src/client/base.ts +++ b/src/client/base.ts @@ -97,7 +97,6 @@ export class BaseClient { this.cache = new Cache( this.cache?.intents ?? 0, cache.adapter, - cache.asyncCache ?? this.cache?.asyncCache, cache.disabledCache ?? this.cache?.disabledCache, this, ); @@ -147,7 +146,7 @@ export class BaseClient { } if (!this.cache) { - this.cache = new Cache(0, new MemoryAdapter(), false, [], this); + this.cache = new Cache(0, new MemoryAdapter(), [], this); } else { this.cache.__setClient(this); } @@ -290,7 +289,7 @@ export type RuntimeConfig = OmitInsert; diff --git a/src/commands/applications/chatcontext.ts b/src/commands/applications/chatcontext.ts index abcc68a..ddf4e70 100644 --- a/src/commands/applications/chatcontext.ts +++ b/src/commands/applications/chatcontext.ts @@ -10,7 +10,7 @@ import { type WebhookMessage, } from '../..'; import type { Client, WorkerClient } from '../../client'; -import { MessageFlags, type When, type If, type UnionToTuple } from '../../common'; +import { MessageFlags, type If, type UnionToTuple, type When } from '../../common'; import type { InteractionCreateBodyRequest, InteractionMessageUpdateBodyRequest } from '../../common/types/write'; import { Message, @@ -108,12 +108,12 @@ export class CommandContext>; channel(mode: 'cache' | 'rest' | 'flow' = 'cache') { if (this.interaction?.channel && mode === 'cache') - return this.client.cache.asyncCache ? Promise.resolve(this.interaction.channel) : this.interaction.channel; + return this.client.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel; switch (mode) { case 'cache': return ( this.client.cache.channels?.get(this.channelId) || - (this.client.cache.asyncCache ? (Promise.resolve() as any) : undefined) + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) ); default: return this.client.channels.fetch(this.channelId, mode === 'rest'); @@ -124,12 +124,12 @@ export class CommandContext; me(mode: 'cache' | 'rest' | 'flow' = 'cache') { if (!this.guildId) - return mode === 'cache' ? (this.client.cache.asyncCache ? Promise.resolve() : undefined) : Promise.resolve(); + return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { case 'cache': return ( this.client.cache.members?.get(this.client.botId, this.guildId) || - (this.client.cache.asyncCache ? (Promise.resolve() as any) : undefined) + (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined) ); default: return this.client.members.fetch(this.guildId, this.client.botId, mode === 'rest'); @@ -141,7 +141,7 @@ export class CommandContext; channel(mode: 'cache' | 'rest' | 'flow' = 'cache') { if (this.interaction?.channel && mode === 'cache') - return this.client.cache.asyncCache ? Promise.resolve(this.interaction.channel) : this.interaction.channel; + return this.client.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel; return this.client.channels.fetch(this.channelId, mode === 'rest'); } @@ -108,7 +108,7 @@ export class MenuCommandContext< me(mode?: 'cache'): ReturnCache; me(mode: 'cache' | 'rest' | 'flow' = 'cache') { if (!this.guildId) - return mode === 'cache' ? (this.client.cache.asyncCache ? Promise.resolve() : undefined) : Promise.resolve(); + return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve(); switch (mode) { case 'cache': return this.client.cache.members?.get(this.client.botId, this.guildId); @@ -122,7 +122,7 @@ export class MenuCommandContext< guild(mode: 'cache' | 'rest' | 'flow' = 'cache') { if (!this.guildId) return ( - mode === 'cache' ? (this.client.cache.asyncCache ? Promise.resolve() : undefined) : Promise.resolve() + mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve() ) as any; switch (mode) { case 'cache':