Brickheart

Handoff: worker and offline mode

: Written by a human with AI assistance.

Goal

Move sql.js and LDraw IndexedDB off the main thread. Make the app work with no network.

Data facts

  • LDraw packs: data/ldraw/subject-{1,2,3}.jsonl.gz. About 10.7 MB gzipped total. One JSON per line: {n, d, t, r?}.
  • Install already streams: fetch → DecompressionStream → TextDecoderStream → split lines → IndexedDB batches (500 rows or 4M chars). Left as is.
  • IndexedDB brickheart-ldraw has stores files, members, subjects. Files are shared across subjects.
  • DB rebrickable.db is 12 MB.

What changed

New src/db.worker.ts

  • One module worker. Owns sql.js (the only copy of the DB) and ldraw-store.ts.
  • Ops: init, query, isInstalled, listStems, install, closure, remove, cleanupInterrupted, cancel.
  • init gives the worker an absolute base URL. Relative paths inside a worker resolve against /js/, not the page.
  • Functions can't be posted, so the worker makes its own onProgress and AbortController.
  • Error name is passed through, so QuotaExceededError and AbortError survive.
  • Only one install runs at a time.

New src/db-client.ts

  • startDb(root) starts the worker. Await it before any other call.
  • call(op, args, {onProgress, signal}) is the RPC. run(Built) runs a query from query.ts.
  • A worker crash rejects all pending calls.

src/main.ts

  • Dropped sql.js. All queries go through run.
  • Render and setSubject are async. A renderSeq counter drops stale results.

src/build.ts

  • Dropped sql.js. query, isInstalled, install and closure go through call.
  • Install URL is made absolute with new URL(..., location.href).
  • installFileLoaderHook() and setOfflineFiles(files) stay on the main thread, because three's FileLoader lives there.
  • releaseGroups() is called after the wall loads. It is added in geometry.ts and clears groupCache. geomCache is kept.

build.mjs

  • src/db.worker.ts is in ENTRIES, and the sql-wasm scan finds the wasm for it.
  • Last block of the file builds the service worker:
    • walks dist
    • skips sw.js, _headers, _redirects, *.map and data/ldraw/
    • hashes the rest into a version
    • turns index.html into folder URLs (/en/build/)
    • writes dist/sw.js with replaceAll for the placeholders (the first version only replaced the first match, which broke it)

New src/sw.js (service worker)

  • Network-first. If the fetch fails, it serves the cache. Navigation uses ignoreSearch, so ?set=... hits the cached page.
  • Skips non-GET, Range requests, cross-origin requests and /data/ldraw/. Packs live in IndexedDB.
  • Cache name is app-<hash>. Old caches are deleted on activate. It uses skipWaiting and clients.claim.
  • Registered in main.ts and build.ts inside start().

CSP: no change. script-src 'self' 'wasm-unsafe-eval' covers the worker and the wasm. There is no worker-src, so it falls back to script-src.

Results

  • JS heap is 42 MB used for 172 distinct parts.
  • All 3 packs install and load from IndexedDB.
  • Service worker serves pages and assets offline.

Not done

  • Cancel: worker side exists, but no abort button in the UI and it has never been exercised.
  • getClosure: I suggested a version with a new transaction per level. I don't know if it was applied. The old one works today but depends on the transaction staying alive across awaits.
  • ldraw-store.ts header still says "Unverified: not run". It works in tests but has no automated tests.
  • Memory: if you want more cuts, look at toNonIndexed in getPartGeometry, and the per-brick LineSegments when drawEdges is on.