feat(App): Loads mode extensions on demand (#3312)

* feat: Dynamic plugin imports

* Remove unused file

* Fix basic dev mode

* Fix the modes issue that Joe requested

* Merge fixes

* Add the ability to publish directory contents

* PR review changes

---------
This commit is contained in:
Bill Wallace authored and GitHub committed 2023-05-08 21:18:35 -04:00
1 parent 2f355fa158
commit a688f03fdb
11 files changed
+225 -197

No files matched your search

+35 -15
View File
@@ -21,6 +21,8 @@ import {
// utils,
} from '@ohif/core';
import loadModules from './pluginImports';
/**
* @param {object|func} appConfigOrFunc - application configuration, or a function that returns application configuration
* @param {object[]} defaultExtensions - array of extension objects
@@ -74,8 +76,12 @@ async function appInit(appConfigOrFunc, defaultExtensions, defaultModes) {
* Example: [ext1, ext2, ext3]
* Example2: [[ext1, config], ext2, [ext3, config]]
*/
const loadedExtensions = await loadModules([
...defaultExtensions,
...appConfig.extensions,
]);
await extensionManager.registerExtensions(
[...defaultExtensions, ...appConfig.extensions],
loadedExtensions,
appConfig.dataSources
);
@@ -87,24 +93,38 @@ async function appInit(appConfigOrFunc, defaultExtensions, defaultModes) {
throw new Error('No modes are defined! Check your app-config.js');
}
for (let i = 0; i < defaultModes.length; i++) {
const { modeFactory, id } = defaultModes[i];
const loadedModes = await loadModules([
...(appConfig.modes || []),
...defaultModes,
]);
// If the appConfig contains configuration for this mode, use it.
const modeConfig =
appConfig.modeConfig && appConfig.modeConfig[i]
? appConfig.modeConfig[id]
: {};
// This is the name for the loaded istance object
appConfig.loadedModes = [];
const modesById = new Set();
for (let i = 0; i < loadedModes.length; i++) {
let mode = loadedModes[i];
if (!mode) continue;
const { id } = mode;
const mode = modeFactory(modeConfig);
if (mode.modeFactory) {
// If the appConfig contains configuration for this mode, use it.
const modeConfig =
appConfig.modeConfig && appConfig.modeConfig[i]
? appConfig.modeConfig[id]
: {};
appConfig.modes.push(mode);
mode = mode.modeFactory(modeConfig);
}
if (modesById.has(id)) continue;
// Prevent duplication
modesById.add(id);
if (!mode || typeof mode !== 'object') continue;
appConfig.loadedModes.push(mode);
}
// remove modes that are not objects, or have no id
appConfig.modes = appConfig.modes.filter(
mode => typeof mode === 'object' && mode.id
);
// Hack alert - don't touch the original modes definition,
// but there are still dependencies on having the appConfig modes defined
appConfig.modes = appConfig.loadedModes;
return {
appConfig,
+17 -18
View File
@@ -16,25 +16,24 @@ import { history } from './utils/history';
* pluginImports.js imports all of the modes and extensions and adds them
* to the window for processing.
*/
import loadDynamicImports, { loadRuntimeImports } from './pluginImports.js';
import {
modes as defaultModes,
extensions as defaultExtensions,
} from './pluginImports';
loadDynamicImports().then(() => {
loadRuntimeImports(window.config).then(() => {
/**
* Combine our appConfiguration with installed extensions and modes.
* In the future appConfiguration may contain modes added at runtime.
* */
const appProps = {
config: window ? window.config : {},
defaultExtensions: window.extensions,
defaultModes: window.modes,
};
/**
* Combine our appConfiguration with installed extensions and modes.
* In the future appConfiguration may contain modes added at runtime.
* */
const appProps = {
config: window ? window.config : {},
defaultExtensions,
defaultModes,
};
/** Create App */
const app = React.createElement(App, appProps, null);
/** Render */
ReactDOM.render(app, document.getElementById('root'));
});
});
/** Create App */
const app = React.createElement(App, appProps, null);
/** Render */
ReactDOM.render(app, document.getElementById('root'));
export { history };
+50 -36
View File
@@ -9,6 +9,7 @@ import ViewportGrid from '@components/ViewportGrid';
import Compose from './Compose';
import getStudies from './studiesList';
import { history } from '../../utils/history';
import loadModules from '../../pluginImports';
const { getSplitParam } = utils;
@@ -102,6 +103,8 @@ export default function ModeRoute({
const [studyInstanceUIDs, setStudyInstanceUIDs] = useState();
const [refresh, setRefresh] = useState(false);
const [allExtensionsLoaded, setAllExtensionsLoaded] = useState(false);
const layoutTemplateData = useRef(false);
const locationRef = useRef(null);
const isMounted = useRef(false);
@@ -235,41 +238,52 @@ export default function ModeRoute({
return;
}
// TODO: For some reason this is running before the Providers
// are calling setServiceImplementation
// TODO -> iterate through services.
// Extension
// Add SOPClassHandlers to a new SOPClassManager.
displaySetService.init(extensionManager, sopClassHandlers);
extensionManager.onModeEnter({
servicesManager,
extensionManager,
commandsManager,
});
// use the URL hangingProtocolId if it exists, otherwise use the one
// defined in the mode configuration
const hangingProtocolIdToUse = hangingProtocolService.getProtocolById(
runTimeHangingProtocolId
)
? runTimeHangingProtocolId
: hangingProtocol;
// Sets the active hanging protocols - if hangingProtocol is undefined,
// resets to default. Done before the onModeEnter to allow the onModeEnter
// to perform custom hanging protocol actions
hangingProtocolService.setActiveProtocolIds(hangingProtocolIdToUse);
mode?.onModeEnter({
servicesManager,
extensionManager,
commandsManager,
});
const setupRouteInit = async () => {
const loadedExtensions = await loadModules(Object.keys(extensions));
for (const extension of loadedExtensions) {
const { id: extensionId } = extension;
if (
extensionManager.registeredExtensionIds.indexOf(extensionId) === -1
) {
await extensionManager.registerExtension(extension);
}
}
setAllExtensionsLoaded(true);
// TODO: For some reason this is running before the Providers
// are calling setServiceImplementation
// TODO -> iterate through services.
// Extension
// Add SOPClassHandlers to a new SOPClassManager.
displaySetService.init(extensionManager, sopClassHandlers);
extensionManager.onModeEnter({
servicesManager,
extensionManager,
commandsManager,
});
// use the URL hangingProtocolId if it exists, otherwise use the one
// defined in the mode configuration
const hangingProtocolIdToUse = hangingProtocolService.getProtocolById(
runTimeHangingProtocolId
)
? runTimeHangingProtocolId
: hangingProtocol;
// Sets the active hanging protocols - if hangingProtocol is undefined,
// resets to default. Done before the onModeEnter to allow the onModeEnter
// to perform custom hanging protocol actions
hangingProtocolService.setActiveProtocolIds(hangingProtocolIdToUse);
mode?.onModeEnter({
servicesManager,
extensionManager,
commandsManager,
});
/**
* The next line should get all the query parameters provided by the URL
* - except the StudyInstanceUIDs - and create an object called filters
@@ -382,8 +396,8 @@ export default function ModeRoute({
<CombinedContextProvider>
<DragAndDropProvider>
{layoutTemplateData.current &&
studyInstanceUIDs?.length &&
studyInstanceUIDs[0] !== undefined &&
studyInstanceUIDs?.[0] !== undefined &&
allExtensionsLoaded &&
renderLayoutData({
...layoutTemplateData.current.props,
ViewportGridComp: ViewportGridWithDataSource,
@@ -336,7 +336,7 @@ function WorkList({
: []
}
>
{appConfig.modes.map((mode, i) => {
{appConfig.loadedModes.map((mode, i) => {
const isFirst = i === 0;
const isValidMode = mode.isValidMode({ modalities });
@@ -1,6 +1,5 @@
import React from 'react';
import ModeRoute from '@routes/Mode';
import checkExtensionDependencies from './checkExtensionDependencies';
/*
Routes uniquely define an entry point to:
@@ -41,8 +40,6 @@ export default function buildModeRoutes({
});
modes.forEach(mode => {
checkExtensionDependencies(mode, extensionManager);
// todo: for each route. add route to path.
dataSourceNames.forEach(dataSourceName => {
const path = `/${mode.routeName}/${dataSourceName}`;
@@ -1,39 +0,0 @@
export default function checkExtensionDependencies(mode, extensionManager) {
const extensionDependencies = mode.extensions;
const dependencyString = `Unmet extension dependency in mode: ${mode.id}`;
Object.keys(extensionDependencies).forEach(extensionId => {
const extensionInstalled = extensionManager.registeredExtensionIds.includes(
extensionId
);
if (!extensionInstalled) {
throw new Error(
`${dependencyString}: extension ${extensionId} not found`
);
}
});
}
function areVersionsCompatible(semanticVersion, installedVersion) {
if (semanticVersion.includes('^')) {
// Major must match
const versionLessCaret = semanticVersion.split('^')[1];
// Index 0 is the major version.
return versionLessCaret[0] === installedVersion[0];
} else if (semanticVersion.includes('~')) {
// Major and minor must match
const versionLessTilde = semanticVersion.split('~')[1];
// Index 0 is the major version.
// Index 2 is the minor version.
return (
versionLessTilde[0] === installedVersion[0] &&
versionLessTilde[2] === installedVersion[2]
);
} else {
return semanticVersion === installedVersion;
}
}