Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added destroy method for the PinchZoom instance #129

Merged

Conversation

oleksandr-danylchenko
Copy link
Contributor

@oleksandr-danylchenko oleksandr-danylchenko commented Jul 22, 2022

Previously when there were multiple invocations of the PinchZoom constructor, a new resize event listener was registered each time. It led to the improper resing of the images when the users rotated their devices.

RPReplay_Final1656005250.1.online-video-cutter.com.mp4

But now developers can explicitly destroy the PinchZoom instance on unmount which will prevent redundant stacking of the event handlers and memory leaks.

Fixes #128

# Removes the global `resize` listener and removes the zooming container
Copy link
Collaborator

@sandstrom sandstrom left a comment

Choose a reason for hiding this comment

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

Good idea!

How about gating the removal behind if-clauses, checking if the to-be-removed stuff exist?

For example, in case destroy() is called multiple times on the same instance.

src/pinch-zoom.js Show resolved Hide resolved
src/pinch-zoom.js Outdated Show resolved Hide resolved
* Unmounts the zooming container and global event listeners
*/
destroy: function () {
window.removeEventListener('resize', this.resizeHandler);
Copy link
Collaborator

@sandstrom sandstrom Jul 27, 2022

Choose a reason for hiding this comment

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

If the instance is destroyed while image loading is in flight (or possibly other async stuff), we may have callbacks triggered on the now-destroyed instance. Those would error out.

Not a blocker, but something to be aware of.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think there's any async handling on the pinch-zoom-js side that we need to care about 🤔. As soon as the PinchZoom instance is created, it synchronously mounts (this.setupMarkdown()) all the required dom elements with events listeners (this.bindEvents()).

Therefore, IMHO, it's the responsibility of the library users to destroy the PinchZoom instance at the right time. Because only they can know how the image is asynchronously loaded in different ways. E.g.: plain img tag with onload handler or some more complicated images library with a different loading handler.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good points, I agree!

@sandstrom sandstrom merged commit f611251 into manuelstofer:master Jul 28, 2022
@sandstrom
Copy link
Collaborator

Thanks @oleksandr-danylchenko!

@sandstrom sandstrom mentioned this pull request Jul 28, 2022
@oleksandr-danylchenko oleksandr-danylchenko deleted the zoomer-destructor branch July 28, 2022 12:11
@oleksandr-danylchenko
Copy link
Contributor Author

You're welcome 🙌

@oleksandr-danylchenko
Copy link
Contributor Author

@sandstrom, when the next lib version should be awaited?

@sandstrom
Copy link
Collaborator

@oleksandr-danylchenko I've released 2.3.5 now (https://github.com/manuelstofer/pinchzoom/releases/tag/v2.3.5).

@oleksandr-danylchenko
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing removal of the resize event listener on the window
3 participants