Creating a global function - Mozilla
This is a discussion on Creating a global function - Mozilla ; Hi,
I am new to developing firefox extensions.
I want to create an extension that executes some functions
(xmlhttprequests, etc.) in the background. They should update data
that my extension displays. The problem is that I do not want to
...
-
Creating a global function
Hi,
I am new to developing firefox extensions.
I want to create an extension that executes some functions
(xmlhttprequests, etc.) in the background. They should update data
that my extension displays. The problem is that I do not want to
execute these functions for every single window, because that would
waste resources, especially if a user opens and closes windows often.
So I need a way to run them globally.
My current implementation of the extension looks like this and does
not include such a "global function".
var myextensionname = {
onLoad: function()
{
//do stuff
},
onUnLoad: function()
{
//do stuff
},
// some other interesting functions
}
window.addEventListener("load", function(e) { myextensionname.onLoad
(); }, false);
window.addEventListener("unload", function(e)
{ myextensionname.onUnLoad(); }, false);
Now I want to declare a "global function" somefunction() that should
be called on application startup. It will then make an asynchronous
xmlhttprequest which will, after being successful, execute somefunction
() again via setTimeout(). And so on.
So the whole thing should run in the background as long as firefox is
running. Even if the user opens a second browser window and closes the
first one.
My second problem is that these background workers should also fire
events so that my normal functions can update the window contents with
the new data.
I would like to know the best way to implement this. Is this
documented somewhere or are there examples?
Kind regards,
Ulrich
-
Re: Creating a global function
I have finally found some documentation after reading this thread:
http://forums.mozillazine.org/viewto...?f=19&t=367298
I am now using an XPCOM object that has a function startup(data) which
is called from my onLoad function when the user opens a window. After
the first call, it sets a variable firstrun to true, so that it is
only started once. The startup-function in the XPCOM object then
starts a thread which does the background work.
But one problem is left. The background thread is doing some work,
that may take longer than a few seconds. If the user quits firefox
while the background thread is running, firefox will wait until the
background thread finishes. Calling nsIThread shutdown() on
application shutdown does not work, because shutdown() does not kill
the thread. Is there a function to kill a thread, or isn't that
possible?
On Dec 30 2009, 11:37*pm, Ulrich wrote:
> Hi,
> I am new to developing firefox extensions.
> I want to create an extension that executes some functions
> (xmlhttprequests, etc.) in the background. They should update data
> that my extension displays. The problem is that I do not want to
> execute these functions for every single window, because that would
> waste resources, especially if a user opens and closes windows often.
> So I need a way to run them globally.
>
> My current implementation of the extension looks like this and does
> not include such a "global function".
>
> var myextensionname = {
> * * onLoad: function()
> * * {
> * * * * //do stuff
> * * },
>
> * * onUnLoad: function()
> * * {
> * * * * //do stuff
> * * },
>
> * * // some other interesting functions}
>
> window.addEventListener("load", function(e) { myextensionname.onLoad
> (); }, false);
> window.addEventListener("unload", function(e)
> { myextensionname.onUnLoad(); }, false);
>
> Now I want to declare a "global function" somefunction() that should
> be called on application startup. It will then make an asynchronous
> xmlhttprequest which will, after being successful, execute somefunction
> () again via setTimeout(). And so on.
> So the whole thing should run in the background as long as firefox is
> running. Even if the user opens a second browser window and closes the
> first one.
>
> My second problem is that these background workers should also fire
> events so that my normal functions can update the window contents with
> the new data.
>
> I would like to know the best way to implement this. Is this
> documented somewhere or are there examples?
>
> Kind regards,
> Ulrich
-
Re: Creating a global function
On Jan 4, 9:54*pm, Eric Jung wrote:
>
> You might find it easier to use a singleton JS module instead of a
> singleton XPCOM component.
Well, I have imlemented the whole XPCOM component in the meantime, but
it is good to know for the future.
> Concerning killing of the thread, thread
> manangement from extension javascript has been notoriously difficult
> in the past.
"Difficult" means it is possible? I could only find the function
shutdown(). In the XPCOM component, I observe "quit-application" and
when the app quits, I tried to use this.mythread.shutdown(), but this
waits for the thread to finish (which could take some time). I've also
tried this.mythred = null: same problem.
The thread would only be used for some XML processing, so a hard kill
would be no problem.
> It might be easier (albeit not more performant) for your
> thread to periodically check if any navigator:browser windows are open
> and, if not, fall through:
>
> var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
>
> .getService(Components.interfaces.nsIWindowMediato r).getMostRecentWindow("navigator:browser");
>
> if (wm) { /* do stuff */ } else { /* end */ }
Good idea. I could also use a global variable "this.stopThread" in the
XPCOM component and set it to true in my observer. It works. But
killing the thread would be better.
Kind regards,
Ulrich