From fdc6645332b44bae2343049f363a4fa221a4d764 Mon Sep 17 00:00:00 2001 From: David Waring Date: Sun, 1 Mar 2020 16:28:41 -0500 Subject: [PATCH] DB: use wasm web worker --- src/utils/db.js | 31 +++----- static/js/{sql-wasm.js => worker.sql-wasm.js} | 74 +++++++++++++++++++ 2 files changed, 85 insertions(+), 20 deletions(-) rename static/js/{sql-wasm.js => worker.sql-wasm.js} (96%) diff --git a/src/utils/db.js b/src/utils/db.js index 18ccbb2..98843e6 100644 --- a/src/utils/db.js +++ b/src/utils/db.js @@ -3,14 +3,7 @@ const JSZip = require("jszip"); const api = require("@/utils/api.js"); const cache = require("@/utils/cache.js"); const store = require("@/utils/store.js"); - -const initSqlJs = require("@/../static/js/sql-wasm.js"); -const config = { - locateFile: function(filename) { - return '/js/' + filename; - } -} -const RightTrackDB = require("right-track-db-sqljs")(initSqlJs, config); +const RightTrackDB = require("right-track-db-sqljs")("/js/worker.sql-wasm.js"); let PREPPING_DB = false; let PREPPING_DB_CALLBACKS = []; @@ -172,19 +165,17 @@ function _uInt8ArraytoDB(agency, data, callback) { let config = rta.getConfig(); // Build the Right Track Database - let db = new RightTrackDB(config, data, function() { - - // Make test DB query - console.log("---> TESTING DB QUERY:"); - db.get("SELECT * FROM rt_about;", function(err, info) { - if ( err ) { - return callback(err); - } - console.log("LOADED RIGHT TRACK DATABASE:"); - console.log(info); - return callback(null, db); - }); + let db = new RightTrackDB(config, data); + // Make test DB query + console.log("---> TESTING DB QUERY:"); + db.get("SELECT * FROM rt_about;", function(err, info) { + if ( err ) { + return callback(err); + } + console.log("LOADED RIGHT TRACK DATABASE:"); + console.log(info); + return callback(null, db); }); }); diff --git a/static/js/sql-wasm.js b/static/js/worker.sql-wasm.js similarity index 96% rename from static/js/sql-wasm.js rename to static/js/worker.sql-wasm.js index 6b57034..56e42d8 100644 --- a/static/js/sql-wasm.js +++ b/static/js/worker.sql-wasm.js @@ -209,3 +209,77 @@ else if (typeof exports === 'object'){ exports["Module"] = initSqlJs; } +// Generated by CoffeeScript 1.12.7 +var createDb, db, sqlModuleReady; + +if (typeof importScripts === 'function') { + db = null; + createDb = function(data) { + if (db != null) { + db.close(); + } + return db = new SQL.Database(data); + }; + sqlModuleReady = initSqlJs(); + self.onmessage = function(event) { + return sqlModuleReady.then(function() { + var buff, callback, data, done, err, result; + data = event['data']; + switch (data != null ? data['action'] : void 0) { + case 'open': + buff = data['buffer']; + createDb((buff ? new Uint8Array(buff) : void 0)); + return postMessage({ + 'id': data['id'], + 'ready': true + }); + case 'exec': + if (db === null) { + createDb(); + } + if (!data['sql']) { + throw 'exec: Missing query string'; + } + return postMessage({ + 'id': data['id'], + 'results': db.exec(data['sql']) + }); + case 'each': + if (db === null) { + createDb(); + } + callback = function(row) { + return postMessage({ + 'id': data['id'], + 'row': row, + 'finished': false + }); + }; + done = function() { + return postMessage({ + 'id': data['id'], + 'finished': true + }); + }; + return db.each(data['sql'], data['params'], callback, done); + case 'export': + buff = db["export"](); + result = { + 'id': data['id'], + 'buffer': buff + }; + try { + return postMessage(result, [result]); + } catch (error) { + err = error; + return postMessage(result); + } + break; + case 'close': + return db != null ? db.close() : void 0; + default: + throw new 'Invalid action : ' + (data != null ? data['action'] : void 0); + } + }); + }; +}