-
Notifications
You must be signed in to change notification settings - Fork 29
Continuous flows
Léo NOEL edited this page Nov 17, 2021
·
1 revision
Use watch
.
(def !input (atom 0))
(def input-value (m/watch !input))
Use reductions
with appropriate seed and reducing function, and relieve
with {}
to discard oldest values.
(def click-events
(m/observe
(fn [!]
(.addEventListener js/window "click" !)
#(.removeEventListener js/window "click" !))))
(def click-count
(->> click-events
(m/reductions (fn [r _] (inc r)) 0)
(m/relieve {})))
Use latest
.
(m/latest vector input-value click-count)
Flows don't implement deref
, but you can derive another flow to extract the value at subscription time.
(def current-click-count (m/eduction (take 1) click-count))
Make sure sharing happens in a reactor
context and use signal!
.
(m/reactor
(let [>x (m/signal! input-value)
>twice-x (m/signal! (m/latest + >x >x))]
,,,
))
Pass the flow to any operator expecting a discrete flow.
(m/reduce (fn [_ c] (println (str "clicked " c " times."))) nil click-count)