Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user-friendly, promise-based SQL.Worker class (in 65 lines-of-code) and deprecate dist/worker.sql-xxx.js #378

Closed
wants to merge 4 commits into from

Conversation

kaizhu256
Copy link
Member

i originally wanted to apply these changes in 3 separate pull-requests, to make the file diffs easier to review.

you can still review the changes in 3 separate parts by clicking the individual commit links below:

commit 1

  • make src/worker.js isomorphic (will load in web-worker, nodejs, and browser without errors).

commit 2

  • update Makefile to include isomorphic src/worker.js in all javascript dist-files
  • deprecate dist/worker.sql-xxx.js

commit 3

  • update src/api.js with new class SQL.Worker and promise-method SQL.Worker.postMessage
  • update src/test.js using SQL.Worker
  • update examples/GUI/gui.js using SQL.Worker
  • update README.md web-worker documentation using SQL.Worker

the new SQL.Worker class is only 65 lines of additional code (including comments):

    /** @typedef {string} url */
    /**
    * @constructs Worker
    * @memberof module:SqlJs
    * @param {url} web-worker url
    * @example
    *     var worker = new SQL.Worker("/dist/sql-wasm.js");
     */
    function SqlWorker(url) {
        this.worker = new Worker(url);
        this.worker.onmessage = function onmessage(msg) {
        /*
         * this function will handle <msg> returned from web-worker
         */
            var callback = workerCallbackDict[msg.data.id];
            delete workerCallbackDict[msg.data.id];
            if (callback) {
                callback(msg.data);
            }
        };
    }

    /** @typedef {Object} msg */
    /**
    * Asynchronously run sql-commands by posting them as web-worker messages
    * @param {msg} web-worker message-object
    * @example
    *     var worker = new SQL.Worker("/dist/sql-wasm.js");
    *     try {
    *         var data = await worker.postMessage({
    *             action: "exec",
    *             params: {"@id": 1},
    *             sql: "SELECT * FROM myTable WHERE id = @id"
    *         });
    *         console.log(data.results[0]);
    *     } catch (sqlError) {
    *         console.error(sqlError);
    *     }
     */
    SqlWorker.prototype["postMessage"] = function postMessage(msg) {
        var callback;
        var error;
        var that;
        that = this;
        // preserve stack-trace, in case of error
        error = new Error();
        return new Promise(function onpromise(resolve, reject) {
            callback = function _callback(data) {
                // if <data>.error, then prepend it to <error>.stack and reject
                if (data.error) {
                    error.message = data.error;
                    error.stack = data.error + "\n" + error.stack;
                    reject(error);
                    return;
                }
                resolve(data);
            };
            // increment workerCallbackId
            // and cycle back to 0 if it overflows past 32-bits
            workerCallbackId = (workerCallbackId + 1) | 0;
            msg.id = workerCallbackId;
            workerCallbackDict[msg.id] = callback;
            that.worker.postMessage(msg);
        });
    };

i also updated the online-demo to use SQL.Worker. full-demo available @
https://kaizhu256.github.io/sql.js-sql-worker/examples/GUI/

i tested the "execute", "Save the db", "Load an SQLite database file" buttons to all work correctly. and also test invalid sql-errors to be handled correctly as well.

image

kaizhu256 and others added 3 commits April 1, 2020 00:20
…pt dist-files

- deprecate dist/worker.sql-xxx.js
…Worker.postMessage

- update src/test.js using SQL.Worker
- update examples/GUI/gui.js using SQL.Worker
- update README.md web-worker documentation using SQL.Worker
@lovasoa
Copy link
Member

lovasoa commented Apr 4, 2020

I think we have discussed that already in #377. I would find it better to let the user choose whatever technology he wants to create his web workers.

And, if I'm not mistaken, this one-file approach causes the same code to be parsed (and downloaded, if it's not served with the right headers) twice. The whole sql.js code (which can be large in the asmjs distribution) is downloaded and parsed a first time just to use the small SqlWorker class, and then re-loaded and re-parsed as a web worker.

@kaizhu256
Copy link
Member Author

ok, closing this pull-request for now as WONTFIX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants