-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web: types for plugins loader, removed circular dependency (#841)
- Loading branch information
Showing
3 changed files
with
45 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] || [] | ||
) | ||
); | ||
} |