Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created June 26, 2022 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ciantic/aef34a58365d70eb5a9cb1d61c2040f9 to your computer and use it in GitHub Desktop.
Save Ciantic/aef34a58365d70eb5a9cb1d61c2040f9 to your computer and use it in GitHub Desktop.
SQLite worker snippet
const worker = new Worker("https://cdn.jsdelivr.net/npm/sql.js@1.7.0/dist/worker.sql-wasm.js");
let rpcid = 0;
async function query(sql: string, params: any, action = "exec") {
const id = rpcid++;
return new Promise<any>((res, rej) => {
const listener = (e: MessageEvent<any>) => {
if (e.data && e.data.id === id) {
worker.removeEventListener("message", listener);
res(e.data);
}
};
worker.addEventListener("message", listener);
worker.postMessage({
id,
action: "exec",
sql,
params,
});
});
}
worker.onerror = (e) => console.log("Worker error: ", e);
console.log("OPEN", await query("", null, "open"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment