You currently have Javascript disabled, to use some of the features (such as the login) you must enable it for this site
Event Listeners and You! | Custom Buttons²

Event Listeners and You!

Info regarding the extension

Postby SCClockDr on Wed Mar 19, 2008 12:05 pm

Hi All

The recent up tick in forum activity is indeed pleasing to note!

I wish to offer some words of :warn: caution :warn: regarding the increased appearance of code containing event listeners and to a lesser extent observers.

When you employ these quite useful techniques you must also provide an explicit means to remove them.
  • Each time a button with a listener/observer is initialized a new (listener/observer) instance is created and the old instance is abandoned/orphaned.
  • Abandoned/orphaned as this instance may be, it still exists in memory.
  • Thus creating a memory leak.
    So!
  • You must provide for detecting the presence of the listener and remove it if found.
  • Then create the new instance within the initialization code on button initialization.
  • You must also provide for its removal for window close.
    • Because each instance of FF initializes a completely new set of Custom Buttons² buttons.
    • The code block below documents a provision within Custom Buttons² for setting up to handle the window close event or you can create your own if so inclined.
      Edited by SCClockDr @ Sat Mar 22 2008 8:53:39 AM GMT-0400 (Eastern Daylight Time)
      A correction! Window close functionality not implemented as yet. But please do implement the code illustrated below because I intend to base the implementation on this structure.
    The bottom line is this must be provided for by you the button author, because the extension is incapable of knowing in advance that you have created these listeners/observers.
Code: Select all
//To initiate
custombuttons.listeners.yourUniqueName = document.getElementById(IDoftheObjectYouareGoingtoListento);
custombuttons.listeners.yourUniqueName.addEventListener("popupshowing", ListenerFunctionDefinition, false);
// These will be closed out by Custom Buttons² automatically on window close.
Custom Buttons² Version 2.0.7.5b1 is not jar'd so you can look into the code referenced above.
Regards
George
User avatar
SCClockDr
Admin
Admin
 
Posts: 844
Joined: Thu Jun 14, 2007 7:28 pm
Location: SC UpState

Postby ithinc on Sat Mar 22, 2008 11:44 am

SCClockDr wrote:So!
  • You must provide for detecting the presence of the listener and remove it if found.
  • Then create the new instance within the initialization code on button initialization.

Recently I find even in normal cases, the buttons could be initialized more than one times, due to toolbar customization. So to detect and remove the listeners of the last button instance is necessary. The code above could be acting for window close, although I don't know how it works(I've read the extension codes, but didn't find where). But how can we do the similar thing for initialization? Is it possible for CB2 to provide a method like 'ondestroy' to do such things when one instance is destroyed?
ithinc
Registered User
Registered User
 
Posts: 67
Joined: Thu Feb 21, 2008 8:22 pm

Postby SCClockDr on Sat Mar 22, 2008 12:49 pm

ithinc wrote:
SCClockDr wrote:So!
  • You must provide for detecting the presence of the listener and remove it if found.
  • Then create the new instance within the initialization code on button initialization.

Recently I find even in normal cases, the buttons could be initialized more than one times, due to toolbar customization. So to detect and remove the listeners of the last button instance is necessary. The code above could be acting for window close, although I don't know how it works(I've read the extension codes, but didn't find where). But how can we do the similar thing for initialization? Is it possible for CB2 to provide a method like 'ondestroy' to do such things when one instance is destroyed?

Hi ithinc

I stand corrected!
The Extension indeed does not iterate an array for listeners I confused listeners with editor windows.
There is no destructor implemented at this point so you are on your own for now.
  1. You will need an onclose listener to do as I've illustrated below for closure
  2. You can call your listener's callback function if you detect its presence on initialization

Custom Buttons² has no idea:
  1. If there is/are listener/s in a button.
  2. What the details are.
Therefore it is the button author's responsibility to test for the presence of your intended listener during initialization and closure. Sorry about that
  • On start up or with a new window there will be no listener present
  • On all other occasions there will be a listener present - These are the situations that need attention
For window close info look at "custombuttons.close" in Cb2_overlay.js (line 1240 bottom of the file)
Code: Select all
custombuttons.close = function(e) {
  let len = this.aEditorWindows.length;
  for ( let i = 0; i < len; i++) {
    this.aEditorWindows[i][2].close(e);
  } // End for
  this.App.close( this );
  this.listeners.Cbcm.removeEventListener("popupshowing", this.deblecontextListener, false);
  this.listeners.Cbcms.removeEventListener("popupshowing", this.deblecontextListenerSub, false);
  window.removeEventListener("load",function(e) {
            custombuttons.init();
          },false);
  window.removeEventListener("unload",function(e) {
            custombuttons.close(e);
            /* custombuttons.App.saveButtonsToProfile( custombuttons ); */
          },false);
};
and (as an example)
Code: Select all
      custombuttons.App.close: function()
      {
        this.saveButtonsToProfile();
        this.Cb.listeners.cm.removeEventListener("popupshowing", this.contextListener, false);
      },
You can use the same architecture within your button to accomplish this end.
Regards
George
User avatar
SCClockDr
Admin
Admin
 
Posts: 844
Joined: Thu Jun 14, 2007 7:28 pm
Location: SC UpState

Postby ithinc on Tue Mar 25, 2008 5:02 pm

SCClockDr wrote:Custom Buttons² has no idea:
  1. If there is/are listener/s in a button.
  2. What the details are.
Therefore it is the button author's responsibility to test for the presence of your intended listener during initialization and closure. Sorry about that
  • On start up or with a new window there will be no listener present
  • On all other occasions there will be a listener present - These are the situations that need attention


I have tryied this code snippet, and it seems to be working.
Code: Select all
if (custombuttons.listeners.autoCopyListeners) {
  for (var i=0; i<custombuttons.listeners.autoCopyListeners.length; i++) {
    custombuttons.listeners.autoCopyListeners[i][0].removeEventListener(
      custombuttons.listeners.autoCopyListeners[i][1],
      custombuttons.listeners.autoCopyListeners[i][2],
      custombuttons.listeners.autoCopyListeners[i][3]);
  }
}
custombuttons.listeners.autoCopyListeners = [];
custombuttons.listeners.autoCopyListeners.push([document, "DOMContentLoaded", onLoad, true]);

Could it make some conflicts when your planned array for listeners would be implemented?
ithinc
Registered User
Registered User
 
Posts: 67
Joined: Thu Feb 21, 2008 8:22 pm

Postby SCClockDr on Tue Mar 25, 2008 5:19 pm

ithinc wrote:
SCClockDr wrote:Custom Buttons² has no idea:
  1. If there is/are listener/s in a button.
  2. What the details are.
Therefore it is the button author's responsibility to test for the presence of your intended listener during initialization and closure. Sorry about that
  • On start up or with a new window there will be no listener present
  • On all other occasions there will be a listener present - These are the situations that need attention


I have tryied this code snippet, and it seems to be working.
Code: Select all
if (custombuttons.listeners.autoCopyListeners) {
  for (var i=0; i<custombuttons.listeners.autoCopyListeners.length; i++) {
    custombuttons.listeners.autoCopyListeners[i][0].removeEventListener(
      custombuttons.listeners.autoCopyListeners[i][1],
      custombuttons.listeners.autoCopyListeners[i][2],
      custombuttons.listeners.autoCopyListeners[i][3]);
  }
}
custombuttons.listeners.autoCopyListeners = [];
custombuttons.listeners.autoCopyListeners.push([document, "DOMContentLoaded", onLoad, true]);

Could it make some conflicts when your planned array for listeners would be implemented?
Might I steal (Borrow permenantly) ;) your code?
Regards
George
User avatar
SCClockDr
Admin
Admin
 
Posts: 844
Joined: Thu Jun 14, 2007 7:28 pm
Location: SC UpState

Postby ithinc on Tue Mar 25, 2008 5:28 pm

Aha, of cource you can. A more simplified one:
Code: Select all
while (custombuttons.listeners.autoCopyListeners && custombuttons.listeners.autoCopyListeners.length) {
  var listener = custombuttons.listeners.autoCopyListeners.pop();
  listener[0].removeEventListener(listener[1], listener[2], listener[3]);
}
custombuttons.listeners.autoCopyListeners = [];

document.addEventListener("DOMContentLoaded", onLoad, true);
custombuttons.listeners.autoCopyListeners.push([document, "DOMContentLoaded", onLoad, true]);
ithinc
Registered User
Registered User
 
Posts: 67
Joined: Thu Feb 21, 2008 8:22 pm

Postby squeaky on Tue Apr 01, 2008 4:00 am

OK guys, you're getting all "technical" again!

I have an observer that SCClockDr gave me:-

Code: Select all
// MANY thanks to SCClockDr for this bit

    this.PS = Components.classes['@mozilla.org/preferences-service;1']
                        .getService(Components.interfaces.nsIPrefBranch);

    // This is how you establish the observer
    this.ob={};
    this.ob.observe=this.setState.bind(this);
    this.PS.addObserver(this.pref,this.ob,false);
    // Establish the initial state
    this.setState();


So... I need to give it a unique name:- this.ob={mycookieobs}; ?

And then modify this to work for my observer...?

Code: Select all
while (custombuttons.listeners.autoCopyListeners && custombuttons.listeners.autoCopyListeners.length) {
  var listener = custombuttons.listeners.autoCopyListeners.pop();
  listener[0].removeEventListener(listener[1], listener[2], listener[3]);
}
custombuttons.listeners.autoCopyListeners = [];

document.addEventListener("DOMContentLoaded", onLoad, true);
custombuttons.listeners.autoCopyListeners.push([document, "DOMContentLoaded", onLoad, true]);
Regards and stuff,
Richard.

CustomButtons2:- ver 2.0.7.5; Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14
User avatar
squeaky
Moderator
Moderator
 
Posts: 445
Joined: Sat Mar 01, 2008 8:48 am
Location: Sunny Suffolk


Return to Announcements

Who is online

Users browsing this forum: • Registered: 0 and 0 guests