Skip to content

Commit

Permalink
feat(plugin): add ctx.getPluginApi method
Browse files Browse the repository at this point in the history
  • Loading branch information
purocean committed Dec 15, 2023
1 parent 2466402 commit 5976534
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/renderer/__tests__/core/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
1 change: 1 addition & 0 deletions src/renderer/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const ctx = Object.freeze({
showExtensionManager: extension.showManager,
getExtensionLoadStatus: extension.getLoadStatus,
getExtensionInitialized: extension.getInitialized,
getPluginApi: plugin.getApi,
version: __APP_VERSION__,
})

Expand Down
14 changes: 12 additions & 2 deletions src/renderer/core/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const logger = getLogger('plugin')

export interface Plugin<Ctx = any> {
name: string;
register?: (ctx: Ctx) => void;
register?: (ctx: Ctx) => any;
}

const plugins: {[name: string]: Plugin} = {}
const apis: {[name: string]: any} = {}

/**
* Register a plugin.
Expand All @@ -23,7 +24,16 @@ export function register <Ctx> (plugin: Plugin<Ctx>, 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 <T = any> (name: string): T {
return apis[name]
}

/**
Expand Down

0 comments on commit 5976534

Please sign in to comment.