Add an SqliteWorker
This commit is contained in:
parent
7ed34a0906
commit
87980bbba1
|
@ -0,0 +1,5 @@
|
|||
import SqliteWorker from './workers/sqlite.ts';
|
||||
|
||||
const sqliteWorker = new SqliteWorker('./data/db.sqlite3');
|
||||
|
||||
export { sqliteWorker };
|
|
@ -0,0 +1,37 @@
|
|||
class SqliteWorker {
|
||||
#path: string;
|
||||
#worker: Worker;
|
||||
|
||||
constructor(path: string) {
|
||||
this.#path = path;
|
||||
this.#worker = new Worker(new URL('./sqlite.worker.ts', import.meta.url).href, { type: 'module' });
|
||||
}
|
||||
|
||||
open(): Promise<void> {
|
||||
return this.#call(['open', [this.#path]]);
|
||||
}
|
||||
|
||||
query(sql: string, params?: any): Promise<unknown[]> {
|
||||
return this.#call(['query', [sql, params]]);
|
||||
}
|
||||
|
||||
#call<T>(msg: [string, unknown[]]): Promise<T> {
|
||||
const id = crypto.randomUUID();
|
||||
|
||||
this.#worker.postMessage([id, msg]);
|
||||
|
||||
// TODO: use a hashmap instead of an event listener for better performance.
|
||||
return new Promise((resolve) => {
|
||||
const handleEvent = (event: MessageEvent<[string, T]>) => {
|
||||
const [_id, result] = event.data;
|
||||
if (_id === id) {
|
||||
this.#worker.removeEventListener('message', handleEvent);
|
||||
resolve(result);
|
||||
}
|
||||
};
|
||||
this.#worker.addEventListener('message', handleEvent);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default SqliteWorker;
|
|
@ -0,0 +1,32 @@
|
|||
/// <reference lib="webworker" />
|
||||
|
||||
import { DenoSqlite3 } from '@/deps.ts';
|
||||
|
||||
let db: DenoSqlite3;
|
||||
|
||||
type Msg =
|
||||
| ['open', [string]]
|
||||
| ['query', [string, unknown[]]];
|
||||
|
||||
function call([cmd, args]: Msg) {
|
||||
switch(cmd) {
|
||||
case 'open':
|
||||
return handleOpen(args[0]);
|
||||
case 'query':
|
||||
return handleQuery(args[0], args[1]);
|
||||
}
|
||||
}
|
||||
|
||||
function handleOpen(path: string): void {
|
||||
db = new DenoSqlite3(path);
|
||||
}
|
||||
|
||||
function handleQuery(sql: string, params: any[] = []) {
|
||||
return db.prepare(sql).all(...params);
|
||||
}
|
||||
|
||||
self.addEventListener('message', (event: MessageEvent<[string, Msg]>) => {
|
||||
const [id, msg] = event.data;
|
||||
const result = call(msg);
|
||||
self.postMessage([id, result]);
|
||||
});
|
Loading…
Reference in New Issue