Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | 1x 1x | import { eventTarget } from '@cornerstonejs/core';
import { Events, SegmentationRepresentations } from '../../../enums';
import addRepresentationData from '../addRepresentationData';
import { triggerSegmentationModified } from '../triggerSegmentationEvents';
import { debounce } from '../../../utilities';
import { registerPolySegWorker } from './registerPolySegWorker';
const computedRepresentations = new Map<
string,
SegmentationRepresentations[]
>();
/**
* Computes a representation using the provided computation function, adds the computed data,
* subscribes to segmentation changes, and triggers segmentation modification.
*
* @param segmentationId - The ID of the segmentation.
* @param representationType - The type of the segmentation representation.
* @param computeFunction - The function that computes the representation data.
* @param options - Additional options for computing the representation.
* @returns - A promise that resolves with the computed representation data.
*/
async function computeAndAddRepresentation<T>(
segmentationId: string,
representationType: SegmentationRepresentations,
computeFunction: () => Promise<T>,
updateFunction?: () => void
): Promise<T> {
// register the worker if it hasn't been registered yet
registerPolySegWorker();
// Compute the specific representation data
const data = await computeFunction();
// Add the computed data to the system
addRepresentationData({
segmentationId,
type: representationType,
data,
});
// Update internal structures and possibly UI components
if (!computedRepresentations.has(segmentationId)) {
computedRepresentations.set(segmentationId, []);
}
const representations = computedRepresentations.get(segmentationId);
if (!representations.includes(representationType)) {
representations.push(representationType);
}
// Subscribe to any changes in the segmentation data for real-time updates
subscribeToSegmentationChanges(updateFunction);
// Notify other system parts that segmentation data has been modified
triggerSegmentationModified(segmentationId);
return data;
}
/**
* Subscribes to segmentation changes by adding an event listener for the SEGMENTATION_DATA_MODIFIED event.
* If there is an existing listener, it will be unsubscribed before adding the new listener.
*/
function subscribeToSegmentationChanges(updateFunction) {
const debouncedUpdateFunction = (event) => {
_debouncedSegmentationModified(event, updateFunction);
};
updateFunction._debouncedUpdateFunction = debouncedUpdateFunction;
eventTarget.removeEventListener(
Events.SEGMENTATION_DATA_MODIFIED,
updateFunction._debouncedUpdateFunction
);
eventTarget.addEventListener(
Events.SEGMENTATION_DATA_MODIFIED,
updateFunction._debouncedUpdateFunction
);
}
const _debouncedSegmentationModified = debounce((event, updateFunction) => {
const segmentationId = event.detail.segmentationId;
const representations = computedRepresentations.get(segmentationId);
if (!representations || !representations.length) {
return;
}
updateFunction(segmentationId);
if (representations.length) {
triggerSegmentationModified(segmentationId);
}
}, 300);
export { computeAndAddRepresentation };
|