requirejs/require-cs

Environment unsupported in WebWorkers

mountain opened this issue · 6 comments

I want to use cs plugin in webworkers, and I just got a "Environment unsupported" error.
Require.js itself support webworkers, but is it possible to use cs in webworkers via require.js?

How to reproduce:

The code you can try is at here:
https://github.com/Wahlque/tutorials/tree/master/coffeescript/004-webworkers
But you should load all the env from
https://github.com/Wahlque/playground

Hence you can clone the playground repository, and serve it in localhost and then hit the link:
http://localhost/tutorials/coffeescript/004-webworkers/

Ah right, the cs.js file explicitly needs to enable running in a web worker. I'll keep this issue open as the ticket used to track the fix. I probably will not get to it until at least this coming weekend.

However, if you want to try a quick fix (this is untested) modify line 4361 in cs.js to look like so:

if ((typeof window !== "undefined" && window.navigator && window.document) || typeof importScripts !== "undefined") {

If that change fixes it for you, please post back, and I'll pursue a fix along those lines, along with a unit test.

Yes, I had already fixed this line.And the postMessage() worked in page or worker. But onmessage() only worked in page. In workers, onmessage() did not hook up. See the code and online demo as below:

I am investigating this problem right now. I am guessing that the global scope onmessage will be hide by local scope in workers

  onmessage = (e) -> postMessage('hello from worker!')

The code might be translated into

  var onmessage = function(e) { postMessage('hello from worker!'); };

I will keep the thread updated after my investigation.

Your fixs works right now. Please see the demo at http://playground.wahlque.org/tutorials/coffeescript/004-webworkers/

Great! Thanks for confirming the fix. I'm hoping to do an update to the repo by the end of this coming weekend.

Finally pushed the change, and did a 0.3.2 release with the change. Available from the website now.

Thank you very much!