Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Composite plugin loading #1587

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading