I'm trying to use a Chrome Worker in my addon. Since Firefox 8 you
theoretically don't need to use Cc/Ci to instantiate the Chrome Worker,
instead you use new ChromeWorker.
However, this will throw an error with Jetpack since it finds
ChromeWorker to be undefined.
Would there be something special to do to make this work with jetpack? I
need to use a ChromeWorker so that expensive processing can be running
in its own thread! Many thanks! :-)
Here is my main.js:
var data = require("self").data;
var {Cc, Ci} = require('chrome');
if (Cc["@mozilla.org/threads/workerfactory;1"]) {
// Firefox 4-7
var workerFactory = Cc["@mozilla.org/threads/workerfactory;1"]
.createInstance(Ci.nsIWorkerFactory);
var worker = workerFactory.newChromeWorker(data.url("myworker.js"));
} else {
// Firefox 8+
var worker = new ChromeWorker(data.url("myworker.js"));
}
worker.onmessage = function(event) {
console.log("Worker said:" + event.data);
};
and the content of myworker.js (in the data/ folder):
self.onmessage = function(event) {
self.postMessage('Hi there!');
};
> However, this will throw an error with Jetpack since it finds
> ChromeWorker to be undefined.
> Would there be something special to do to make this work with jetpack? I
> need to use a ChromeWorker so that expensive processing can be running
> in its own thread! Many thanks! :-)
ChromeWorker is defined in the global object and JSM in the regular XUL
development.
Unfortunately, a jetpack module is a different thing. I don't know if
there are any plan to have the Worker / ChromeWorker available directly
from jetpack, I hope so, because since FF8 there is no way to obtain
them using XPCOM.
At the moment I will suggest to you to load a JS Module through
`Cu.import` where export / instantiate the ChromeWorker.