| 1 |
//#ifnot omit-oo1 |
| 2 |
/* |
| 3 |
2022-05-23 |
| 4 |
|
| 5 |
The author disclaims copyright to this source code. In place of a |
| 6 |
legal notice, here is a blessing: |
| 7 |
|
| 8 |
* May you do good and not evil. |
| 9 |
* May you find forgiveness for yourself and forgive others. |
| 10 |
* May you share freely, never taking more than you give. |
| 11 |
|
| 12 |
*********************************************************************** |
| 13 |
|
| 14 |
This is a JS Worker file for the main sqlite3 api. It loads |
| 15 |
sqlite3.js, initializes the module, and postMessage()'s a message |
| 16 |
after the module is initialized: |
| 17 |
|
| 18 |
{type: 'sqlite3-api', result: 'worker1-ready'} |
| 19 |
|
| 20 |
This seemingly superfluous level of indirection is necessary when |
| 21 |
loading sqlite3.js via a Worker. Instantiating a worker with new |
| 22 |
Worker("sqlite.js") will not (cannot) call sqlite3InitModule() to |
| 23 |
initialize the module due to a timing/order-of-operations conflict |
| 24 |
(and that symbol is not exported in a way that a Worker loading it |
| 25 |
that way can see it). Thus JS code wanting to load the sqlite3 |
| 26 |
Worker-specific API needs to pass _this_ file (or equivalent) to the |
| 27 |
Worker constructor and then listen for an event in the form shown |
| 28 |
above in order to know when the module has completed initialization. |
| 29 |
|
| 30 |
This file accepts a URL arguments to adjust how it loads sqlite3.js: |
| 31 |
|
| 32 |
- `sqlite3.dir`, if set, treats the given directory name as the |
| 33 |
directory from which `sqlite3.js` will be loaded. |
| 34 |
*/ |
| 35 |
//#if target=es6-bundler-friendly |
| 36 |
import {default as sqlite3InitModule} from './sqlite3-bundler-friendly.mjs'; |
| 37 |
//#else |
| 38 |
"use strict"; |
| 39 |
{ |
| 40 |
const urlParams = globalThis.location |
| 41 |
? new URL(globalThis.location.href).searchParams |
| 42 |
: new URLSearchParams(); |
| 43 |
let theJs = 'sqlite3.js'; |
| 44 |
if(urlParams.has('sqlite3.dir')){ |
| 45 |
theJs = urlParams.get('sqlite3.dir') + '/' + theJs; |
| 46 |
} |
| 47 |
//console.warn("worker1 theJs =",theJs); |
| 48 |
importScripts(theJs); |
| 49 |
} |
| 50 |
//#endif |
| 51 |
sqlite3InitModule().then(sqlite3 => sqlite3.initWorker1API()); |
| 52 |
//#else |
| 53 |
/* Built with the omit-oo1 flag. */ |
| 54 |
//#endif ifnot omit-oo1 |