19 Commits

Author SHA1 Message Date
Florian Schroedl
59f9cacdb0 test: add multi-client sync flow tests
Add 8 end-to-end flow tests exercising full client-server sync scenarios:
- two-client convergence on same key
- delete propagation across clients
- update propagation across clients
- store persistence across client restarts
- fresh client pulling from server
- batch sync of 50 documents
- pending changes surviving server restart
- group isolation between different document groups
2026-04-16 20:36:30 +02:00
Florian Schroedl
b68f97247a refactor: rename library from pocketbook to atomsync 2026-04-16 20:18:45 +02:00
Florian Schroedl
bcf7e03332 docs: add AGENTS.md with async conventions and project structure 2026-04-16 20:06:50 +02:00
Florian Schroedl
973b079ae3 refactor: replace core.async with promesa for all async operations
- Store protocol now returns promesa promises instead of core.async channels
- MemoryStore: `(p/resolved val)` replaces chan+put!+close! ceremony
- IDBStore: `p/create` with resolve/reject wraps IDB callbacks
- sync.cljc: CLJ uses `p/vthread`, CLJS returns native Promise chains
- core.cljc: `p/let` replaces go blocks, timer-based sync loop replaces
  go-loop+alts!, debounced push replaces kick channel
- Tests use `deref` with timeout on promesa promises
- Todomvc example uses `p/let` instead of go/<!
2026-04-16 20:05:08 +02:00
Florian Schroedl
fffd934262 config(git): Ignore clj-kondo 2026-04-16 19:52:49 +02:00
Florian Schroedl
4510b6f736 fix: narrow CLJS watch path to avoid infinite recompile loop
Watch path `example/todomvc` included the `js/` output directory,
causing each compile to trigger another. Narrowed to
`example/todomvc/pocketbook` (source only).
2026-04-16 19:51:48 +02:00
Florian Schroedl
c971988ce9 refactor: use hiccup instead of string concatenation in todomvc
Replace hand-built HTML strings with hiccup vectors rendered by a
minimal hiccup→HTML converter (example/todomvc/pocketbook/hiccup.cljs).
Supports CSS-style selectors (:div.class#id), attribute maps, void
elements, and nested children.
2026-04-16 19:51:41 +02:00
Florian Schroedl
86b54e1291 refactor: extract shared .cljc library with store protocol
Move core, sync, and transit from platform-specific .clj/.cljs to
shared .cljc files with reader conditionals. This enables testing
the full sync logic on the JVM and using SyncedAtom from Clojure
clients.

Key changes:
- PStore protocol (store.cljc) decouples core from storage backend
- IDB store (store/idb.cljs) and memory store (store/memory.cljc)
- SyncedAtom implements CLJ IDeref/IAtom/IRef + CLJS equivalents
- Sync client uses java.net.http on CLJ, fetch on CLJS
- SSE remains CLJS-only; JVM clients use polling
- API change: store passed explicitly instead of pb/open
- 7 new JVM tests: local ops, persistence, watches, two-client sync
- 28 tests total, 87 assertions, all passing
2026-04-16 19:42:06 +02:00
Florian Schroedl
5ab102b550 refactor: remove auth system
Remove Bearer token auth, per-user group ACLs, and auth_test.clj.
The server now accepts all requests without authentication.
2026-04-16 19:17:46 +02:00
Florian Schroedl
06d0fa5e05 feat: instant sync — push on write, pull on reconnect, render from cache
- Add kick-ch to SyncedAtom: swap!/reset! trigger immediate push
  instead of waiting for the 15s sync loop interval
- SSE: pull on reconnect ("connected" message), not just on new events,
  so coming back online picks up missed server changes
- TodoMVC: render and bind events before IDB load completes; the watch
  re-renders automatically when cached data arrives
2026-04-16 19:09:08 +02:00
Florian Schroedl
38a15b7a34 fix: CLJS watch mode missing todomvc from cljs_deps.js
Two issues caused "Loading from local store..." after tmux restart:

1. Watch path only included `src/`, so `example/todomvc/` sources
   weren't compiled on the watcher's initial build, leaving
   pocketbook.todomvc out of cljs_deps.js.
   Fix: `-w "src:example/todomvc"`

2. Stale output dir from previous builds caused incremental compile
   to skip regenerating cljs_deps.js.
   Fix: `cljs:clean` task (deletes js output) runs before `cljs:watch`.
2026-04-04 18:38:22 +02:00
Florian Schroedl
9c3ecc7038 fix(sse): send plain string body for subsequent SSE events
http-kit's send! after the initial response expects a plain string,
not a full response map with status/headers. Sending a response map
was silently dropped, so SSE clients never received change
notifications after the initial "connected" message.

Verified working: toggling a todo in one tab updates the other tab
within ~30ms via SSE.
2026-04-04 18:34:10 +02:00
Florian Schroedl
7b0b320a76 fix: disable browser REPL preload that blocks script loading
The default ClojureScript build includes clojure.browser.repl.preload
which tries to connect to a REPL server on load. When no REPL is
running, this blocks the goog.require chain and the app never
initializes (stuck on "Loading from local store").

Set :preloads [] and :browser-repl false in build.edn.
2026-04-04 18:21:53 +02:00
Florian Schroedl
455e80f4e8 feat: add SSE live sync between clients
When any client pushes changes, the server broadcasts an SSE event to
all connected clients watching that group. Clients immediately pull
the latest data — no polling delay, no page reload.

Server (~30 lines):
- GET /events?group=G — SSE endpoint, holds connection open
- On successful POST /sync, notify all SSE listeners for affected groups
- Auth-aware: respects token and group permissions
- Auto-cleanup on disconnect via http-kit on-close

Client (~25 lines):
- EventSource connects to /events?group=G on sync loop start
- On SSE message, triggers do-pull! to fetch latest data
- Auto-reconnects (browser EventSource built-in behavior)
- Cleanup on destroy!

Periodic polling remains as fallback (30s default). SSE provides
sub-second sync for the common case.
2026-04-04 17:14:55 +02:00
Florian Schroedl
570a087f53 refactor: move TodoMVC example into example/ directory
- example/todomvc/pocketbook/todomvc.cljs — app source
- example/todomvc/todomvc.html — page
- example/todomvc/js/ — compiled output (gitignored)

Server static file serving is now filesystem-based via --static-dir
flag instead of classpath resources. No static dir by default (API
only); bb server passes --static-dir example/todomvc.

Removed resources/public/ — library has no bundled static assets.
2026-04-04 17:05:12 +02:00
Florian Schroedl
6f70fbfdbb fix: resolve IDB nil-on-channel and CLJS deref bugs
- idb/get-meta and idb/get-doc were calling (put! ch nil) when no
  result found, which is illegal in core.async. Now close the channel
  instead (reader gets nil from closed channel).
- todomvc.cljs used @(!todos) which calls the atom as a function.
  Fixed to @@!todos (deref atom → deref SyncedAtom).
2026-04-04 16:53:54 +02:00
Florian Schroedl
d5a0082b67 chore: add bb.edn task runner
Tasks: server, cljs, cljs:watch, test, dev (tmux), dev:stop, dev:restart
2026-04-04 16:48:29 +02:00
Florian Schroedl
ab68a21dd6 feat: add TodoMVC example with editorial design
Full TodoMVC implementation using Pocketbook's synced atom:
- Add, toggle, edit (double-click), destroy todos
- Filter: All / Active / Completed
- Toggle all, clear completed
- Live sync status indicator (online/offline/pending)
- Data persists in IndexedDB, syncs to server via Transit

Design: Ink & Paper aesthetic — cream paper background with grain
texture, Instrument Serif italic header, Newsreader body text,
DM Mono UI chrome, terracotta accent with green completion marks,
deckled left border on card.

Also:
- Server now serves static files from resources/public (/ → todomvc.html)
- Fix CLJS compilation: resolve close!/put! naming conflicts with
  core.async, use qualified async/close!, add clojure.string require
- Fix unbalanced parens in do-pull!
- Remove old placeholder example
2026-04-04 16:45:14 +02:00
Florian Schroedl
55cddf751b feat: implement Pocketbook — a Clojure-native synced atom
Offline-first key-value store with atom interface (swap!, deref,
add-watch) that syncs to a SQLite-backed server over Transit.

Server (CLJ):
- SQLite storage with Nippy serialization preserving all Clojure types
- GET /sync?group=G&since=T pull endpoint with prefix-based groups
- POST /sync push endpoint with per-document version checking
- Conflict detection (stale write rejection)
- Token-based auth with per-user group access
- CORS support, soft deletes, purge compaction

Client (CLJS):
- IndexedDB wrapper with Transit serialization
- SyncedAtom implementing IAtom (IDeref, ISwap, IReset, IWatchable)
- Write-through to IndexedDB on every swap!
- Background sync loop (pull + push) with configurable interval
- Online/offline detection with reconnect sync
- Conflict resolution (accept server value)
- ready? channel for initial load
- Custom cache atom support (Reagent ratom compatible)

25 tests, 77 assertions across db, transit, server, and auth.
2026-04-04 16:33:14 +02:00