SimpleChatTC:DataStore: Initial skeleton of a Db WebWorker
Create the DB store Try Get and Set operations The post back to main thread done from asynchronous paths. NOTE: given that it has been ages since indexed db was used, so this is a logical implementation by refering to mdn as needed.
This commit is contained in:
parent
4f857575f5
commit
aedffe1df0
|
|
@ -0,0 +1,86 @@
|
|||
//@ts-check
|
||||
// STILL DANGER DANGER DANGER - Simple and Stupid - Use from a discardable VM only
|
||||
// Helpers to handle db related tool/function calling using web worker
|
||||
// by Humans for All
|
||||
//
|
||||
|
||||
/**
|
||||
* Expects to get a message with cid, tcid, (f)name and args
|
||||
* Posts message with cid, tcid, (f)name and data if any
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Allows the db connection to be openned.
|
||||
*/
|
||||
function db_open() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const dbConn = indexedDB.open('TCDB', 1);
|
||||
dbConn.onupgradeneeded = (ev) => {
|
||||
console.debug("DBUG:WWDb:Conn:Upgrade needed...")
|
||||
dbConn.result.createObjectStore('theDB');
|
||||
dbConn.result.onerror = (ev) => {
|
||||
console.debug(`DBUG:WWDb:Db:Op failed [${ev}]...`)
|
||||
}
|
||||
};
|
||||
dbConn.onsuccess = (ev) => {
|
||||
console.debug("DBUG:WWDb:Conn:Opened...")
|
||||
resolve(dbConn.result);
|
||||
}
|
||||
dbConn.onerror = (ev) => {
|
||||
console.debug(`DBUG:WWDb:Conn:Failed [${ev}]...`)
|
||||
reject(ev);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
self.onmessage = async function (ev) {
|
||||
try {
|
||||
console.info(`DBUG:WWDb:${ev.data.name}:OnMessage started...`)
|
||||
/** @type {IDBDatabase} */
|
||||
let db = await db_open();
|
||||
let dbTrans = db.transaction('theDB', 'readwrite');
|
||||
let dbOS = dbTrans.objectStore('theDB');
|
||||
let args = JSON.parse(ev.data.args);
|
||||
switch (ev.data.name) {
|
||||
case 'data_store_get':
|
||||
let reqGet = dbOS.get(args['key'])
|
||||
reqGet.onsuccess = (evGet) => {
|
||||
console.info(`DBUG:WWDb:${ev.data.name}:transact success`)
|
||||
self.postMessage({
|
||||
cid: ev.data.cid,
|
||||
tcid: ev.data.tcid,
|
||||
name: ev.data.name,
|
||||
data: { 'status': 'ok', 'data': reqGet.result, 'msg': `DataStoreGet:Ok:${args['key']}:${reqGet.result}`}
|
||||
});
|
||||
}
|
||||
break;
|
||||
case 'data_store_set':
|
||||
let reqSet = dbOS.add(args['value'], args['key']);
|
||||
reqSet.onsuccess = (evSet) => {
|
||||
console.info(`DBUG:WWDb:${ev.data.name}:transact success`)
|
||||
self.postMessage({
|
||||
cid: ev.data.cid,
|
||||
tcid: ev.data.tcid,
|
||||
name: ev.data.name,
|
||||
data: { 'status': 'ok', 'msg': `DataStoreSet:Ok:${args['key']}:${reqSet.result}`}
|
||||
});
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.info(`ERRR:WWDb:${ev.data.name}:OnMessage:Unknown func call...`)
|
||||
break;
|
||||
}
|
||||
console.info(`DBUG:WWDb:${ev.data.name}:OnMessage end`)
|
||||
} catch (/** @type {any} */error) {
|
||||
let errMsg = `\n\nTool/Function call "${ev.data.name}" raised an exception:${error.name}:${error.message}\n\n`;
|
||||
self.postMessage({
|
||||
cid: ev.data.cid,
|
||||
tcid: ev.data.tcid,
|
||||
name: ev.data.name,
|
||||
data: {'status': 'error', 'msg': errMsg}
|
||||
});
|
||||
console.info(`ERRR:WWDb:${ev.data.name}:OnMessage end:${error}`)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue