Handoff: worker and offline mode
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-ldrawhas storesfiles,members,subjects. Files are shared across subjects. - DB
rebrickable.dbis 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. initgives 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
onProgressandAbortController. - Error
nameis passed through, soQuotaExceededErrorandAbortErrorsurvive. - 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 fromquery.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
renderSeqcounter drops stale results.
src/build.ts
- Dropped sql.js.
query,isInstalled,installandclosurego throughcall. - Install URL is made absolute with
new URL(..., location.href). installFileLoaderHook()andsetOfflineFiles(files)stay on the main thread, because three'sFileLoaderlives there.releaseGroups()is called after the wall loads. It is added ingeometry.tsand clearsgroupCache.geomCacheis kept.
build.mjs
src/db.worker.tsis inENTRIES, 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,*.mapanddata/ldraw/ - hashes the rest into a version
- turns
index.htmlinto folder URLs (/en/build/) - writes
dist/sw.jswithreplaceAllfor the placeholders (the first version only replaced the first match, which broke it)
- walks
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 usesskipWaitingandclients.claim. - Registered in
main.tsandbuild.tsinsidestart().
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.tsheader still says "Unverified: not run". It works in tests but has no automated tests.- Memory: if you want more cuts, look at
toNonIndexedingetPartGeometry, and the per-brickLineSegmentswhendrawEdgesis on.