84 lines
2.0 KiB
JavaScript
84 lines
2.0 KiB
JavaScript
import IpcAdapter from './IpcAdapter'
|
|
|
|
export const createPluginManager = (registry) => {
|
|
let activePluginId = null
|
|
let adapter = null
|
|
|
|
return {
|
|
listPlugins() {
|
|
return registry.list()
|
|
},
|
|
|
|
getPlugin(pluginId) {
|
|
return registry.get(pluginId)
|
|
},
|
|
|
|
getAdapter(pluginId, adapterConfig = {}) {
|
|
const plugin = registry.get(pluginId)
|
|
|
|
if (!plugin) {
|
|
throw new Error(`Plugin not found: ${pluginId}`)
|
|
}
|
|
|
|
return plugin.createAdapter(adapterConfig)
|
|
},
|
|
|
|
setActivePlugin(pluginId, adapterConfig = {}) {
|
|
activePluginId = pluginId
|
|
adapter = this.getAdapter(pluginId, adapterConfig)
|
|
return adapter
|
|
},
|
|
|
|
async testPlugin(pluginId, adapterConfig = {}) {
|
|
const plugin = registry.get(pluginId)
|
|
|
|
if (!plugin) {
|
|
throw new Error(`Plugin not found: ${pluginId}`)
|
|
}
|
|
|
|
const adapter = this.getAdapter(pluginId, adapterConfig)
|
|
await adapter.init()
|
|
|
|
return await adapter.testConnection()
|
|
},
|
|
|
|
getActiveAdapter() {
|
|
return adapter
|
|
},
|
|
|
|
getActivePluginId() {
|
|
return activePluginId
|
|
},
|
|
}
|
|
}
|
|
|
|
// Client for calling manager through IPC
|
|
export const createPluginManagerClient = () => {
|
|
return {
|
|
listPlugins() {
|
|
return window.api.pluginManagerCall('listPlugins')
|
|
},
|
|
|
|
getAdapter(pluginId, config) {
|
|
return new IpcAdapter(pluginId, config)
|
|
},
|
|
|
|
setActivePlugin(pluginId, adapterConfig) {
|
|
return window.api.pluginManagerCall(
|
|
'setActivePlugin',
|
|
pluginId,
|
|
adapterConfig,
|
|
)
|
|
},
|
|
|
|
async testPlugin(id, config) {
|
|
const test = await window.api.pluginManagerCall(
|
|
'testPlugin',
|
|
id,
|
|
config,
|
|
)
|
|
return test
|
|
},
|
|
}
|
|
}
|