A tour of how the Widget (Virtual DOM) code in Discourse works

I’m using appEvents to trigger widget re-rendering after message-bus notifications. This is working ok, but I can’t find any way to “de-register” an appEvent listener once a widget is no longer required.

I setup the listener in the widget’s defaultState() function, in the same way as is done in core widgets:

https://github.com/davidtaylorhq/discourse-whos-online/blob/master/assets/javascripts/discourse/initializers/start-whos-online.js.es6#L85-L92

Ideally then I want to be able to call

appEvents.off("whosonline:changed")

when the widget is no longer needed. Otherwise the array of listeners keeps growing and eventually explodes

https://meta.discourse.org/t/whos-online-plugin/52345/90?u=david_taylor

I was hoping to use the widget destroy function, but it doesn’t look like it’s actually wired up to anything. The default implementation is a console message, and I don’t ever see that in the console.

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/widgets/widget.js.es6#L176-L178

@eviltrout is there some way to run logic when a widget is thrown away? Or is there another way I should be approaching this problem?

4 Likes