Content-addressed computation: freeze → hash → CID → thaw
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Hash frozen SX to a content identifier (djb2 → hex). Same state always produces the same CID. Store by CID, retrieve by CID. - content-hash: djb2 hash of SX text → hex string - content-put/get: in-memory content store - freeze-to-cid: freeze scope → store → return CID - thaw-from-cid: look up CID → thaw signals - char-code-at / to-hex primitives for both platforms - Live demo: counter + name widget, content-address button, CID display, restore from CID input, CID history Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -478,6 +478,73 @@
|
||||
;; CEK Freeze / Thaw — serializable computation
|
||||
;; ---------------------------------------------------------------------------
|
||||
|
||||
(defisland ~geography/cek/content-address-demo ()
|
||||
(let ((count (signal 0))
|
||||
(name (signal "world"))
|
||||
(cid-display (signal ""))
|
||||
(cid-input (signal ""))
|
||||
(cid-history (signal (list)))
|
||||
(status (signal "")))
|
||||
;; Register in freeze scope
|
||||
(freeze-scope "ca-demo" (fn ()
|
||||
(freeze-signal "count" count)
|
||||
(freeze-signal "name" name)))
|
||||
(div (~cssx/tw :tokens "space-y-4")
|
||||
;; Interactive widget
|
||||
(div (~cssx/tw :tokens "flex gap-4 items-center")
|
||||
(div (~cssx/tw :tokens "flex items-center gap-2")
|
||||
(button :on-click (fn (e) (swap! count dec))
|
||||
(~cssx/tw :tokens "px-2 py-1 rounded bg-stone-200 text-stone-700 hover:bg-stone-300") "-")
|
||||
(span (~cssx/tw :tokens "font-mono text-lg font-bold w-8 text-center") (deref count))
|
||||
(button :on-click (fn (e) (swap! count inc))
|
||||
(~cssx/tw :tokens "px-2 py-1 rounded bg-stone-200 text-stone-700 hover:bg-stone-300") "+"))
|
||||
(input :type "text" :bind name
|
||||
(~cssx/tw :tokens "px-3 py-1 rounded border border-stone-300 font-mono text-sm")))
|
||||
;; Live output
|
||||
(div (~cssx/tw :tokens "rounded bg-violet-50 border border-violet-200 p-3 text-violet-800")
|
||||
(str "Hello, " (deref name) "! Count = " (deref count)))
|
||||
;; Content address button
|
||||
(div (~cssx/tw :tokens "flex gap-2")
|
||||
(button :on-click (fn (e)
|
||||
(let ((cid (freeze-to-cid "ca-demo")))
|
||||
(reset! cid-display cid)
|
||||
(reset! status (str "Stored as " cid))
|
||||
(swap! cid-history (fn (h) (append h (list cid))))))
|
||||
(~cssx/tw :tokens "px-3 py-1.5 rounded bg-stone-700 text-white text-sm hover:bg-stone-800")
|
||||
"Content-address"))
|
||||
;; CID display
|
||||
(when (not (empty? (deref cid-display)))
|
||||
(div (~cssx/tw :tokens "font-mono text-sm bg-stone-50 rounded p-2 flex items-center gap-2")
|
||||
(span (~cssx/tw :tokens "text-stone-400") "CID:")
|
||||
(span (~cssx/tw :tokens "text-violet-700 font-bold") (deref cid-display))))
|
||||
;; Status
|
||||
(when (not (empty? (deref status)))
|
||||
(div (~cssx/tw :tokens "text-xs text-emerald-600") (deref status)))
|
||||
;; Restore from CID
|
||||
(div (~cssx/tw :tokens "flex gap-2 items-end")
|
||||
(div (~cssx/tw :tokens "flex-1")
|
||||
(label (~cssx/tw :tokens "text-xs text-stone-400 block mb-1") "Restore from CID")
|
||||
(input :type "text" :bind cid-input
|
||||
(~cssx/tw :tokens "w-full px-3 py-1 rounded border border-stone-300 font-mono text-sm")))
|
||||
(button :on-click (fn (e)
|
||||
(if (thaw-from-cid (deref cid-input))
|
||||
(reset! status (str "Restored from " (deref cid-input)))
|
||||
(reset! status (str "CID not found: " (deref cid-input)))))
|
||||
(~cssx/tw :tokens "px-3 py-1.5 rounded bg-emerald-600 text-white text-sm hover:bg-emerald-700")
|
||||
"Restore"))
|
||||
;; CID history
|
||||
(when (not (empty? (deref cid-history)))
|
||||
(div (~cssx/tw :tokens "space-y-1")
|
||||
(label (~cssx/tw :tokens "text-xs text-stone-400 block") "History")
|
||||
(map (fn (cid)
|
||||
(button :on-click (fn (e)
|
||||
(if (thaw-from-cid cid)
|
||||
(reset! status (str "Restored from " cid))
|
||||
(reset! status (str "CID not found: " cid))))
|
||||
(~cssx/tw :tokens "block w-full text-left px-2 py-1 rounded bg-stone-50 hover:bg-stone-100 font-mono text-xs text-stone-600")
|
||||
cid))
|
||||
(deref cid-history)))))))
|
||||
|
||||
(defcomp ~geography/cek/cek-freeze-content ()
|
||||
(~docs/page :title "Freeze / Thaw"
|
||||
|
||||
@@ -525,6 +592,16 @@
|
||||
"The frozen SX appears below. Click Thaw to resume from the frozen state.")
|
||||
(~geography/cek/freeze-demo))
|
||||
|
||||
(~docs/section :title "Content addressing" :id "content-addressing"
|
||||
(p "Hash the frozen SX " (~docs/inline-code "\u2192") " content identifier. "
|
||||
"Same state always produces the same CID. Store by CID, retrieve by CID, verify by CID.")
|
||||
(~docs/code :code (highlight
|
||||
"(freeze-to-cid \"widget\")\n;; => \"d9eea67b\"\n\n(thaw-from-cid \"d9eea67b\")\n;; Signals restored. Same CID = same state."
|
||||
"lisp"))
|
||||
(p "Try it: change the values, click Content-address. Copy the CID. "
|
||||
"Change the values again. Paste the CID and Restore.")
|
||||
(~geography/cek/content-address-demo))
|
||||
|
||||
(~docs/section :title "What this enables" :id "enables"
|
||||
(ul :class "list-disc pl-6 mb-4 space-y-2 text-stone-600"
|
||||
(li (strong "Persistence") " \u2014 save reactive island state to localStorage, "
|
||||
|
||||
Reference in New Issue
Block a user