Add an SqliteWorker

This commit is contained in:
Alex Gleason 2023-11-15 16:22:57 -06:00
parent 7ed34a0906
commit 87980bbba1
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 74 additions and 0 deletions

5
src/workers.ts Normal file
View File

@ -0,0 +1,5 @@
import SqliteWorker from './workers/sqlite.ts';
const sqliteWorker = new SqliteWorker('./data/db.sqlite3');
export { sqliteWorker };

37
src/workers/sqlite.ts Normal file
View File

@ -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;

View File

@ -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]);
});