9

I have an ember application that I create like this:

window.App = Ember.Application.create({});

I want to do some background processing on a web worker.

How can I get access to the window object or some other global object in the separate web worker thread?

dagda1
  • 26,856
  • 59
  • 237
  • 450
  • Can you use `postMessage()` to send state back? – alex Jun 08 '12 at 08:49
  • I can but does that not pass context back to the main thread? – dagda1 Jun 08 '12 at 08:54
  • 1
    Yeah. From what I can gather, you're supposed to do things on a new thread with no immediate side effects on the main thread. – alex Jun 08 '12 at 08:57
  • What I am trying to do is load an object with json and have it available later. Should I put the data into local storage as trying to update the store on the new thread has side effects. – dagda1 Jun 08 '12 at 08:58

1 Answers1

17

Short answer. You can't.

The only resources available to web workers are that which they load from JavaScript files using importScripts() or anything that is passed to them via postMessage().

You can however now pass Objects to them. They are serialized and de-serialized to JSON automatically.

Also, there is no access to local storage from the Worker.

Eric Reed
  • 377
  • 1
  • 6
  • 21
Jivings
  • 22,834
  • 6
  • 60
  • 101
  • Do you know if I call importScripts, would the window object be available in the inmported script? – dagda1 Jun 08 '12 at 09:17
  • 8
    You can have **no access to the window Object from a worker.** – Jivings Jun 08 '12 at 09:18
  • @dagda1 Try having a look at the [web worker tutorial on MDN](https://developer.mozilla.org/En/Using_web_workers). – Jivings Jun 08 '12 at 09:23
  • if I call self.postMessage from the web worker, does that push context back to the main UI thread or is it still on the worker thread? – dagda1 Jun 08 '12 at 09:24
  • @dagda1 Do you mean scope? The scope of `postMessage()` will be the Worker Object. – Jivings Jun 08 '12 at 09:26
  • I mean which thread will it be running on after the web worker has called self.postMessage() and the onMessage callback is invoked, is the execution returned to the UI thread? I am assuming yes because the window object is accessible in this handler. – dagda1 Jun 08 '12 at 09:30
  • Everything is handled by callbacks in the appropriate threads. – Jivings Jun 08 '12 at 09:33