feat: add production-grade Web Worker data layer#658
Draft
feat: add production-grade Web Worker data layer#658
Conversation
Agent-Logs-Url: https://github.com/arolariu/arolariu.ro/sessions/1cf624ba-6c0a-4ddc-9aaa-127809d69b6b Co-authored-by: arolariu <56928070+arolariu@users.noreply.github.com>
- src/lib/workerTypes.ts: shared discriminated-union types (WorkerRequest, WorkerResponse, WorkerSyncMessage, WorkerProcessor, PayloadFor<T>) - src/workers/coreWorker.ts: stateful worker with 3-tier storage (Map → Dexie/IndexedDB → BroadcastChannel cross-tab sync) and typed processor registry (FILTER_BY_PREFIX, COUNT, KEYS, CLEAR_ALL) - src/workers/client.ts: singleton WorkerClient with UUID-keyed pending Map, queueMicrotask-based onReady, and high-level get/set/delete/query/process API - src/hooks/useWorkerThread.ts: 'use client' hook with stable useCallback references, reactive ready flag, and SSR guard" Agent-Logs-Url: https://github.com/arolariu/arolariu.ro/sessions/1cf624ba-6c0a-4ddc-9aaa-127809d69b6b Co-authored-by: arolariu <56928070+arolariu@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
arolariu
April 19, 2026 06:23
View session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements a singleton, promise-based Web Worker data layer with cross-tab synchronisation, typed command dispatch, and a clean React hook API — no third-party abstractions.
Architecture
3-tier storage in the worker:
Map<string, unknown>: hot-path in-memory store, pre-warmed from IndexedDB on initarolariu-worker-state): durable persistence; written on SET/DELETE whenpersist=trueBroadcastChannel: fans out mutations to all other tabs; receivers update memory only (no re-persist, no re-broadcast → loop-safe)New files
src/lib/workerTypes.tsShared discriminated-union contract.
PayloadFor<T>andWorkerResultTypeMapgive end-to-end generic inference — noany.src/workers/coreWorker.tsStateful dedicated worker. Dispatch uses an async IIFE per message for full concurrency with per-request error isolation. Processor registry uses the Map constructor pattern (no eval, no dynamic code):
src/workers/client.tsSingleton
WorkerClient(private constructor +getInstance()). In-flight requests are tracked in aMap<uuid, {resolve, reject}>— safe for arbitrary parallelism. Discriminates the READY signal ondata.type === "READY"so TypeScript correctly narrows toWorkerResponsein the response path.onReady()usesqueueMicrotaskand returns an unsubscribe function.src/hooks/useWorkerThread.ts"use client"hook. All returned methods areuseCallback-stabilised (empty deps) — safe inuseEffectdependency arrays. Single reactivereadyflag.WorkerClient.getInstance()is called insideuseEffectonly, ensuring no SSR execution.