Skip to content

Commit

Permalink
feat(imageIdSpecificStateManager): add replace imageIdToolState fun…
Browse files Browse the repository at this point in the history
…ctions

There is currently no function to consume the same output from getImageIdToolState, which is the
state of a specific tool for the given imageId. addElementToolState requires a single item input,
and without replace, requires both a clear step and an iteration over all items to add into the
tool's id-specific state.
  • Loading branch information
mix3d committed Dec 16, 2020
1 parent 8a261a2 commit 3650e05
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/stateManagement/imageIdSpecificStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ function newImageIdSpecificToolStateManager() {
return imageIdToolState[toolName];
}

function replaceElementToolState(element, toolName, data) {
const enabledElement = external.cornerstone.getEnabledElement(element);

if (!enabledElement.image) {
return;
}
replaceImageIdToolState(enabledElement.image.imageId, toolName, data);
}

function replaceImageIdToolState(imageId, toolName, data) {
const imageIdToolState = toolState[imageId];

// Replace the toolState
imageIdToolState[toolName] = data;
}

// Clears all tool data from this toolStateManager.
function clearElementToolState(element) {
const enabledElement = external.cornerstone.getEnabledElement(element);
Expand All @@ -115,9 +131,11 @@ function newImageIdSpecificToolStateManager() {
return {
get: getElementToolState,
add: addElementToolState,
replace: replaceElementToolState,
clear: clearElementToolState,
getImageIdToolState,
addImageIdToolState,
replaceImageIdToolState,
clearImageIdToolState,
saveImageIdToolState,
restoreImageIdToolState,
Expand Down
33 changes: 33 additions & 0 deletions src/stateManagement/imageIdSpecificStateManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,36 @@ describe('imageIdSpecificStateManager.add', () => {
});
});
});

describe('imageIdSpecificStateManager.replace', () => {
it('replaces data in the existing toolState', () => {
const stateManager = newImageIdSpecificToolStateManager();
const toolName = 'TestTool';
const imageId = 'abc123';
const testElement = {
image: {
imageId,
},
};

externalModules.cornerstone.getEnabledElement.mockImplementationOnce(
() => testElement
);

// Setup with some intial data
stateManager.restoreImageIdToolState(imageId, {
[toolName]: { data: ['initialData'] },
});
// Add more data
stateManager.replace(testElement, toolName, {
data: ['replacedData1', 'replacedData2'],
});

// Check the results
const allToolState = stateManager.saveToolState();

expect(allToolState[imageId][toolName].data).toEqual(
expect.arrayContaining(['replacedData1', 'replacedData2'])
);
});
});

0 comments on commit 3650e05

Please sign in to comment.