Skip to content

Instantly share code, notes, and snippets.

@dustingetz
Last active March 5, 2023 17:45
Show Gist options
  • Save dustingetz/13c99420fe9bf75dd8178c1a633d3bbe to your computer and use it in GitHub Desktop.
Save dustingetz/13c99420fe9bf75dd8178c1a633d3bbe to your computer and use it in GitHub Desktop.
Two clocks – Electric Clojure

Two clocks – Electric Clojure

  • demonstrates streaming lexical scope that crosses frontend/backend seamlessly
20230224.two.clocks.mp4

Code notes:

  • server and client clocks s and c are defined in the same expression
  • (- s c) shows local and remote values are consumed in the same expression
  • Lexical bindings c and s are reactive values
  • Clojure itself is made reactive by e/defn which compiles Clojure forms to DAGs
  • everything is reactive, so it feels like nothing is reactive
  • Lexical bindings like c can be thought of as either a flow and a value, both worldviews are valid, sometimes you need one sometimes the other
  • the latency can skew negative because the client and server sampling rates are not synchronized. Note it doesn’t drift.
(ns user.demo-6-two-clocks
#?(:cljs (:require-macros user.demo-6-two-clocks))
(:require [hyperfiddle.electric :as e]
[hyperfiddle.electric-dom2 :as dom]))
(e/defn App []
(e/client
(dom/h1 (dom/text "Two Clocks"))
(let [c (e/client e/system-time-ms)
s (e/server e/system-time-ms)]
(dom/div (dom/text "client time: " c))
(dom/div (dom/text "server time: " s))
(dom/div (dom/text "latency: " (- s c))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment