Le 12/10/2011 19:32, John J Barton a écrit :
> Is their any hope of solving the "GC" problem with events? By that I
> mean the tedious business of matching removeEventListener to every
> addEventListener. If I take an event-thingy from an object, then drop
> the reference, the remove is automatic:
> foo.observeBar = function(event) {
> // foo deals with bar's change
> }.magicHere( bar.changed);
> ...
> delete foo.observeBar; // remove event listener
> or foo gets deleted or goes out of scope etc.
In my implementation, the only things which have access to the listeners
are addListener, removeListener and fire (+revokeEvent if the
implementation doesn't figure out that this method actually doesn't use
listeners).
Once all references to these are lost, (when doing "delete o.e;" (see
delete trap) or if a reference to the object is lost), then, references
to the listeners can be freed.
A leak remains if someone keeps a reference to any of the 3 functions
(revokeEvent is internal and I take care of removing all references to
it internally).
... wait a minute...
Instead of the canFire boolean mess, I could just do "listeners =
null;". The only to the array of listener would be lost enabling freeing
the listeners.
Unless removed manually, listeners would be bound to the lifetime of the
event property which sounds good to me. If the event property is
deleted, listeners can be removed. If the object is GC'd, then so can be
the event property, then so can be the listeners.
Do you see another leak?
David
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss