Skip to content

fix: updateImage and draw should not throw#482

Merged
swederik merged 2 commits intocornerstonejs:masterfrom
mikehazell:pr/updateImage-should-not-throw
Sep 13, 2021
Merged

fix: updateImage and draw should not throw#482
swederik merged 2 commits intocornerstonejs:masterfrom
mikehazell:pr/updateImage-should-not-throw

Conversation

@mikehazell
Copy link
Copy Markdown
Contributor

@mikehazell mikehazell commented Sep 2, 2020

Allow calling updateImage or draw whether an image has been loaded or not

Changes

This PR:

  • removes the code which checks for a loaded image from the updateImage and draw methods.
  • adds some tests to ensure drawImageSync is correctly verifying an elements state.
  • fixes the condition in drawImageSync because of an edge case condition where the element has a layer added without an image.

Some context

I'm not sure if this was the original intention of updateImage but it is often used to tell cornerstone that the canvas state is invalid and needs to be re-rendered.

There are several similar methods drawImage, draw, invalidate, invalidateImageId and drawInvalidated.

  • All these end up setting one or both the flags needsRedraw and invalid on one or more enabledElement's.
  • None of these methods ever actually use any of the properties of the image.
  • Only draw and updateImage currently have a check to see if the element has an image loaded.

This check is not needed

drawImageSync is the method which actually causes something to be rendered to the canvas and is therefore the method which would encounter an error if some required state were missing. The implementation of this method already contains checks to ensure the enabledElement has all the state required to render and will quietly return early if it does not.

So if these checks are working correctly, there is no need for draw or updateImage to also check this state.

Why is it worth changing this

The most obvious example of updateImage throwing causing pain is when a cornerstone tool is changing mode.

The tool may need to tell cornerstone that the state of the canvas needs to be updated. For example to stop showing a cursor, or to redraw without an annotation.

We can see a case of this causing a crashing bug here:
cornerstonejs/cornerstoneTools#1303

This has also been an issue with the BrushTool.

@mikehazell mikehazell force-pushed the pr/updateImage-should-not-throw branch 2 times, most recently from fbd7c1b to c8cbd26 Compare September 9, 2020 05:38
@mikehazell mikehazell force-pushed the pr/updateImage-should-not-throw branch from c8cbd26 to 9472274 Compare September 9, 2020 05:59
@mikehazell
Copy link
Copy Markdown
Contributor Author

NOTE: I've removed yarn.lock because it was breaking the travis check. Happy to revert that change if you'd prefer but we really should have only one of package-lock.json and yarn.lock.

@mikehazell mikehazell marked this pull request as ready for review September 9, 2020 06:04
@mikehazell mikehazell changed the title updateImage should not throw fix: updateImage should not throw Sep 9, 2020
@mikehazell mikehazell changed the title fix: updateImage should not throw fix: updateImage and draw should not throw Sep 9, 2020
@mikehazell
Copy link
Copy Markdown
Contributor Author

Hey @dannyrb, any thoughts on this?

Copy link
Copy Markdown

@zakjholt zakjholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it'll solve an issue I'm currently running into with the layers api

@swederik swederik merged commit 776ca92 into cornerstonejs:master Sep 13, 2021
@swederik
Copy link
Copy Markdown
Member

Thanks!

@swederik
Copy link
Copy Markdown
Member

🎉 This PR is included in version 2.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants