diff options
author | stephan <stephan@noemail.net> | 2022-08-24 05:59:23 +0000 |
---|---|---|
committer | stephan <stephan@noemail.net> | 2022-08-24 05:59:23 +0000 |
commit | 9a34509a06ad893ae3ac786363ebf8d29b3e3a7c (patch) | |
tree | 45fe8feae7e32d3a5db59c969857486de728d168 /ext/wasm/api/sqlite3-api-worker1.js | |
parent | efeee19a958b905cc8e939e54b2959089bb89108 (diff) | |
download | sqlite-9a34509a06ad893ae3ac786363ebf8d29b3e3a7c.tar.gz sqlite-9a34509a06ad893ae3ac786363ebf8d29b3e3a7c.zip |
More work on how to configure the sqlite3 JS API bootstrapping process from higher-level code. Initial version of sqlite3-worker1-promiser, a Promise-based proxy for the Worker API #1.
FossilOrigin-Name: b030f321bd5a38cdd5d6f6735f201afa62d30d2b0ba02e67f055b4895553a878
Diffstat (limited to 'ext/wasm/api/sqlite3-api-worker1.js')
-rw-r--r-- | ext/wasm/api/sqlite3-api-worker1.js | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/ext/wasm/api/sqlite3-api-worker1.js b/ext/wasm/api/sqlite3-api-worker1.js index 2e8d5a25d..39263a4ab 100644 --- a/ext/wasm/api/sqlite3-api-worker1.js +++ b/ext/wasm/api/sqlite3-api-worker1.js @@ -92,11 +92,8 @@ sqlite3.initWorker1API = function(){ defaultDb: undefined, idSeq: 0, idMap: new WeakMap, - open: function(arg){ - // TODO? if arg is a filename, look for a db in this.dbs with the - // same filename and close/reopen it (or just pass it back as is?). - if(!arg && this.defaultDb) return this.defaultDb; - const db = (Array.isArray(arg) ? new DB(...arg) : new DB(arg)); + open: function(opt){ + const db = new DB(opt.filename); this.dbs[getDbId(db)] = db; if(!this.defaultDb) this.defaultDb = db; return db; @@ -169,14 +166,26 @@ sqlite3.initWorker1API = function(){ envelope to other calls in this API to tell them which db to use. If it is not provided to future calls, they will default to operating on the first-opened db. + + persistent: prepend sqlite3.capi.sqlite3_web_persistent_dir() + to the given filename so that it is stored + in persistent storage _if_ the environment supports it. + If persistent storage is not supported, the filename + is used as-is. } */ open: function(ev){ - const oargs = [], args = (ev.args || {}); + const oargs = Object.create(null), args = (ev.args || Object.create(null)); if(args.simulateError){ // undocumented internal testing option toss("Throwing because of simulateError flag."); } - if(args.filename) oargs.push(args.filename); + if(args.persistent && args.filename){ + oargs.filaname = sqlite3.capi.sqlite3_web_persistent_dir() + args.filename; + }else if('' === args.filename){ + oargs.filename = args.filename; + }else{ + oargs.filename = args.filename || ':memory:'; + } const db = wState.open(oargs); return { filename: db.filename, @@ -184,15 +193,15 @@ sqlite3.initWorker1API = function(){ }; }, /** - Proxy for DB.close(). ev.args may either be a boolean or an - object with an `unlink` property. If that value is truthy then - the db file (if the db is currently open) will be unlinked from - the virtual filesystem, else it will be kept intact. The - result object is: + Proxy for DB.close(). ev.args may be elided or an object with + an `unlink` property. If that value is truthy then the db file + (if the db is currently open) will be unlinked from the virtual + filesystem, else it will be kept intact. The result object is: { filename: db filename _if_ the db is opened when this is called, else the undefined value + dbId: the ID of the closed b, or undefined if none is closed } It does not error if the given db is already closed or no db is @@ -356,6 +365,7 @@ sqlite3.initWorker1API = function(){ dbId: DB handle ID, [messageId: if set in the inbound message], result: { + operation: "inbound message's 'type' value", message: error string, errorClass: class name of the error type, input: ev.data @@ -378,6 +388,7 @@ sqlite3.initWorker1API = function(){ }catch(err){ evType = 'error'; result = { + operation: ev.type, message: err.message, errorClass: err.name, input: ev @@ -405,7 +416,7 @@ sqlite3.initWorker1API = function(){ result: result }, wMsgHandler.xfer); }; - setTimeout(()=>self.postMessage({type:'sqlite3-api',result:'worker1-ready'}), 0); + self.postMessage({type:'sqlite3-api',result:'worker1-ready'}); }.bind({self, sqlite3}); }); |