Skip to content

Commit

Permalink
fix: Service consistency typing (#3309)
Browse files Browse the repository at this point in the history
* Use more consistent type/structure for services

* Fix restoring SR so the tests described work

* One more typed service

* Added types for the Cornerstone library

* Couple more type fixes
  • Loading branch information
wayfarer3130 authored Apr 18, 2023
1 parent 47c25f4 commit 6cf271d
Show file tree
Hide file tree
Showing 24 changed files with 309 additions and 299 deletions.
11 changes: 0 additions & 11 deletions extensions/cornerstone-dicom-seg/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ const extension = {
*/
id,

/**
* Perform any pre-registration tasks here. This is called before the extension
* is registered. Usually we run tasks such as: configuring the libraries
* (e.g. cornerstone, cornerstoneTools, ...) or registering any services that
* this extension is providing.
*/
preRegistration: ({
servicesManager,
commandsManager,
configuration = {},
}) => {},
/**
* PanelModule should provide a list of panels that will be available in OHIF
* for Modes to consume and render. Each panel is defined by a {name,
Expand Down
4 changes: 2 additions & 2 deletions extensions/cornerstone-dicom-sr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"test:unit:ci": "jest --ci --runInBand --collectCoverage --passWithNoTests"
},
"peerDependencies": {
"@ohif/core": "^3.0.0",
"@ohif/extension-cornerstone": "^3.0.0",
"@ohif/core": ">=3.0.0",
"@ohif/extension-cornerstone": ">=3.0.0",
"@ohif/extension-measurement-tracking": "^3.0.0",
"@ohif/ui": "^2.0.0",
"dcmjs": "^0.29.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import PropTypes from 'prop-types';
import React, { useCallback, useContext, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import OHIF, { utils, ServicesManager, ExtensionManager } from '@ohif/core';
import { CornerstoneServices } from '@ohif/extension-cornerstone/types';

import { setTrackingUniqueIdentifiersForElement } from '../tools/modules/dicomSRModule';

import {
Expand Down Expand Up @@ -37,7 +39,7 @@ function OHIFCornerstoneSRViewport(props) {
displaySetService,
cornerstoneViewportService,
measurementService,
} = servicesManager.services;
} = servicesManager.services as CornerstoneServices;

// SR viewport will always have a single display set
if (displaySets.length > 1) {
Expand Down Expand Up @@ -91,10 +93,12 @@ function OHIFCornerstoneSRViewport(props) {
SeriesInstanceUIDs[0]
);
if (displaySets.length) {
viewportGridService.setDisplaySetsForViewport({
viewportIndex: activeViewportIndex,
displaySetInstanceUIDs: [displaySets[0].displaySetInstanceUID],
});
viewportGridService.setDisplaySetsForViewports([
{
viewportIndex: activeViewportIndex,
displaySetInstanceUIDs: [displaySets[0].displaySetInstanceUID],
},
]);
}
};
}
Expand Down Expand Up @@ -422,6 +426,7 @@ OHIFCornerstoneSRViewport.propTypes = {
children: PropTypes.node,
customProps: PropTypes.object,
viewportOptions: PropTypes.object,
viewportLabel: PropTypes.string,
servicesManager: PropTypes.instanceOf(ServicesManager).isRequired,
extensionManager: PropTypes.instanceOf(ExtensionManager).isRequired,
};
Expand Down
5 changes: 5 additions & 0 deletions extensions/cornerstone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"repository": "OHIF/Viewers",
"main": "dist/index.umd.js",
"module": "src/index.tsx",
"types": "src/types/index.ts",
"exports": {
".": "./src/index.tsx",
"./types": "./src/types/index.ts"
},
"engines": {
"node": ">=10",
"npm": ">=6",
Expand Down
13 changes: 7 additions & 6 deletions extensions/cornerstone/src/Viewport/OHIFCornerstoneViewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import {
utilities as csUtils,
CONSTANTS,
} from '@cornerstonejs/core';
import { CinePlayer, useCine, useViewportGrid } from '@ohif/ui';
import {
IStackViewport,
IVolumeViewport,
} from '@cornerstonejs/core/dist/esm/types';

import { setEnabledElement } from '../state';

import './OHIFCornerstoneViewport.css';
import CornerstoneOverlays from './Overlays/CornerstoneOverlays';
import {
IStackViewport,
IVolumeViewport,
} from '@cornerstonejs/core/dist/esm/types';
import getSOPInstanceAttributes from '../utils/measurementServiceMappings/utils/getSOPInstanceAttributes';
import { CinePlayer, useCine, useViewportGrid } from '@ohif/ui';
import CornerstoneServices from '../types/CornerstoneServices';

const STACK = 'stack';

Expand Down Expand Up @@ -129,7 +130,7 @@ const OHIFCornerstoneViewport = React.memo(props => {
cornerstoneCacheService,
viewportGridService,
stateSyncService,
} = servicesManager.services;
} = servicesManager.services as CornerstoneServices;

const cineHandler = () => {
if (!cines || !cines[viewportIndex] || !enabledVPElement) {
Expand Down
24 changes: 9 additions & 15 deletions extensions/cornerstone/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
imageRetrievalPoolManager,
} from '@cornerstonejs/core';
import { Enums as cs3DToolsEnums } from '@cornerstonejs/tools';
import { Types } from '@ohif/core';
import { ServicesManager, Types } from '@ohif/core';

import init from './init';
import getCommandsModule from './commandsModule';
Expand All @@ -16,10 +16,11 @@ import ToolGroupService from './services/ToolGroupService';
import SyncGroupService from './services/SyncGroupService';
import SegmentationService from './services/SegmentationService';
import CornerstoneCacheService from './services/CornerstoneCacheService';
import CornerstoneViewportService from './services/ViewportService/CornerstoneViewportService';
import * as CornerstoneExtensionTypes from './types';

import { toolNames } from './initCornerstoneTools';
import { getEnabledElement, reset as enabledElementReset } from './state';
import CornerstoneViewportService from './services/ViewportService/CornerstoneViewportService';
import dicomLoaderService from './utils/dicomLoaderService';
import { registerColormap } from './utils/colormap/transferFunctionHelpers';

Expand Down Expand Up @@ -72,18 +73,11 @@ const cornerstoneExtension: Types.Extensions.Extension = {
props: Types.Extensions.ExtensionParams
): Promise<void> {
const { servicesManager } = props;
// Todo: we should be consistent with how services get registered. Use REGISTRATION static method for all
servicesManager.registerService(
CornerstoneViewportService(servicesManager)
);
servicesManager.registerService(
ToolGroupService.REGISTRATION(servicesManager)
);
servicesManager.registerService(SyncGroupService(servicesManager));
servicesManager.registerService(SegmentationService(servicesManager));
servicesManager.registerService(
CornerstoneCacheService.REGISTRATION(servicesManager)
);
servicesManager.registerService(CornerstoneViewportService.REGISTRATION);
servicesManager.registerService(ToolGroupService.REGISTRATION);
servicesManager.registerService(SyncGroupService.REGISTRATION);
servicesManager.registerService(SegmentationService.REGISTRATION);
servicesManager.registerService(CornerstoneCacheService.REGISTRATION);

return init.call(this, props);
},
Expand Down Expand Up @@ -145,5 +139,5 @@ const cornerstoneExtension: Types.Extensions.Extension = {
};

export type { PublicViewportOptions };
export { measurementMappingUtils };
export { measurementMappingUtils, CornerstoneExtensionTypes };
export default cornerstoneExtension;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ServicesManager } from '@ohif/core';
import { ServicesManager, Types } from '@ohif/core';
import { cache as cs3DCache, Enums, volumeLoader } from '@cornerstonejs/core';

import getCornerstoneViewportType from '../../utils/getCornerstoneViewportType';
Expand All @@ -10,20 +10,21 @@ import {
const VOLUME_LOADER_SCHEME = 'cornerstoneStreamingImageVolume';

class CornerstoneCacheService {
static REGISTRATION = (serviceManager: ServicesManager) => {
return {
name: 'cornerstoneCacheService',
altName: 'CornerstoneCacheService',
create: ({ configuration = {} }) => {
return new CornerstoneCacheService(serviceManager);
},
};
static REGISTRATION = {
name: 'cornerstoneCacheService',
altName: 'CornerstoneCacheService',
create: ({
servicesManager,
}: Types.Extensions.ExtensionParams): CornerstoneCacheService => {
return new CornerstoneCacheService(servicesManager);
},
};

stackImageIds: Map<string, string[]> = new Map();
volumeImageIds: Map<string, string[]> = new Map();
readonly servicesManager: ServicesManager;

constructor(servicesManager) {
constructor(servicesManager: ServicesManager) {
this.servicesManager = servicesManager;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cloneDeep from 'lodash.clonedeep';

import { Types as OhifTypes, ServicesManager, PubSubService } from '@ohif/core';
import {
cache,
eventTarget,
Expand All @@ -16,7 +17,6 @@ import {
Types as cstTypes,
utilities as cstUtils,
} from '@cornerstonejs/tools';
import { pubSubServiceInterface } from '@ohif/core';
import isEqual from 'lodash.isequal';
import { easeInOutBell } from '../../utils/transitions';
import {
Expand Down Expand Up @@ -47,19 +47,26 @@ const EVENTS = {

const VALUE_TYPES = {};

class SegmentationService {
listeners = {};
class SegmentationService extends PubSubService {
static REGISTRATION = {
name: 'segmentationService',
altName: 'SegmentationService',
create: ({
servicesManager,
}: OhifTypes.Extensions.ExtensionParams): SegmentationService => {
return new SegmentationService({ servicesManager });
},
};

segmentations: Record<string, Segmentation>;
servicesManager = null;
readonly servicesManager: ServicesManager;
highlightIntervalId = null;
_broadcastEvent: (eventName: string, callbackProps: any) => void;
readonly EVENTS = EVENTS;

constructor({ servicesManager }) {
super(EVENTS);
this.segmentations = {};
this.listeners = {};

Object.assign(this, pubSubServiceInterface);
this.servicesManager = servicesManager;

this._initSegmentationService();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import SegmentationService from './SegmentationService';

export default function ExtendedSegmentationService(servicesManager) {
return {
name: 'segmentationService',
altName: 'SegmentationService',
create: ({ configuration = {} }) => {
return new SegmentationService({ servicesManager });
},
};
}
export default SegmentationService;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Synchronizer,
} from '@cornerstonejs/tools';

import { pubSubServiceInterface } from '@ohif/core';
import { pubSubServiceInterface, Types, ServicesManager } from '@ohif/core';

const EVENTS = {
TOOL_GROUP_CREATED: 'event::cornerstone::syncgroupservice:toolgroupcreated',
Expand Down Expand Up @@ -37,7 +37,17 @@ const asSyncGroup = (syncGroup: string | SyncGroup): SyncGroup =>
typeof syncGroup === 'string' ? { type: syncGroup } : syncGroup;

export default class SyncGroupService {
serviceManager: any;
static REGISTRATION = {
name: 'syncGroupService',
altName: 'SyncGroupService',
create: ({
servicesManager,
}: Types.Extensions.ExtensionParams): SyncGroupService => {
return new SyncGroupService(servicesManager);
},
};

servicesManager: ServicesManager;
listeners: { [key: string]: (...args: any[]) => void } = {};
EVENTS: { [key: string]: string };
synchronizerCreators: Record<string, SyncCreator> = {
Expand All @@ -47,8 +57,8 @@ export default class SyncGroupService {
[STACKIMAGE]: synchronizers.createStackImageSynchronizer,
};

constructor(serviceManager) {
this.serviceManager = serviceManager;
constructor(serviceManager: ServicesManager) {
this.servicesManager = serviceManager;
this.listeners = {};
this.EVENTS = EVENTS;
//
Expand Down
10 changes: 1 addition & 9 deletions extensions/cornerstone/src/services/SyncGroupService/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import SyncGroupService from './SyncGroupService';

export default function ExtendedSyncGroupService(serviceManager) {
return {
altName: 'SyncGroupService',
name: 'syncGroupService',
create: ({ configuration = {} }) => {
return new SyncGroupService(serviceManager);
},
};
}
export default SyncGroupService;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ToolGroupManager, Enums, Types } from '@cornerstonejs/tools';

import { pubSubServiceInterface } from '@ohif/core';
import { Types as OhifTypes, pubSubServiceInterface } from '@ohif/core';

const EVENTS = {
VIEWPORT_ADDED: 'event::cornerstone::toolgroupservice:viewportadded',
Expand All @@ -20,14 +20,14 @@ type Tools = {
};

export default class ToolGroupService {
public static REGISTRATION = serviceManager => {
return {
name: 'toolGroupService',
altName: 'ToolGroupService',
create: ({ configuration = {} }) => {
return new ToolGroupService(serviceManager);
},
};
public static REGISTRATION = {
name: 'toolGroupService',
altName: 'ToolGroupService',
create: ({
servicesManager,
}: OhifTypes.Extensions.ExtensionParams): ToolGroupService => {
return new ToolGroupService(servicesManager);
},
};

serviceManager: any;
Expand Down
Loading

0 comments on commit 6cf271d

Please sign in to comment.