Skip to content

Commit

Permalink
Web: types for plugins loader, removed circular dependency (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
psrok1 committed Jun 23, 2023
1 parent 06dd1ea commit 775542a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion mwdb/web/src/commons/plugins/Extension.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fromPlugins } from ".";
import { fromPlugins } from "./loader";

type Props = {
[extensionProp: string]: any;
Expand Down
37 changes: 1 addition & 36 deletions mwdb/web/src/commons/plugins/index.jsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
import _ from "lodash";
import pluginLoaders from "@mwdb-web/plugins";

export { Extension } from "./Extension";
export { Extendable } from "./Extendable";

let loadedPlugins = {};
let pluginsLoadedCallbacks = [];

export async function loadPlugins() {
let plugins = {};
for (const [pluginName, pluginModulePromise] of Object.entries(
pluginLoaders
)) {
try {
// await import("@mwdb-core/plugin-xyz")
const pluginModule = (await pluginModulePromise).default;
plugins[pluginName] = pluginModule();
} catch (e) {
console.error(`Plugin ${pluginName} failed to load`, e);
}
}
// Hacky but I want to avoid top-level await
loadedPlugins = plugins;
pluginsLoadedCallbacks.map((callback) => callback());
}

export function afterPluginsLoaded(callback) {
pluginsLoadedCallbacks.push(callback);
}

export function fromPlugins(element) {
return _.flatten(
Object.keys(loadedPlugins).map(
(name) => loadedPlugins[name][element] || []
)
);
}
export { loadPlugins, afterPluginsLoaded, fromPlugins } from "./loader";
43 changes: 43 additions & 0 deletions mwdb/web/src/commons/plugins/loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import _ from "lodash";
// @ts-ignore
import pluginLoaders from "@mwdb-web/plugins";

type PluginSpec = { [element: string]: any[] };
type Plugins = { [pluginName: string]: PluginSpec };

type PluginLoader = { default: () => PluginSpec };
type PluginLoaderPromise = Promise<PluginLoader>;
type PluginLoaders = { [pluginName: string]: PluginLoaderPromise };

let loadedPlugins: Plugins = {};
let pluginsLoadedCallbacks: Array<() => void> = [];

export async function loadPlugins(): Promise<void> {
let plugins: Plugins = {};
for (const [pluginName, pluginModulePromise] of Object.entries(
pluginLoaders as PluginLoaders
)) {
try {
// await import("@mwdb-core/plugin-xyz")
const pluginModule = (await pluginModulePromise).default;
plugins[pluginName] = pluginModule();
} catch (e) {
console.error(`Plugin ${pluginName} failed to load`, e);
}
}
// Hacky but I want to avoid top-level await
loadedPlugins = plugins;
pluginsLoadedCallbacks.map((callback) => callback());
}

export function afterPluginsLoaded(callback: () => void): void {
pluginsLoadedCallbacks.push(callback);
}

export function fromPlugins(element: string): any[] {
return _.flatten(
Object.keys(loadedPlugins).map(
(name) => loadedPlugins[name][element] || []
)
);
}

0 comments on commit 775542a

Please sign in to comment.