Problems with Parallel.js/WebWorker and optimized code

375 Views Asked by At

I'm using rjs optimizer via grunt. I want to use webworker using the Parallel.js lib, but I get syntax errors running the optimized code.

Comparison of optimized and unoptimized code:

See this gist at github: https://gist.github.com/mwager/4123a3fd5577b630582b

As you cann see I want to use smt like helper().importedHelperFunction1() inside the worker. This way it's easy to import helper functions to my worker code (see http://adambom.github.io/parallel.js/#require).

The real problem is syntax error because of the optimized variable- and function-names. Syntax error in chrome dev tools is:

Uncaught SyntaxError: Unexpected token (

which has problems with the function () { in line 2 of the gist.

In the optimized code the helpers stuff looks like this:

f=function(){return{importedHelperFunction1:function(a,b){return a+b}}}

But Parallel.js requires named functions to import into workers:

var webworkerHelpers = function helper() { // <-- named! but not after optimization )-:
    return {
        importedHelperFunction1: function(a, b) {
            return a + b;
        }
...

So it will import the function like this:

function() { return { importedHelperFunction1: ...

which leads to the syntax error...

I'm doing smt like this:

var theWorker = new Parallel(workerData);
theWorker.require(webworkerHelpers);

Does anyone have an idea what could be the best way to handle this kind of problem? Should I quit using Parallel js? Or is there any option in the optimizer to "don't remove function names?". Sorry, just cannot get this to work...

1

There are 1 best solutions below

1
fr00t On

Hmm, maybe I should consider another (better) way of importing my helper functions...

I wanted to import this way because of legacy browser support, so if webworker support is not available (e.g. IE9) I could just call the function which should be passed in side the worker as normally and then use helper().someFn() there too...