From d21af3f133492fa31492413b8782936c9ff18b44 Mon Sep 17 00:00:00 2001 From: Danny Brown Date: Thu, 9 Apr 2020 07:06:41 -0400 Subject: [PATCH] fix: Revert "refactor: Reduce bundle size (#1575)" (#1622) This reverts commit 81dcbe294daf1b2ecae1e30fec9bd7d530593b04. --- extensions/vtk/src/OHIFVTKViewport.js | 36 ++++----- extensions/vtk/src/VTKViewport.js | 11 +-- extensions/vtk/src/commandsModule.js | 27 ++++--- .../vtk/src/utils/getReactVtkjsViewport.js | 5 -- .../core/src/classes/StudyLoadingListener.js | 5 +- .../services/wado/studyInstanceHelpers.js | 3 +- .../src/utils/cornerstoneWADOImageLoader.js | 5 -- platform/core/src/utils/dicomLoaderService.js | 13 ++- platform/viewer/src/App.js | 6 +- platform/viewer/src/config.js | 30 +++++++ platform/viewer/src/lib/filesToStudies.js | 2 +- .../lib/localFileLoaders/dicomFileLoader.js | 5 +- .../lib/localFileLoaders/fileLoaderService.js | 5 +- .../src/lib/localFileLoaders/pdfFileLoader.js | 5 +- platform/viewer/src/routes/routesUtil.js | 23 ++---- .../src/utils/cornerstoneWADOImageLoader.js | 79 ------------------- platform/viewer/src/utils/index.js | 3 +- platform/viewer/src/utils/index.test.js | 2 +- platform/viewer/src/utils/initWebWorkers.js | 22 ++++++ .../viewer/src/utils/initWebWorkers.test.js | 23 ++++++ 20 files changed, 136 insertions(+), 174 deletions(-) delete mode 100644 extensions/vtk/src/utils/getReactVtkjsViewport.js delete mode 100644 platform/core/src/utils/cornerstoneWADOImageLoader.js delete mode 100644 platform/viewer/src/utils/cornerstoneWADOImageLoader.js create mode 100644 platform/viewer/src/utils/initWebWorkers.js create mode 100644 platform/viewer/src/utils/initWebWorkers.test.js diff --git a/extensions/vtk/src/OHIFVTKViewport.js b/extensions/vtk/src/OHIFVTKViewport.js index b538cf27579..69c89c1648e 100644 --- a/extensions/vtk/src/OHIFVTKViewport.js +++ b/extensions/vtk/src/OHIFVTKViewport.js @@ -1,5 +1,5 @@ import React, { Component } from 'react'; -import { getReactVtkjsViewport } from './utils/getReactVtkjsViewport'; +import { getImageData, loadImageData } from 'react-vtkjs-viewport'; import ConnectedVTKViewport from './ConnectedVTKViewport'; import LoadingIndicator from './LoadingIndicator.js'; import OHIF from '@ohif/core'; @@ -69,7 +69,7 @@ class OHIFVTKViewport extends Component { }; static defaultProps = { - onScroll: () => { }, + onScroll: () => {}, }; static id = 'OHIFVTKViewport'; @@ -133,8 +133,7 @@ class OHIFVTKViewport extends Component { displaySetInstanceUID, SOPClassUID, SOPInstanceUID, - frameIndex, - reactVtkjsViewport + frameIndex ) => { const stack = OHIFVTKViewport.getCornerstoneStack( studies, @@ -145,10 +144,7 @@ class OHIFVTKViewport extends Component { frameIndex ); - const imageDataObject = reactVtkjsViewport.getImageData( - stack.imageIds, - displaySetInstanceUID - ); + const imageDataObject = getImageData(stack.imageIds, displaySetInstanceUID); let labelmapDataObject; let labelmapColorLUT; @@ -261,9 +257,7 @@ class OHIFVTKViewport extends Component { return volumeActor; } - async setStateFromProps() { - const reactVtkjsViewport = await getReactVtkjsViewport(); - + setStateFromProps() { const { studies, displaySet } = this.props.viewportData; const { StudyInstanceUID, @@ -301,10 +295,8 @@ class OHIFVTKViewport extends Component { studies, StudyInstanceUID, displaySetInstanceUID, - sopClassUIDs[0], SOPInstanceUID, - frameIndex, - reactVtkjsViewport + frameIndex ); this.imageDataObject = imageDataObject; @@ -325,7 +317,7 @@ class OHIFVTKViewport extends Component { dataDetails, }, () => { - this.loadProgressively(imageDataObject, reactVtkjsViewport); + this.loadProgressively(imageDataObject); // TODO: There must be a better way to do this. // We do this so that if all the data is available the react-vtkjs-viewport @@ -343,26 +335,26 @@ class OHIFVTKViewport extends Component { ); } - async componentDidMount() { - await this.setStateFromProps(); + componentDidMount() { + this.setStateFromProps(); } - async componentDidUpdate(prevProps) { + componentDidUpdate(prevProps) { const { displaySet } = this.props.viewportData; const prevDisplaySet = prevProps.viewportData.displaySet; if ( displaySet.displaySetInstanceUID !== - prevDisplaySet.displaySetInstanceUID || + prevDisplaySet.displaySetInstanceUID || displaySet.SOPInstanceUID !== prevDisplaySet.SOPInstanceUID || displaySet.frameIndex !== prevDisplaySet.frameIndex ) { - await this.setStateFromProps(); + this.setStateFromProps(); } } - loadProgressively(imageDataObject, reactVtkjsViewport) { - reactVtkjsViewport.loadImageData(imageDataObject); + loadProgressively(imageDataObject) { + loadImageData(imageDataObject); const { isLoading, insertPixelDataPromises } = imageDataObject; diff --git a/extensions/vtk/src/VTKViewport.js b/extensions/vtk/src/VTKViewport.js index 274cc523907..26e1a6ed485 100644 --- a/extensions/vtk/src/VTKViewport.js +++ b/extensions/vtk/src/VTKViewport.js @@ -1,16 +1,9 @@ import React, { useEffect, useCallback } from 'react'; -// import { View2D } from 'react-vtkjs-viewport'; +import { View2D } from 'react-vtkjs-viewport'; import PropTypes from 'prop-types'; -import asyncComponent from './asyncComponent.js'; -import { getReactVtkjsViewport } from './utils/getReactVtkjsViewport'; import './VTKViewport.css'; -const View2D = asyncComponent(async () => { - const reactVtkjsViewport = await getReactVtkjsViewport(); - return { default: reactVtkjsViewport.View2D }; -}); - const VTKViewport = props => { const style = { width: '100%', height: '100%', position: 'relative' }; @@ -59,7 +52,7 @@ VTKViewport.propTypes = { }; VTKViewport.defaultProps = { - onScroll: () => { }, + onScroll: () => {}, }; export default VTKViewport; diff --git a/extensions/vtk/src/commandsModule.js b/extensions/vtk/src/commandsModule.js index ba873931911..7702b565634 100644 --- a/extensions/vtk/src/commandsModule.js +++ b/extensions/vtk/src/commandsModule.js @@ -1,5 +1,10 @@ import throttle from 'lodash.throttle'; -import { getReactVtkjsViewport } from './utils/getReactVtkjsViewport'; +import { + vtkInteractorStyleMPRCrosshairs, + vtkInteractorStyleMPRWindowLevel, + vtkInteractorStyleMPRRotate, + vtkSVGCrosshairsWidget, +} from 'react-vtkjs-viewport'; import setMPRLayout from './utils/setMPRLayout.js'; import setViewportToVTK from './utils/setViewportToVTK.js'; @@ -119,18 +124,16 @@ const commandsModule = ({ commandsManager }) => { _setView(api, [0, 1, 0], [0, 0, 1]); }, - enableRotateTool: async () => { - const reactVtkjsViewport = await getReactVtkjsViewport(); + enableRotateTool: () => { apis.forEach(api => { - const istyle = reactVtkjsViewport.vtkInteractorStyleMPRRotate.newInstance(); + const istyle = vtkInteractorStyleMPRRotate.newInstance(); api.setInteractorStyle({ istyle }); }); }, - enableCrosshairsTool: async () => { - const reactVtkjsViewport = await getReactVtkjsViewport(); + enableCrosshairsTool: () => { apis.forEach((api, apiIndex) => { - const istyle = reactVtkjsViewport.vtkInteractorStyleMPRCrosshairs.newInstance(); + const istyle = vtkInteractorStyleMPRCrosshairs.newInstance(); api.setInteractorStyle({ istyle, @@ -138,7 +141,7 @@ const commandsModule = ({ commandsManager }) => { }); }); }, - enableLevelTool: async () => { + enableLevelTool: () => { function updateVOI(apis, windowWidth, windowCenter) { apis.forEach(api => { api.updateVOI(windowWidth, windowCenter); @@ -159,9 +162,8 @@ const commandsModule = ({ commandsManager }) => { }, }; - const reactVtkjsViewport = await getReactVtkjsViewport(); apis.forEach(api => { - const istyle = reactVtkjsViewport.vtkInteractorStyleMPRWindowLevel.newInstance(); + const istyle = vtkInteractorStyleMPRWindowLevel.newInstance(); api.setInteractorStyle({ istyle, callbacks }); }); @@ -258,15 +260,14 @@ const commandsModule = ({ commandsManager }) => { } // Add widgets and set default interactorStyle of each viewport. - const reactVtkjsViewport = await getReactVtkjsViewport(); apis.forEach((api, apiIndex) => { api.addSVGWidget( - reactVtkjsViewport.vtkSVGCrosshairsWidget.newInstance(), + vtkSVGCrosshairsWidget.newInstance(), 'crosshairsWidget' ); const uid = api.uid; - const istyle = reactVtkjsViewport.vtkInteractorStyleMPRCrosshairs.newInstance(); + const istyle = vtkInteractorStyleMPRCrosshairs.newInstance(); api.setInteractorStyle({ istyle, diff --git a/extensions/vtk/src/utils/getReactVtkjsViewport.js b/extensions/vtk/src/utils/getReactVtkjsViewport.js deleted file mode 100644 index 611e9fffe17..00000000000 --- a/extensions/vtk/src/utils/getReactVtkjsViewport.js +++ /dev/null @@ -1,5 +0,0 @@ -export function getReactVtkjsViewport() { - return import( - /* webpackChunkName: "ReactVtkjsViewport" */ 'react-vtkjs-viewport' - ); -} diff --git a/platform/core/src/classes/StudyLoadingListener.js b/platform/core/src/classes/StudyLoadingListener.js index 10cafab5ad4..c655250bcde 100644 --- a/platform/core/src/classes/StudyLoadingListener.js +++ b/platform/core/src/classes/StudyLoadingListener.js @@ -1,10 +1,10 @@ import cornerstone from 'cornerstone-core'; +import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; import { clearStudyLoadingProgress, setStudyLoadingProgress, } from '../redux/actions'; import StackManager from '../utils/StackManager'; -import { getCornerstoneWADOImageLoader } from '../utils/cornerstoneWADOImageLoader'; class BaseLoadingListener { constructor(stack, options = {}) { @@ -103,8 +103,7 @@ class DICOMFileLoadingListener extends BaseLoadingListener { this._checkCachedData(); } - async _checkCachedData() { - const cornerstoneWADOImageLoader = await getCornerstoneWADOImageLoader(); + _checkCachedData() { const dataSet = cornerstoneWADOImageLoader.wadouri.dataSetCacheManager.get( this._dataSetUrl ); diff --git a/platform/core/src/studies/services/wado/studyInstanceHelpers.js b/platform/core/src/studies/services/wado/studyInstanceHelpers.js index 98eac1e10c7..0aa59369d9a 100644 --- a/platform/core/src/studies/services/wado/studyInstanceHelpers.js +++ b/platform/core/src/studies/services/wado/studyInstanceHelpers.js @@ -1,8 +1,8 @@ import DICOMWeb from '../../../DICOMWeb'; import metadataProvider from '../../../classes/MetadataProvider'; import getWADORSImageId from '../../../utils/getWADORSImageId'; +import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; import getReferencedSeriesSequence from './getReferencedSeriesSequence'; -import { getCornerstoneWADOImageLoader } from '../../../utils/cornerstoneWADOImageLoader'; /** * Create a plain JS object that describes a study (a study descriptor object) @@ -160,7 +160,6 @@ async function makeSOPInstance(server, study, instance) { const wadoRSMetadata = Object.assign(instance); - const cornerstoneWADOImageLoader = await getCornerstoneWADOImageLoader(); if (sopInstance.NumberOfFrames) { for (let i = 0; i < sopInstance.NumberOfFrames; i++) { const wadorsImageId = getWADORSImageId(sopInstance, i); diff --git a/platform/core/src/utils/cornerstoneWADOImageLoader.js b/platform/core/src/utils/cornerstoneWADOImageLoader.js deleted file mode 100644 index 64865712e96..00000000000 --- a/platform/core/src/utils/cornerstoneWADOImageLoader.js +++ /dev/null @@ -1,5 +0,0 @@ -export function getCornerstoneWADOImageLoader() { - return import( - /* webpackChunkName: "CornerstoneWADOImageLoader" */ 'cornerstone-wado-image-loader' - ); -} diff --git a/platform/core/src/utils/dicomLoaderService.js b/platform/core/src/utils/dicomLoaderService.js index a5bf5ad3c98..42d3faad71d 100644 --- a/platform/core/src/utils/dicomLoaderService.js +++ b/platform/core/src/utils/dicomLoaderService.js @@ -1,7 +1,7 @@ import cornerstone from 'cornerstone-core'; +import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; import { api } from 'dicomweb-client'; import DICOMWeb from '../DICOMWeb'; -import { getCornerstoneWADOImageLoader } from './cornerstoneWADOImageLoader'; const getImageId = imageObj => { if (!imageObj) { @@ -86,7 +86,7 @@ const getImageLoaderType = imageId => { }; class DicomLoaderService { - async getLocalData(dataset, studies) { + getLocalData(dataset, studies) { if (dataset && dataset.localFile) { // Use referenced imageInstance const imageInstance = getImageInstance(dataset); @@ -98,7 +98,6 @@ class DicomLoaderService { } if (!someInvalidStrings(imageId)) { - const cornerstoneWADOImageLoader = await getCornerstoneWADOImageLoader(); return cornerstoneWADOImageLoader.wadouri.loadFileRequest(imageId); } } @@ -177,16 +176,16 @@ class DicomLoaderService { } } - async *getLoaderIterator(dataset, studies) { - yield await this.getLocalData(dataset, studies); + *getLoaderIterator(dataset, studies) { + yield this.getLocalData(dataset, studies); yield this.getDataByImageType(dataset); yield this.getDataByDatasetType(dataset); } - async findDicomDataPromise(dataset, studies) { + findDicomDataPromise(dataset, studies) { const loaderIterator = this.getLoaderIterator(dataset, studies); // it returns first valid retriever method. - for await (const loader of loaderIterator) { + for (const loader of loaderIterator) { if (loader) { return loader; } diff --git a/platform/viewer/src/App.js b/platform/viewer/src/App.js index b88cabe0b70..6819838ff29 100644 --- a/platform/viewer/src/App.js +++ b/platform/viewer/src/App.js @@ -34,7 +34,10 @@ import i18n from '@ohif/i18n'; import './config'; /** Utils */ -import { getUserManagerForOpenIdConnectClient } from './utils/index.js'; +import { + getUserManagerForOpenIdConnectClient, + initWebWorkers, +} from './utils/index.js'; /** Extensions */ import { GenericViewerCommands, MeasurementsPanel } from './appExtensions'; @@ -137,6 +140,7 @@ class App extends Component { */ _initHotkeys(appConfigHotkeys); _initServers(servers); + initWebWorkers(); } render() { diff --git a/platform/viewer/src/config.js b/platform/viewer/src/config.js index 1945654abea..3b8a1f2e98f 100644 --- a/platform/viewer/src/config.js +++ b/platform/viewer/src/config.js @@ -1,4 +1,7 @@ +import OHIF from '@ohif/core'; import cornerstone from 'cornerstone-core'; +import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; +import dicomParser from 'dicom-parser'; import version from './version.js'; let homepage; @@ -12,4 +15,31 @@ window.info = { homepage, }; +// For debugging +//if (process.env.node_env === 'development') { window.cornerstone = cornerstone; +window.cornerstoneWADOImageLoader = cornerstoneWADOImageLoader; +//} + +cornerstoneWADOImageLoader.external.cornerstone = cornerstone; +cornerstoneWADOImageLoader.external.dicomParser = dicomParser; + +OHIF.user.getAccessToken = () => { + // TODO: Get the Redux store from somewhere else + const state = window.store.getState(); + if (!state.oidc || !state.oidc.user) { + return; + } + + return state.oidc.user.access_token; +}; + +cornerstoneWADOImageLoader.configure({ + beforeSend: function(xhr) { + const headers = OHIF.DICOMWeb.getAuthorizationHeader(); + + if (headers.Authorization) { + xhr.setRequestHeader('Authorization', headers.Authorization); + } + }, +}); diff --git a/platform/viewer/src/lib/filesToStudies.js b/platform/viewer/src/lib/filesToStudies.js index ed177c0602c..4f192b04bc7 100644 --- a/platform/viewer/src/lib/filesToStudies.js +++ b/platform/viewer/src/lib/filesToStudies.js @@ -3,7 +3,7 @@ import FileLoaderService from './localFileLoaders/fileLoaderService'; const processFile = async file => { try { const fileLoaderService = new FileLoaderService(file); - const imageId = await fileLoaderService.addFile(file); + const imageId = fileLoaderService.addFile(file); const image = await fileLoaderService.loadFile(file, imageId); const dataset = await fileLoaderService.getDataset(image, imageId); const studies = await fileLoaderService.getStudies(dataset, imageId); diff --git a/platform/viewer/src/lib/localFileLoaders/dicomFileLoader.js b/platform/viewer/src/lib/localFileLoaders/dicomFileLoader.js index bf24f152c14..71da0cbc8a7 100644 --- a/platform/viewer/src/lib/localFileLoaders/dicomFileLoader.js +++ b/platform/viewer/src/lib/localFileLoaders/dicomFileLoader.js @@ -1,14 +1,13 @@ import * as dcmjs from 'dcmjs'; +import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; import FileLoader from './fileLoader'; import OHIF from '@ohif/core'; -import { getCornerstoneWADOImageLoader } from '../../utils/cornerstoneWADOImageLoader'; const metadataProvider = OHIF.cornerstone.metadataProvider; const DICOMFileLoader = new (class extends FileLoader { fileType = 'application/dicom'; - async loadFile(file, imageId) { - const cornerstoneWADOImageLoader = await getCornerstoneWADOImageLoader(); + loadFile(file, imageId) { return cornerstoneWADOImageLoader.wadouri.loadFileRequest(imageId); } diff --git a/platform/viewer/src/lib/localFileLoaders/fileLoaderService.js b/platform/viewer/src/lib/localFileLoaders/fileLoaderService.js index 8d22be22cbc..9a2bbe534ae 100644 --- a/platform/viewer/src/lib/localFileLoaders/fileLoaderService.js +++ b/platform/viewer/src/lib/localFileLoaders/fileLoaderService.js @@ -1,7 +1,7 @@ +import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; import FileLoader from './fileLoader'; import PDFFileLoader from './pdfFileLoader'; import DICOMFileLoader from './dicomFileLoader'; -import { getCornerstoneWADOImageLoader } from '../../utils/cornerstoneWADOImageLoader'; class FileLoaderService extends FileLoader { fileType; @@ -53,8 +53,7 @@ class FileLoaderService extends FileLoader { return result; } - async addFile(file) { - const cornerstoneWADOImageLoader = await getCornerstoneWADOImageLoader(); + addFile(file) { return cornerstoneWADOImageLoader.wadouri.fileManager.add(file); } diff --git a/platform/viewer/src/lib/localFileLoaders/pdfFileLoader.js b/platform/viewer/src/lib/localFileLoaders/pdfFileLoader.js index 27ada81921a..bf2ae6ec2a2 100644 --- a/platform/viewer/src/lib/localFileLoaders/pdfFileLoader.js +++ b/platform/viewer/src/lib/localFileLoaders/pdfFileLoader.js @@ -1,10 +1,9 @@ +import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; import FileLoader from './fileLoader'; -import { getCornerstoneWADOImageLoader } from '../../utils/cornerstoneWADOImageLoader'; const PDFFileLoader = new (class extends FileLoader { fileType = 'application/pdf'; - async loadFile(file, imageId) { - const cornerstoneWADOImageLoader = await getCornerstoneWADOImageLoader(); + loadFile(file, imageId) { return cornerstoneWADOImageLoader.wadouri.loadFileRequest(imageId); } diff --git a/platform/viewer/src/routes/routesUtil.js b/platform/viewer/src/routes/routesUtil.js index 6f5a11cdb2d..f98c0659453 100644 --- a/platform/viewer/src/routes/routesUtil.js +++ b/platform/viewer/src/routes/routesUtil.js @@ -1,5 +1,4 @@ import asyncComponent from '../components/AsyncComponent.js'; -import { withCornerstone } from '../utils/cornerstoneWADOImageLoader'; import OHIF from '@ohif/core'; const { urlUtil: UrlUtil } = OHIF.utils; @@ -10,10 +9,8 @@ const IHEInvokeImageDisplay = asyncComponent(() => /* webpackChunkName: "IHEInvokeImageDisplay" */ './IHEInvokeImageDisplay.js' ) ); -const ViewerRouting = withCornerstone( - asyncComponent(() => - import(/* webpackChunkName: "ViewerRouting" */ './ViewerRouting.js') - ) +const ViewerRouting = asyncComponent(() => + import(/* webpackChunkName: "ViewerRouting" */ './ViewerRouting.js') ); const StudyListRouting = asyncComponent(() => @@ -21,18 +18,12 @@ const StudyListRouting = asyncComponent(() => /* webpackChunkName: "StudyListRouting" */ '../studylist/StudyListRouting.js' ) ); -const StandaloneRouting = withCornerstone( - asyncComponent(() => - import( - /* webpackChunkName: "ConnectedStandaloneRouting" */ '../connectedComponents/ConnectedStandaloneRouting.js' - ) - ) +const StandaloneRouting = asyncComponent(() => + import(/* webpackChunkName: "ConnectedStandaloneRouting" */ '../connectedComponents/ConnectedStandaloneRouting.js') ); -const ViewerLocalFileData = withCornerstone( - asyncComponent(() => - import( - /* webpackChunkName: "ViewerLocalFileData" */ '../connectedComponents/ViewerLocalFileData.js' - ) +const ViewerLocalFileData = asyncComponent(() => + import( + /* webpackChunkName: "ViewerLocalFileData" */ '../connectedComponents/ViewerLocalFileData.js' ) ); diff --git a/platform/viewer/src/utils/cornerstoneWADOImageLoader.js b/platform/viewer/src/utils/cornerstoneWADOImageLoader.js deleted file mode 100644 index 0d76cfb2280..00000000000 --- a/platform/viewer/src/utils/cornerstoneWADOImageLoader.js +++ /dev/null @@ -1,79 +0,0 @@ -import OHIF from '@ohif/core'; -import cornerstone from 'cornerstone-core'; -import dicomParser from 'dicom-parser'; -import React from 'react'; - -export function getCornerstoneWADOImageLoader() { - return import( - /* webpackChunkName: "CornerstoneWADOImageLoader" */ 'cornerstone-wado-image-loader' - ); -} - -let initialized = false; -export async function initCornerstoneWADOImageLoader() { - if (initialized) return; - const cornerstoneWADOImageLoader = await getCornerstoneWADOImageLoader(); - // For debugging - //if (process.env.node_env === 'development') { - window.cornerstoneWADOImageLoader = cornerstoneWADOImageLoader; - //} - - cornerstoneWADOImageLoader.external.cornerstone = cornerstone; - cornerstoneWADOImageLoader.external.dicomParser = dicomParser; - - OHIF.user.getAccessToken = () => { - // TODO: Get the Redux store from somewhere else - const state = window.store.getState(); - if (!state.oidc || !state.oidc.user) { - return; - } - - return state.oidc.user.accesstoken; - }; - - cornerstoneWADOImageLoader.configure({ - beforeSend: function (xhr) { - const headers = OHIF.DICOMWeb.getAuthorizationHeader(); - - if (headers.Authorization) { - xhr.setRequestHeader('Authorization', headers.Authorization); - } - }, - }); - - const config = { - maxWebWorkers: Math.max(navigator.hardwareConcurrency - 1, 1), - startWebWorkersOnDemand: true, - taskConfiguration: { - decodeTask: { - initializeCodecsOnStartup: false, - usePDFJS: false, - strict: false, - }, - }, - }; - cornerstoneWADOImageLoader.webWorkerManager.initialize(config); - - initialized = true; -} - -export function withCornerstone(Component) { - // eslint-disable-next-line react/display-name - return props => { - const [ - cornerstoneWADOImageLoaderInitialized, - setCornerstoneWADOImageLoaderInitialized, - ] = React.useState(false); - - React.useEffect(() => { - initCornerstoneWADOImageLoader().then(() => - setCornerstoneWADOImageLoaderInitialized(true) - ); - }, []); - - if (!cornerstoneWADOImageLoaderInitialized) { - return null; - } - return ; - }; -} diff --git a/platform/viewer/src/utils/index.js b/platform/viewer/src/utils/index.js index fe4dd7c32fb..efcc1628c34 100644 --- a/platform/viewer/src/utils/index.js +++ b/platform/viewer/src/utils/index.js @@ -1,3 +1,4 @@ import getUserManagerForOpenIdConnectClient from './getUserManagerForOpenIdConnectClient.js'; +import initWebWorkers from './initWebWorkers.js'; -export { getUserManagerForOpenIdConnectClient }; +export { getUserManagerForOpenIdConnectClient, initWebWorkers }; diff --git a/platform/viewer/src/utils/index.test.js b/platform/viewer/src/utils/index.test.js index 748b04eb8cf..03f96fe6489 100644 --- a/platform/viewer/src/utils/index.test.js +++ b/platform/viewer/src/utils/index.test.js @@ -5,7 +5,7 @@ describe('utils', () => { const utilExports = Object.keys(utils).sort(); expect(utilExports).toEqual( - ['getUserManagerForOpenIdConnectClient'].sort() + ['getUserManagerForOpenIdConnectClient', 'initWebWorkers'].sort() ); }); }); diff --git a/platform/viewer/src/utils/initWebWorkers.js b/platform/viewer/src/utils/initWebWorkers.js new file mode 100644 index 00000000000..8bbfa28d974 --- /dev/null +++ b/platform/viewer/src/utils/initWebWorkers.js @@ -0,0 +1,22 @@ +import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; + +let initialized = false; + +export default function initWebWorkers() { + const config = { + maxWebWorkers: Math.max(navigator.hardwareConcurrency - 1, 1), + startWebWorkersOnDemand: true, + taskConfiguration: { + decodeTask: { + initializeCodecsOnStartup: false, + usePDFJS: false, + strict: false, + }, + }, + }; + + if (!initialized) { + cornerstoneWADOImageLoader.webWorkerManager.initialize(config); + initialized = true; + } +} diff --git a/platform/viewer/src/utils/initWebWorkers.test.js b/platform/viewer/src/utils/initWebWorkers.test.js new file mode 100644 index 00000000000..cbd554a790d --- /dev/null +++ b/platform/viewer/src/utils/initWebWorkers.test.js @@ -0,0 +1,23 @@ +import initWebWorkers from './initWebWorkers.js'; +import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader'; + +describe('initWebWorkers', () => { + it("initializes cornerstoneWADOImageLoader's web workers", () => { + initWebWorkers(); + + expect( + cornerstoneWADOImageLoader.webWorkerManager.initialize + ).toHaveBeenCalled(); + }); +}); + +describe('initWebWorkers', () => { + it("initializes cornerstoneWADOImageLoader's web workers only once", () => { + initWebWorkers(); + initWebWorkers(); + + expect( + cornerstoneWADOImageLoader.webWorkerManager.initialize + ).toHaveBeenCalledTimes(1); + }); +});