Skip to content

Webworker

Mathias Rangel Wulff edited this page Feb 13, 2016 · 5 revisions

WebWorker

There are some different versions of webworkers for AlaSQL

Option 1:

    <script src="alasql-worker.js"></script>
   <script>
        alasql('SELECT VALUE 1+1', [], function(res){
            console.log(res);
        });
    </script>

Include file alasql-worker.js on the page, and it will download alasql.min.js as a webworker. After that alasql() function will post message to webworker, and then return result value back. The webworker version of alasql() is async.

Try this example here.

If you want to load scripts into webworker you can use REQUIRE statement:

    alasql('REQUIRE "script1.js", "script2.js" ',[],function(){
        // sql used script1.js
    });

Usually this required for user-defined functions, like:

    alasql.fn.myfn = function(x){ return x*x; }

Please note that webworkers does not support localStorage so this is not supported for AlaSQL running in a webworker.

Option 2:

    <script src="alasql.js"></script>
   <script>
        alasql.worker();
        alasql('SELECT VALUE 1+1', [], function(res){
            console.log(res);
        });
    </script>

Include file alasql.js on the page, and then run alasql.worker(). It will create new Worker based on the alasql.min.js. After that you can use AlaSQL as shown before. Again, the webworker version of alasql() is async.

Try this example here.

alasql.worker() function has three parameters:

    alasql.worker() - start worker
    alasql("alasql.min.js") - start worker from specified path
    alasql("alasql.min.js",["script1.js","script2.js"], callback) - load additional javascript files.

Try these three examples:

Your option is number 2. Can you check it?

index1.html

    <h1>Worker demo: simple worker</h1>
    <p>See Console for worker results</p>
    <script>
        var worker = new Worker('other-worker.js');
    </script>

other-worker.js

    importScripts('../../console/alasql.min.js');
    if(alasql) {
        console.log('AlaSQL is here?',alasql('SELECT VALUE TRUE'));
        console.log('self.onmessage is free?',!self.onmessage);
    }
```;
Clone this wiki locally