From 597653435d432ea73c08886552e6a084a28aedd9 Mon Sep 17 00:00:00 2001 From: purocean Date: Fri, 15 Dec 2023 15:13:24 +0800 Subject: [PATCH] feat(plugin): add `ctx.getPluginApi` method --- src/renderer/__tests__/core/plugin.ts | 7 +++++++ src/renderer/context/index.ts | 1 + src/renderer/core/plugin.ts | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/renderer/__tests__/core/plugin.ts b/src/renderer/__tests__/core/plugin.ts index 3b5e552b7..2d34219dd 100644 --- a/src/renderer/__tests__/core/plugin.ts +++ b/src/renderer/__tests__/core/plugin.ts @@ -19,4 +19,11 @@ test('plugin usage', () => { }, 123) expect(fn).toHaveBeenCalledTimes(1) + + plugin.register({ + name: 'test2', + register: () => '12345', + }, 123) + + expect(plugin.getApi('test2')).toBe('12345') }) diff --git a/src/renderer/context/index.ts b/src/renderer/context/index.ts index 1a5e90283..bddcac909 100644 --- a/src/renderer/context/index.ts +++ b/src/renderer/context/index.ts @@ -69,6 +69,7 @@ const ctx = Object.freeze({ showExtensionManager: extension.showManager, getExtensionLoadStatus: extension.getLoadStatus, getExtensionInitialized: extension.getInitialized, + getPluginApi: plugin.getApi, version: __APP_VERSION__, }) diff --git a/src/renderer/core/plugin.ts b/src/renderer/core/plugin.ts index 891464064..483e852d4 100644 --- a/src/renderer/core/plugin.ts +++ b/src/renderer/core/plugin.ts @@ -4,10 +4,11 @@ const logger = getLogger('plugin') export interface Plugin { name: string; - register?: (ctx: Ctx) => void; + register?: (ctx: Ctx) => any; } const plugins: {[name: string]: Plugin} = {} +const apis: {[name: string]: any} = {} /** * Register a plugin. @@ -23,7 +24,16 @@ export function register (plugin: Plugin, ctx: Ctx) { } plugins[plugin.name] = plugin - plugin.register && plugin.register(ctx) + apis[plugin.name] = plugin.register && plugin.register(ctx) +} + +/** + * Get a plugin exported api. + * @param name + * @returns + */ +export function getApi (name: string): T { + return apis[name] } /**