Skip to content

bronifty/web-workers-observable-wasm-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQLite WASM in a Web Worker

  • currently the setup in "opfs-vite" is working with the todos app (opfs works), so just piling on that with a bunch of scripts testing things.

Links

  • opfs doesn't work in a SharedWorker; wa-sqlite demo has a workaround wa-sqlite opfs
  • rxdb opfs has something too rxdb opfs

Vite Config for OPFS

// vite.config.js
import { defineConfig } from "vite";

export default defineConfig({
  server: {
    headers: {
      "Cross-Origin-Opener-Policy": "same-origin",
      "Cross-Origin-Embedder-Policy": "require-corp",
    },
  },
  optimizeDeps: {
    exclude: ["@sqlite.org/sqlite-wasm"],
  },
});

Worker API vs Shared Worker API Reference

Worker API

// main thread using a worker

const worker = new Worker("worker.mjs", { type: "module" });
const log = console.log;

log("worker.postMessage hello world");
worker.postMessage({
  type: "hello",
  payload: "world",
});

worker.onmessage = (e) => {
  log("worker.onmessage", JSON.stringify(e.data, null, 2));
  const { type, payload } = e.data;
  if (type === "log") {
    console.log(payload);
  }
};
// worker
const log = console.log;
self.onmessage = (e) => {
  log("worker.onmessage");
  const { type, payload } = e.data;
  if (type === "init") {
    log("worker.postMessage type log payload worker initialized");
    self.postMessage({ type: "log", payload: "worker initialized" });
  }
};

Shared Worker API

// main thread using a shared worker

const worker = new SharedWorker("worker.mjs", { type: "module" });

worker.port.postMessage({ type: "init" });

worker.port.onmessage = (e) => {
  const { type, payload } = e.data;
  if (type === "log") {
    console.log(payload);
  }
};
// shared worker

onconnect = (e) => {
  const port = e.ports[0];

  port.onmessage = (e) => {
    const { type, payload } = e.data;
    if (type === "log") {
      console.log(payload);
    }
  };
  port.postMessage({ type: "init" });
};

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published