Hi

I'm experimenting with a workerpool, but I seem to be running into a
serious obstacle, trying to access an outside function within a
worker.  In short, here's my setup (using a main.js file and a
worker.js file):

Contents of main.js:

var myObj =  //Makes sure all is in my own namespace
{
  "hi": "there",  //Just a string
  "workerPool": null,  //Will be the main workerpool object
  "worker": null,  //Will be the id of the worker we call
  "somefunction: function(){alert("hello");},  //Some function which
will be called from within the worker
  "initWorkerPool": function()  //Initialize the workerpool
  {
    myObj.workerPool = google.gears.factory.create('beta.workerpool');
    myObj.workerPool.onmessage = function(a, b, message)
    {
      alert(message.body);
    }

    //Use jquery to retrieve worker text
    myObj.worker = myObj.workerPool.createWorker($.ajax({"type":
"GET", "url": "worker.js", "async": false}).responseText);
  }
}

myObj.initWorkerPool();

//Call worker
myObj.workerPool.sendMessage([JSON.stringify(myObj)], myObj.worker);



Contents of worker.js:

var worker = google.gears.workerPool;
worker.onmessage = function(a, b, message)
{
  var myObj = JSON.parse(message.body[0]);
  var x = myObj.somefuntion();
  worker.sendMessage("Hello", message.sender);
}



A short explanation:

1. I use jquery to retrieve the worker.js file.  Seems like Gears
cannot handle local paths (File://).  This works like a charm.
2. I use JSON to stringify and parse myObj.  I need its functionality
inside the worker function and passing it as a real object is not
permitted by Gears ("Cannot marshall...")

I tried the above code and it all works fine, except one thing.
JSON.stringify seems to trip out all functions, so "somefunction" is
no longer part of myObj.  I verified this with a for(var prop in myObj)
{}.  When I return myObj.hi, it will alert "there" as it is supposed
to.

Is there a way to make available all the elements of myObj from within
the worker function?

Thanks!

Reply via email to