This commit is contained in:
MARCROCK22 2024-03-13 14:03:51 -04:00
parent cf780977bc
commit b059fb0d5d
8 changed files with 21 additions and 16 deletions

View File

@ -1,6 +1,8 @@
import type { Adapter } from './types'; import type { Adapter } from './types';
export class MemoryAdapter implements Adapter { export class MemoryAdapter implements Adapter {
isAsync = false;
readonly storage = new Map<string, string>(); readonly storage = new Map<string, string>();
readonly relationships = new Map<string, string[]>(); readonly relationships = new Map<string, string[]>();

View File

@ -14,6 +14,8 @@ interface RedisAdapterOptions {
} }
export class RedisAdapter implements Adapter { export class RedisAdapter implements Adapter {
isAsync = true;
client: import('ioredis').Redis; client: import('ioredis').Redis;
namespace: string; namespace: string;

View File

@ -1,4 +1,6 @@
export interface Adapter { export interface Adapter {
isAsync: boolean;
scan(query: string, keys?: false): RPV<any[]>; scan(query: string, keys?: false): RPV<any[]>;
scan(query: string, keys: true): RPV<string[]>; scan(query: string, keys: true): RPV<string[]>;
scan(query: string, keys?: boolean): RPV<(any | string)[]>; scan(query: string, keys?: boolean): RPV<(any | string)[]>;

View File

@ -5,6 +5,7 @@ import type { WorkerSendCacheRequest } from '../../websocket/discord/worker';
import type { Adapter } from './types'; import type { Adapter } from './types';
export class WorkerAdapter implements Adapter { export class WorkerAdapter implements Adapter {
isAsync = true;
promises = new Map<string, { resolve: (value: unknown) => void; timeout: NodeJS.Timeout }>(); promises = new Map<string, { resolve: (value: unknown) => void; timeout: NodeJS.Timeout }>();
constructor(readonly parent: MessagePort) {} constructor(readonly parent: MessagePort) {}

1
src/cache/index.ts vendored
View File

@ -102,7 +102,6 @@ export class Cache {
constructor( constructor(
public intents: number, public intents: number,
public adapter: Adapter, public adapter: Adapter,
public asyncCache = false,
readonly disabledCache: (NonGuildBased | GuildBased | GuildRelated)[] = [], readonly disabledCache: (NonGuildBased | GuildBased | GuildRelated)[] = [],
client?: BaseClient, client?: BaseClient,
) { ) {

View File

@ -97,7 +97,6 @@ export class BaseClient {
this.cache = new Cache( this.cache = new Cache(
this.cache?.intents ?? 0, this.cache?.intents ?? 0,
cache.adapter, cache.adapter,
cache.asyncCache ?? this.cache?.asyncCache,
cache.disabledCache ?? this.cache?.disabledCache, cache.disabledCache ?? this.cache?.disabledCache,
this, this,
); );
@ -147,7 +146,7 @@ export class BaseClient {
} }
if (!this.cache) { if (!this.cache) {
this.cache = new Cache(0, new MemoryAdapter(), false, [], this); this.cache = new Cache(0, new MemoryAdapter(), [], this);
} else { } else {
this.cache.__setClient(this); this.cache.__setClient(this);
} }
@ -290,7 +289,7 @@ export type RuntimeConfig = OmitInsert<InternalRuntimeConfig, 'intents', { inten
export type ServicesOptions = { export type ServicesOptions = {
rest?: ApiHandler; rest?: ApiHandler;
cache?: { adapter: Adapter; disabledCache?: Cache['disabledCache']; asyncCache?: boolean }; cache?: { adapter: Adapter; disabledCache?: Cache['disabledCache'] };
langs?: { langs?: {
default?: string; default?: string;
aliases?: Record<string, LocaleString[]>; aliases?: Record<string, LocaleString[]>;

View File

@ -10,7 +10,7 @@ import {
type WebhookMessage, type WebhookMessage,
} from '../..'; } from '../..';
import type { Client, WorkerClient } from '../../client'; 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 type { InteractionCreateBodyRequest, InteractionMessageUpdateBodyRequest } from '../../common/types/write';
import { import {
Message, Message,
@ -108,12 +108,12 @@ export class CommandContext<T extends OptionsRecord = {}, M extends keyof Regist
channel(mode?: 'cache'): ReturnCache<If<InferWithPrefix, AllChannels | undefined, AllChannels>>; channel(mode?: 'cache'): ReturnCache<If<InferWithPrefix, AllChannels | undefined, AllChannels>>;
channel(mode: 'cache' | 'rest' | 'flow' = 'cache') { channel(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (this.interaction?.channel && mode === '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) { switch (mode) {
case 'cache': case 'cache':
return ( return (
this.client.cache.channels?.get(this.channelId) || 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: default:
return this.client.channels.fetch(this.channelId, mode === 'rest'); return this.client.channels.fetch(this.channelId, mode === 'rest');
@ -124,12 +124,12 @@ export class CommandContext<T extends OptionsRecord = {}, M extends keyof Regist
me(mode?: 'cache'): ReturnCache<GuildMember | undefined>; me(mode?: 'cache'): ReturnCache<GuildMember | undefined>;
me(mode: 'cache' | 'rest' | 'flow' = 'cache') { me(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (!this.guildId) 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) { switch (mode) {
case 'cache': case 'cache':
return ( return (
this.client.cache.members?.get(this.client.botId, this.guildId) || 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: default:
return this.client.members.fetch(this.guildId, this.client.botId, mode === 'rest'); return this.client.members.fetch(this.guildId, this.client.botId, mode === 'rest');
@ -141,7 +141,7 @@ export class CommandContext<T extends OptionsRecord = {}, M extends keyof Regist
guild(mode: 'cache' | 'rest' | 'flow' = 'cache') { guild(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (!this.guildId) if (!this.guildId)
return (mode === 'cache' return (mode === 'cache'
? this.client.cache.asyncCache ? this.client.cache.adapter.isAsync
? Promise.resolve() ? Promise.resolve()
: undefined : undefined
: Promise.resolve()) as unknown as undefined; : Promise.resolve()) as unknown as undefined;
@ -149,7 +149,7 @@ export class CommandContext<T extends OptionsRecord = {}, M extends keyof Regist
case 'cache': case 'cache':
return ( return (
this.client.cache.guilds?.get(this.guildId) || this.client.cache.guilds?.get(this.guildId) ||
(this.client.cache.asyncCache ? (Promise.resolve() as any) : undefined) (this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined)
); );
default: default:
return this.client.guilds.fetch(this.guildId, mode === 'rest'); return this.client.guilds.fetch(this.guildId, mode === 'rest');

View File

@ -1,12 +1,12 @@
import { CommandContext, type WebhookMessage, type ReturnCache } from '../..'; import { CommandContext, type ReturnCache, type WebhookMessage } from '../..';
import { import {
ApplicationCommandType, ApplicationCommandType,
MessageFlags, MessageFlags,
type When,
toSnakeCase, toSnakeCase,
type InteractionCreateBodyRequest, type InteractionCreateBodyRequest,
type InteractionMessageUpdateBodyRequest, type InteractionMessageUpdateBodyRequest,
type UnionToTuple, type UnionToTuple,
type When,
} from '../../common'; } from '../../common';
import { import {
Message, Message,
@ -100,7 +100,7 @@ export class MenuCommandContext<
channel(mode?: 'cache'): ReturnCache<AllChannels>; channel(mode?: 'cache'): ReturnCache<AllChannels>;
channel(mode: 'cache' | 'rest' | 'flow' = 'cache') { channel(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (this.interaction?.channel && mode === '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'); return this.client.channels.fetch(this.channelId, mode === 'rest');
} }
@ -108,7 +108,7 @@ export class MenuCommandContext<
me(mode?: 'cache'): ReturnCache<GuildMember | undefined>; me(mode?: 'cache'): ReturnCache<GuildMember | undefined>;
me(mode: 'cache' | 'rest' | 'flow' = 'cache') { me(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (!this.guildId) 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) { switch (mode) {
case 'cache': case 'cache':
return this.client.cache.members?.get(this.client.botId, this.guildId); return this.client.cache.members?.get(this.client.botId, this.guildId);
@ -122,7 +122,7 @@ export class MenuCommandContext<
guild(mode: 'cache' | 'rest' | 'flow' = 'cache') { guild(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (!this.guildId) if (!this.guildId)
return ( 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; ) as any;
switch (mode) { switch (mode) {
case 'cache': case 'cache':