From b94896a0f8fa43b957a9bdd6ab57ada0ab8101af Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sun, 27 Jul 2025 19:37:11 +0100 Subject: refactor: Extract meilisearch as a plugin --- packages/shared/plugins.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 packages/shared/plugins.ts (limited to 'packages/shared/plugins.ts') diff --git a/packages/shared/plugins.ts b/packages/shared/plugins.ts new file mode 100644 index 00000000..2ce5826a --- /dev/null +++ b/packages/shared/plugins.ts @@ -0,0 +1,64 @@ +// Implementation inspired from Outline + +import logger from "./logger"; +import { SearchIndexClient } from "./search"; + +export enum PluginType { + Search = "search", +} + +interface PluginTypeMap { + [PluginType.Search]: SearchIndexClient; +} + +export interface TPlugin { + type: T; + name: string; + provider: PluginProvider; +} + +export interface PluginProvider { + getClient(): Promise; +} + +export class PluginManager { + private static providers = new Map[]>(); + + static register(plugin: TPlugin): void { + const p = PluginManager.providers.get(plugin.type); + if (!p) { + PluginManager.providers.set(plugin.type, [plugin]); + return; + } + p.push(plugin); + } + + static async getClient( + type: T, + ): Promise { + const provider = PluginManager.providers.get(type); + if (!provider) { + return null; + } + return await provider[provider.length - 1].provider.getClient(); + } + + static isRegistered(type: T): boolean { + return !!PluginManager.providers.get(type); + } + + static logAllPlugins() { + logger.info("Plugins (Last one wins):"); + for (const type of Object.values(PluginType)) { + logger.info(` ${type}:`); + const plugins = PluginManager.providers.get(type); + if (!plugins) { + logger.info(" - None"); + continue; + } + for (const plugin of plugins) { + logger.info(` - ${plugin.name}`); + } + } + } +} -- cgit v1.3-1-g0d28