Auto event listener cleanup
January 8th, 2009
I have made some patches that adds an additional option to listener configs, a src attribute that indicates the listener should be removed whenever this 'src' is destroyed. src must be an Ext.Component.
This code is pretty messy (sorry).... and it uses prototype Enumerable functions. If you guys think this is a good idea, I will clean it up so its ready for actual implementation.
Usage looks like this:
object.on("someevent", function(){
}, this, {
src:this
});
when "this" is destroyed, the listener for someevent is automatically removed.
(I removed my code here because its really a hack... this really needs to be implemented in the core code instead of attaching function interceptors and the like)
yourObject.on('event', this.someFn, this);
this.mon(yourObject, 'event', this.someFn, this);
when you can do:
yourObject.on('event', this.someFn, this, {mon:this});
While I am at it... :-)... I also added a function "addDestroyable" to component so you can register other components that will have their destroy functions called when the parent component is destroyed.
I did this in Ext 1.1 so please correct me if this situation no longer applies: The problem I was having in 1.1 was with certain "floating" (for lack of a better term) components like menu / drop downs / window. The html would not get cleaned up when a panel was closed. This is because the html for "floating" components existed outside of the panel html. So, I was getting memory leakage every time a panel was closed. Granted, the more efficient solution might be to reuse these components from some global scope between tabs but that really isn't ideal from a programming aesthetic standpoint. Plus I think you will get the same problem with grid editors which you cannot really do this with.
I created the "addDestroyable" method as a really simple way to do memory management with components and you didn't have to think about it that much.
yourObject.on('event', this.someFn, this); // not needed
this.mon(yourObject, 'event', this.someFn, this);
vs:
yourObject.on('event', this.someFn, this, {mon:this});
The key things in the decision was 2 fold.
1) The "src" should not be responsible for cleaning up listener's event handlers. listener should clean up it's own mess on destruction.
2) It is also supported for element listeners for more efficient garbage collection (the primary reason it was created). e.g.
this.mon(this.someEl, 'click', this.onClick, this);
this.mon(yourObject, 'event', this.someFn, this);
Using mon if this is destroyed, the listener is automatically removed.
this.mon(yourObject, 'event', this.someFn, this);
Using mon if this is destroyed, the listener is automatically removed.
is it experimental? it's currently only used by the Slider :-/
#If you have any other info about this subject , Please add it free.# |
Posted in freedeadaim.com | edit
A little something about you, the author. Nothing lengthy, just an overview.