diff options
| author | Mohamed Bassem <me@mbassem.com> | 2025-09-14 18:16:40 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2025-09-14 18:16:57 +0000 |
| commit | 8d32055485858210252096483bb20533dc8bdf60 (patch) | |
| tree | ce8a1373411d1ce40aa0dbe6c37e707f0dbf4c98 /packages/shared/plugins.ts | |
| parent | 6ba61b46154e076fca47d3841b158105dbeeef80 (diff) | |
| download | karakeep-8d32055485858210252096483bb20533dc8bdf60.tar.zst | |
refactor: Move callsites to liteque to be behind a plugin
Diffstat (limited to 'packages/shared/plugins.ts')
| -rw-r--r-- | packages/shared/plugins.ts | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/packages/shared/plugins.ts b/packages/shared/plugins.ts index 2ce5826a..2aa7df4a 100644 --- a/packages/shared/plugins.ts +++ b/packages/shared/plugins.ts @@ -1,14 +1,17 @@ // Implementation inspired from Outline +import type { QueueClient } from "./queueing"; import logger from "./logger"; import { SearchIndexClient } from "./search"; export enum PluginType { Search = "search", + Queue = "queue", } interface PluginTypeMap { [PluginType.Search]: SearchIndexClient; + [PluginType.Queue]: QueueClient; } export interface TPlugin<T extends PluginType> { @@ -21,37 +24,38 @@ export interface PluginProvider<T> { getClient(): Promise<T | null>; } +// Preserve the key-dependent value type: for K, store TPlugin<K>[] +type ProviderMap = { [K in PluginType]: TPlugin<K>[] }; + export class PluginManager { - private static providers = new Map<PluginType, TPlugin<PluginType>[]>(); + private static providers: ProviderMap = { + [PluginType.Search]: [], + [PluginType.Queue]: [], + }; static register<T extends PluginType>(plugin: TPlugin<T>): void { - const p = PluginManager.providers.get(plugin.type); - if (!p) { - PluginManager.providers.set(plugin.type, [plugin]); - return; - } - p.push(plugin); + PluginManager.providers[plugin.type].push(plugin); } static async getClient<T extends PluginType>( type: T, ): Promise<PluginTypeMap[T] | null> { - const provider = PluginManager.providers.get(type); - if (!provider) { + const providers: TPlugin<T>[] = PluginManager.providers[type]; + if (providers.length === 0) { return null; } - return await provider[provider.length - 1].provider.getClient(); + return await providers[providers.length - 1]!.provider.getClient(); } static isRegistered<T extends PluginType>(type: T): boolean { - return !!PluginManager.providers.get(type); + return PluginManager.providers[type].length > 0; } static logAllPlugins() { logger.info("Plugins (Last one wins):"); for (const type of Object.values(PluginType)) { logger.info(` ${type}:`); - const plugins = PluginManager.providers.get(type); + const plugins = PluginManager.providers[type]; if (!plugins) { logger.info(" - None"); continue; |
