What is the status of Weak References?
bruant.d at gmail.com
Sat Mar 2 16:37:08 PST 2013
I'm still balanced on whether/when/how the one-time listener pattern can
be effective, but I feel like it's an interesting idea to discuss.
Le 02/03/2013 21:13, Marius Gundersen a écrit :
> > I won't say it's absolutely better than WeakRefs and it may not
> apply to the data binding case (?), but it's an interesting pattern to
> keep in mind.
> I can't see how this would work in an observer/listener application.
> The listening object has no way of knowing if it will be deleted when
> the event occurs, so it cannot decide whether to resubscribe or not.
> If all references to it were still deleted, it would not go away. It
> would therefore need a shouldNotResubscribe flag, which must be set
> when it should be deleted. When the next event occurs it can react and
> decide not to resubscribe. This means that a listening object still
> needs a dispose method (to set the shouldNotResubscribe flag)
In the weakref case, someone has to keep a strong reference to the
listener until it's not needed anymore. The decision to cut this last
strong reference is exactly the same decision as deciding when not to
I think the difference is that it might be that the last reference is
cut because another object got released and this object got released
because another object got released, etc. Unless the entire application
is collectable, I think somewhere in the chain, there is an explicit
strong reference cut.
My point is that being garbage collectable needs an explicit action
somewhere. WeakRef is a convenient construct that benefits from a
cascading of the explicit action. But there is an explicit action needed
> and it also means that it would not be deleted until the next event
> occurs, which could be in a very long time.
There can be a removeEventListener too.
> On Sat, Mar 2, 2013 at 7:53 PM, Bill Frantz <frantz at pwpconsult.com
> <mailto:frantz at pwpconsult.com>> wrote:
> On 3/2/13 at 3:47 AM, bruant.d at gmail.com
> <mailto:bruant.d at gmail.com> (David Bruant) wrote:
> "I won't say it's absolutely better than WeakRefs and it may
> not apply to the data binding case (?), but it's an
> interesting pattern to keep in mind."
> Speaking from ignorance here.
> One advantage of the "resubscribe for every event" pattern is that
> if the events are generated in one process -- an animation process
> is the example used here -- and a message is sent to the observer
> in another process, and the observer is slow, the natural outcome
> will be to drop frames instead of queue up a lot of messages
> pertaining to events that no longer need to be processed.
> Cheers - Bill
> Bill Frantz | When it comes to the world | Periwinkle
> (408)356-8506 <tel:%28408%29356-8506> | around us, is there any
> choice | 16345 Englewood Ave
> www.pwpconsult.com <http://www.pwpconsult.com> | but to explore? -
> Lisa Randall | Los Gatos, CA 95032
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> es-discuss mailing list
> es-discuss at mozilla.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss