Skip to content

Commit

Permalink
feat: Composite plugin loading
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrunyon committed Oct 19, 2023
1 parent 94cc82c commit d3b2860
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
14 changes: 13 additions & 1 deletion packages/app-utils/src/components/PluginsBootstrap.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { type Plugin } from '@deephaven/plugin';
import React, { createContext, useEffect, useState } from 'react';
import Log from '@deephaven/log';
import { PluginModuleMap, loadModulePlugins } from '../plugins';

const log = Log.module('PluginsBootstrap');

export const PluginsContext = createContext<PluginModuleMap | null>(null);

export type PluginsBootstrapProps = {
Expand Down Expand Up @@ -39,7 +42,16 @@ export function PluginsBootstrap({
const corePluginPairs = corePlugins.map(
plugin => [plugin.name, plugin] as const
);
setPlugins(new Map([...corePluginPairs, ...pluginModules]));
const newPlugins: PluginModuleMap = new Map();
[...corePluginPairs, ...pluginModules].forEach(([name, plugin]) => {
if (newPlugins.has(name)) {
log.warn(
`Plugin with name '${name}' already exists. Overriding existing with ${plugin}`
);
}
newPlugins.set(name, plugin);
});
setPlugins(newPlugins);
}
}
loadPlugins();
Expand Down
22 changes: 15 additions & 7 deletions packages/app-utils/src/plugins/PluginUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function loadJson(jsonUrl: string): Promise<PluginManifest> {
*/
export async function loadModulePlugins(
modulePluginsUrl: string
): Promise<PluginModuleMap> {
): Promise<[string, PluginModule][]> {
log.debug('Loading plugins...');
try {
const manifest = await loadJson(`${modulePluginsUrl}/manifest.json`);
Expand All @@ -74,7 +74,9 @@ export async function loadModulePlugins(
}

log.debug('Plugin manifest loaded:', manifest);
const pluginPromises: Promise<LegacyPlugin | { default: Plugin }>[] = [];
const pluginPromises: Promise<
LegacyPlugin | { default: Plugin | Plugin[] }
>[] = [];
for (let i = 0; i < manifest.plugins.length; i += 1) {
const { name, main } = manifest.plugins[i];
const pluginMainUrl = `${modulePluginsUrl}/${name}/${main}`;
Expand All @@ -83,7 +85,7 @@ export async function loadModulePlugins(

const pluginModules = await Promise.allSettled(pluginPromises);

const pluginMap: PluginModuleMap = new Map();
const plugins: [string, PluginModule][] = [];
for (let i = 0; i < pluginModules.length; i += 1) {
const module = pluginModules[i];
const { name } = manifest.plugins[i];
Expand All @@ -98,19 +100,25 @@ export async function loadModulePlugins(

if (moduleValue == null) {
log.error(`Plugin '${name}' is missing an exported value.`);
} else if (Array.isArray(moduleValue)) {
moduleValue.forEach(plugin => {
plugins.push([plugin.name, plugin]);
});
} else if (isLegacyPlugin(moduleValue)) {
plugins.push([name, moduleValue]);
} else {
pluginMap.set(name, moduleValue);
plugins.push([moduleValue.name, moduleValue]);
}
} else {
log.error(`Unable to load plugin '${name}'`, module.reason);
}
}
log.info('Plugins loaded:', pluginMap);
log.info('Plugins loaded:', plugins);

return pluginMap;
return plugins;
} catch (e) {
log.error('Unable to load plugins:', e);
return new Map();
return [];
}
}

Expand Down

0 comments on commit d3b2860

Please sign in to comment.