Marius Gundersen (2013-11-08T00:27:09.000Z)
domenic at domenicdenicola.com (2013-11-17T17:50:59.224Z)
On Thu, Nov 7, 2013 at 11:42 PM, K. Gadd <kg at luminance.org> wrote: > I'll try and restate an example I used before. Instead of using weakRefs to solve your problem you could use weak event listeners. Using weak event listeners would mean you have an event dispatcher which has a weak reference to an event handler. This way the handler object can listen to events from other parts of the system without being kept alive by those other parts. In your example, each tab can be an event handler, and they can weakly listen to events from other tabs. Only the coordinator has a strong reference to the tabs, and so when the coordinator severs the strong reference, the tabs are eventually garbage collected. Or another example would be a page consisting of multiple web components. Each component is strongly reference by the DOM, but they weakly listen to events from each other. This means that you can delete a component by removing it from the DOM. The object will (eventually) be garbage collected, and will no longer listen to events. I have created a gist which describes how weak event listeners could work: https://gist.github.com/mariusGundersen/7364253 It seems that every (real) usecase for weakRefs, weakMaps and weakSets is to implement a weak event listener system. Unfortunately weak event listeners cannot be implemented with the current weakSet/weakMap spec, since they are not iterable. To implement an event dispatch system we need to be able to iterate over the listeners. A weak event dispatcher has a hidden iterable weakSet of listeners. It iterates over this weakSet and notifys each entry of the event which has occured. But it does not expose this iterable set. Does it sound like weak event listeners could solve the challenges you face? Is there a problem which can be implemented with weak references which cannot be solved by weak event listeners?