0

Is it necessary to call unregisterAllListeners() in Pub Sub model? What is the impact if we do not call unregisterAllListeners(). Kindly advice.

Thanks. Janakiraman

3 Answers 3

1

You need to unsubscribe for each time you subscribe. If you don't, you will end up forcing a hard reference to disposed components, and they will continue to respond to events that no longer apply to them, and those components will remain in memory, effectively leaking memory.

You can either unsubscribe one at a time, or use unregisterAllListeners to unsubscribe from all events at once. It is strongly recommended that you use unregisterAllListeners to avoid situations where you forget to unsubscribe from an event, thus causing memory leaks and unnecessary event handling, which would eventually cause performance problems.

Either way, do not forget to unsubscribe during the disconnectedCallback handler, or you will face performance problems and glitches over time.

0

As per documentation `The framework takes care of managing and cleaning up listeners for you as part of the component lifecycle.

But you need to remove listeners in disconnectedCallback if you attach you listeners to window or document etc.

Like window.addEventListener etc.

0

It is NOT necessary to call unregisterAllListeners() in PubSub model and there is no impact as such if unregisterAllListeners() isn't called at all; but this could be a subjective topic based on the business requirement. PubSub model is essentially setting up a channel for communication across DOM (between sibling components), where one component (publisher) could fire an event and all other components (subscribers) would respond to the event via registered listeners. You would unregister the listener on a specific subscriber or unregister all subscriber listeners as dictated by business requirements. If there is no such business requirements, the subscribers would simply continue responding to the events fired by publisher.

BTW, PubSub module was released as temporary solution for the lack of feature parity between LWC and Aura. It has now been replaced by Lightning Message Service and you should try to use LMS, unless you are working on an unsupported scenario that isn't listed here.

4
  • Are you sure? It sounds like there would be a memory leak if you didn't remove the listeners.
    – sfdcfox
    Dec 21, 2020 at 13:08
  • @sfdcfox +1 to your answer. While it's a best practice to unsubscribe events to avoid memory leakage, I left the answer tagged as "subjective topic" because not unsubscribing is still a bad coding practice and has a potential to cause unreported memory leakage in future. May be I should have clarified that part in bit more detail.
    – arut
    Dec 21, 2020 at 14:01
  • @sfdcfox My reasons were that the pubsub module is restricted to a single page and the code is already following best practices (such as avoiding global variables, no circular references, local event object to store all event references relevant to the page). So, when user moves out of the page, the page reference would be removed & there shouldn't be any marked references to the memory consumed by this module. So, the Garbage Collector (Mark & Sweep reference) should clear out the memory?
    – arut
    Dec 21, 2020 at 14:05
  • @sfdcfox Do you see anything other than the above points that would pave an easy way for memory leakage in using PubSub module? If yes, I would like delete this answer or make amends to this answer. I understand that if it was LMS, then unsubscribing is a definite requirement to avoid memory leakage.
    – arut
    Dec 21, 2020 at 14:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .