Skip to content

Commit 695e0f2

Browse files
committed
[electric] flow stats POC (live, done, transfers)
1 parent 5044e33 commit 695e0f2

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/hyperfiddle/electric/impl/missionary_util.cljc

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,37 @@
172172

173173
(def -stall-ms 2000)
174174

175+
(def !stats (atom {:live 0, :done 0, :transfers 0}))
176+
177+
(def gather-stats
178+
[:gather-stats nil
179+
(fn [_ evt]
180+
(swap! !stats
181+
(fn [stats]
182+
(case (:event evt)
183+
(:spawned) (update stats :live inc)
184+
(:done) (-> stats (update :done inc) (update :live dec))
185+
(:transferred) (update stats :transfers inc)
186+
#_else stats))))])
187+
188+
#?(:cljs (defn format-stats [{:keys [live done transfers]}]
189+
(str "Live: " live "\nDone: " done "\nTransfers: " transfers)))
190+
191+
#?(:cljs
192+
(defn insert-stats! [!stats node]
193+
(m/observe
194+
(fn [!]
195+
(let [txt (.createTextNode js/document "")]
196+
(! nil)
197+
(.appendChild node txt)
198+
(.setInterval js/window #(set! (.-textContent txt) (format-stats @!stats)) 1000))))))
199+
200+
;; circular dependency, inline at usage site
201+
;; (e/defn FlowStatsMonitor [] (e/client (dom/pre (e/input (insert-stats! !stats dom/node)))))
202+
175203
(def uninitialized-checks [#_(log prn) step-cannot-throw done-cannot-throw cancel-cannot-throw init-cannot-throw
176204
step-after-done step-after-throw double-step double-transfer
177-
done-twice step-in-exceptional-transfer
205+
done-twice step-in-exceptional-transfer #_gather-stats
178206
#_(flow-transfer-stalled -stall-ms println) ; clogs the clock, revisit after optimizations
179207
(flow-cancellation-stalled -stall-ms println)]) ; experimental, not protocol violations
180208
(def initialized-checks (conj uninitialized-checks initialized))

0 commit comments

Comments
 (0)