- Notifications
You must be signed in to change notification settings - Fork689
Webworker
There are some different versions of webworkers for AlaSQL
<scriptsrc="alasql-worker.js"></script><script>alasql('SELECT VALUE 1+1',[],function(res){console.log(res);});</script>
Include filealasql-worker.js on the page, and it will downloadalasql.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 examplehere.
If you want to load scripts into webworker you can use REQUIRE statement:
alasql(['REQUIRE "script1.js", "script2.js"']).then(function(){// sql can usescript1.js });
Usually this required for user-defined functions, like:
alasql.fn.myfn=function(x){returnx*x;}
Please note that webworkers does not support localStorage so this is not supported for AlaSQL running in a webworker.
<scriptsrc="alasql.js"></script><script>alasql.worker();alasql(['SELECT VALUE 1+1'].then(function(res){console.log(res);});</script>
Include filealasql.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 examplehere.
alasql.worker() function has three parameters:
alasql.worker()-startworkeralasql.worker("alasql.min.js")-startworkerfromspecifiedpathalasql.worker("alasql.min.js",["script1.js","script2.js"],callback)-loadadditionaljavascriptfiles.
Try these three examples:
- http://alasql.org/demo/012worker/index.html - option 1 - include alasql-worker.min.js, which loads alasql.min.js as webworker. AlaSQL catches messages
- http://alasql.org/demo/012worker/index1.html - option 2 - just load importScripts("alasql.min.js") inside webworker. AlaSQL does not catch messages
- http://alasql.org/demo/012worker/index2.html - option 3 - run alasql.worker() function, which loads alasql.min.js as webworker. AlaSQL catches messages
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>varworker=newWorker('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);}```;
© 2014-2026,Andrey Gershun &Mathias Rangel Wulff
Please help improve the documentation by opening a PR on thewiki repo