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

Implementing proper cleanup of components when destroyed #443

Open
jahaganiev opened this issue Aug 13, 2024 Discussed in #438 · 0 comments
Open

Implementing proper cleanup of components when destroyed #443

jahaganiev opened this issue Aug 13, 2024 Discussed in #438 · 0 comments

Comments

@jahaganiev
Copy link
Member

Discussed in #438

Originally posted by langscot August 12, 2024
A lot of JavaScript frameworks (React, Vue, SvelteKit, etc.) rely on the handy HSStaticMethods.autoInit()method. This is often ran whenever the DOM conditionally re-renders (page change, user logged in, tab navigation, etc.). Whilst this is a good fix, it results in the respective collections array for each plugin growing in size for the duration of the session (without page refresh).

To get around this, we can look to the HSStaticMethods.cleanCollection() method, which flushes out the instances from each collection array. So, we could call this clean method before we call the auto intialize method. However, the clean method does not "cleanup" the event listeners or other elements added by the components init() methods. This results in things breaking. An example is an overlay toggle, which may have previously "destroyed" overlay instances' onClick event listeners attached, resulting in multiple backdrops being shown.

See my fork, where I have added an abstract method destroy() on the HSBasePlugin class, and have made the HSStaticMethods.cleanCollection() method call this destroy() for each instance of HSBasePlugin in the respective collection arrays.

I have only really implemented a proper destroy() method for the HSOverlay class, which removes all event listeners from any DOM elements. This has fixed my issue where multiple backdrops would appear when clicking an open modal button.

Would the maintainers be open to implementing such a mechanism for cleanup, if so, I would be happy to put in the effort to create an issue, discuss a proper design solution, and implement/contribute to a PR. If not, no worries, I will continue to use my fork.

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

No branches or pull requests

1 participant