Fork me on GitHub

what is the difference between using a reactor and reduce ap to a function and firing it?

(def atm_ (atom 0))

(def s1 (atom 0))
(def s2 (atom 0))

(def a1 (m/ap (let [v (m/?< (m/watch atm_))] (reset! s1 v))))
(def r1 (m/reactor (m/stream! (m/ap (let [v (m/?< (m/watch atm_))] (reset! s2 v))))))

((m/reduce conj a1) prn prn)
(r1 prn prn)
(swap! atm_ inc)
;; => 1
;; => 1


in this case no difference, ap is consumed as fast as possible


use reactor when you need to share flows




stream! is normally used when you need to dispatch the values of a single flow to several listeners. The backpressure is then collected from the listeners and propagated upstream. In this example, nobody is listening to the stream, therefore there's no backpressure so the flow is consumed at maximal speed, as per reduce.