2020-11-26 16:29:10 +11:00
|
|
|
import * as Comlink from "comlink";
|
|
|
|
|
|
|
|
|
|
import { getDatabase } from "../database";
|
|
|
|
|
|
|
|
|
|
// Worker to load large amounts of database data on a separate thread
|
|
|
|
|
let obj = {
|
2021-01-22 14:59:05 +11:00
|
|
|
data: null,
|
|
|
|
|
/**
|
|
|
|
|
* Load either a whole table or individual item from the DB
|
|
|
|
|
* @param {string} table Table to load from
|
|
|
|
|
* @param {string|undefined} key Optional database key to load, if undefined whole table will be loaded
|
|
|
|
|
*/
|
|
|
|
|
async loadData(table, key) {
|
2020-11-26 17:08:09 +11:00
|
|
|
try {
|
|
|
|
|
let db = getDatabase({});
|
2021-01-22 14:59:05 +11:00
|
|
|
if (key) {
|
|
|
|
|
// Load specific item
|
|
|
|
|
this.data = await db.table(table).get(key);
|
|
|
|
|
} else {
|
|
|
|
|
// Load entire table
|
|
|
|
|
this.data = [];
|
|
|
|
|
// Use a cursor instead of toArray to prevent IPC max size error
|
|
|
|
|
await db.table(table).each((item) => this.data.push(item));
|
|
|
|
|
}
|
2020-11-26 17:08:09 +11:00
|
|
|
} catch {}
|
2020-11-26 16:29:10 +11:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Comlink.expose(obj);
|