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.
This commit is contained in:
32
src/pocketbook/transit.clj
Normal file
32
src/pocketbook/transit.clj
Normal file
@@ -0,0 +1,32 @@
|
||||
(ns pocketbook.transit
|
||||
"Transit encoding/decoding helpers for the HTTP wire format."
|
||||
(:require [cognitect.transit :as t])
|
||||
(:import [java.io ByteArrayInputStream ByteArrayOutputStream]))
|
||||
|
||||
(defn encode
|
||||
"Encode a Clojure value to a Transit+JSON byte array."
|
||||
[v]
|
||||
(let [out (ByteArrayOutputStream. 4096)
|
||||
w (t/writer out :json)]
|
||||
(t/write w v)
|
||||
(.toByteArray out)))
|
||||
|
||||
(defn encode-str
|
||||
"Encode a Clojure value to a Transit+JSON string."
|
||||
[v]
|
||||
(let [out (ByteArrayOutputStream. 4096)
|
||||
w (t/writer out :json)]
|
||||
(t/write w v)
|
||||
(.toString out "UTF-8")))
|
||||
|
||||
(defn decode
|
||||
"Decode a Transit+JSON byte array or input stream to a Clojure value."
|
||||
[input]
|
||||
(let [in (cond
|
||||
(instance? ByteArrayInputStream input) input
|
||||
(instance? java.io.InputStream input) input
|
||||
(bytes? input) (ByteArrayInputStream. input)
|
||||
(string? input) (ByteArrayInputStream. (.getBytes ^String input "UTF-8"))
|
||||
:else (throw (ex-info "Cannot decode, unsupported input type"
|
||||
{:type (type input)})))]
|
||||
(t/read (t/reader in :json))))
|
||||
Reference in New Issue
Block a user