Replies: 5 comments 10 replies
-
Do you know why cleanCollection(), i.e. resetting the array of the elements, is not enough to clean up old instances? Like, where is the reference that keeps an instance alive? I could not observe problems like you're describing, but as you've seen I have other issues with ajax + preline, and well, some memory leaks... |
Beta Was this translation helpful? Give feedback.
-
I think I understand the issue now. I don't like your approach with But in general there has to be a way to "destroy" preline instances ( What preline needs to properly work with ajax/partials is two things
This would be actually quite easy to implement, and I could provide a PR if someone more official gives the go-ahead. And in my case with HTMX, I would only need event listeners which call |
Beta Was this translation helpful? Give feedback.
-
A little bit spammy on my part, but I just had a go at implementing my approach Seems to work well as a proof of concept. As I described earlier I now just clean up and initialize when partial updates happen document.addEventListener("htmx:afterSwap", function (event) {
// event.detail.target is the node that was swapped out by HTMX, we clean up all preline in there
window.HSStaticMethods.autoClean(undefined, event.detail.target);
// event.detail.elt is the node that was swapped in by HTMX, we init up all preline in there
window.HSStaticMethods.autoInit(undefined, event.detail.elt);
}); One could even do this clean up completely automagically by checking inside EDIT: The code above is wrong. One would probably need to use both beforeSwap and afterSwap... |
Beta Was this translation helpful? Give feedback.
-
First of all huge thanks for helping others with the replies - appreciate it much! Our team will look into the above issue in details as soonest as possible. Cheers! |
Beta Was this translation helpful? Give feedback.
-
@langscot @oliverhaas Thank you, guys, for your discussion and for your great interest in our library. I really appreciate your contributions to improving it! @oliverhaas I've noticed that the previous PR doesn't include the changes present in your fork. If it's not too much trouble, could you please create a PR from your repository to ours? Thank you in advance! |
Beta Was this translation helpful? Give feedback.
-
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 componentsinit()
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 theHSBasePlugin
class, and have made theHSStaticMethods.cleanCollection()
method call thisdestroy()
for each instance ofHSBasePlugin
in the respective collection arrays.I have only really implemented a proper
destroy()
method for theHSOverlay
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.
Beta Was this translation helpful? Give feedback.
All reactions