// deno-lint-ignore-file require-await no-explicit-any prefer-const import { PresenceUpdatePayload } from "../../types/mod.ts"; import { cache } from "../../util/cache.ts"; import { Collection } from "../../util/collection.ts"; import { Channel, Guild, Member, Message } from "../structures/mod.ts"; export type TableName = | "guilds" | "unavailableGuilds" | "channels" | "messages" | "members" | "presences"; function set( table: "guilds", key: string, value: Guild, ): Promise>; function set( table: "channels", key: string, value: Channel, ): Promise>; function set( table: "messages", key: string, value: Message, ): Promise>; function set( table: "members", key: string, value: Member, ): Promise>; function set( table: "presences", key: string, value: PresenceUpdatePayload, ): Promise>; function set( table: "unavailableGuilds", key: string, value: number, ): Promise>; async function set(table: TableName, key: string, value: any) { return cache[table].set(key, value); } function get(table: "guilds", key: string): Promise; function get(table: "channels", key: string): Promise; function get(table: "messages", key: string): Promise; function get(table: "members", key: string): Promise; function get( table: "presences", key: string, ): Promise; function get( table: "unavailableGuilds", key: string, ): Promise; async function get(table: TableName, key: string) { return cache[table].get(key); } function forEach( table: "guilds", callback: (value: Guild, key: string, map: Map) => unknown, ): void; function forEach( table: "unavailableGuilds", callback: (value: Guild, key: string, map: Map) => unknown, ): void; function forEach( table: "channels", callback: (value: Channel, key: string, map: Map) => unknown, ): void; function forEach( table: "messages", callback: (value: Message, key: string, map: Map) => unknown, ): void; function forEach( table: "members", callback: (value: Member, key: string, map: Map) => unknown, ): void; function forEach( table: TableName, callback: (value: any, key: string, map: Map) => unknown, ) { return cache[table].forEach(callback); } function filter( table: "guilds", callback: (value: Guild, key: string) => boolean, ): Promise>; function filter( table: "unavailableGuilds", callback: (value: Guild, key: string) => boolean, ): Promise>; function filter( table: "channels", callback: (value: Channel, key: string) => boolean, ): Promise>; function filter( table: "messages", callback: (value: Message, key: string) => boolean, ): Promise>; function filter( table: "members", callback: (value: Member, key: string) => boolean, ): Promise>; async function filter( table: TableName, callback: (value: any, key: string) => boolean, ) { return cache[table].filter(callback); } export let cacheHandlers = { /** Deletes all items from the cache */ clear: async function (table: TableName) { return cache[table].clear(); }, /** Deletes 1 item from cache using the key */ delete: async function (table: TableName, key: string) { return cache[table].delete(key); }, /** Check if something exists in cache with a key */ has: async function (table: TableName, key: string) { return cache[table].has(key); }, /** Get the number of key-value pairs */ size: async (table: TableName) => { return cache[table].size; }, // Done differently to have overloads /** Add a key value pair to the cache */ set, /** Get the value from the cache using its key */ get, /** Run a function on all items in this cache */ forEach, /** Allows you to filter our all items in this cache. */ filter, };