So basically, I have a json file that defines what to load when a user logs in. This includes javascript files. When a different user logs in, the previous stuff needs to be unloaded without a page refresh.
I'm making use of the javascript dynamic import
modules[moduleId] = await import(url);
I'm finding, that even after I clear the "modules"-variable in to an empty object, the event listeners defined within the dynamically imported module are still firing. I'm assuming most everything else defined within that closure is still taking up memory also.
Now if I were to abuse the login/logout like this, I'm assuming I would end up with memory leak issues. How do I make sure the modules loaded in to an object reference are properly unloaded when they are no longer needed?
I'm working with pure js here with no access to outside libraries or tools.
import
several times since importing an already-loaded module will not load it again but simply return the existing instance. If you have event listeners in there though, then you need to call some function that unregisters them. – Tabret