Fork me on GitHub
#missionary
<
2021-11-13
>
ribelo18:11:57

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)
@s1
;; => 1
@s2
;; => 1

leonoel19:11:20

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

leonoel19:11:44

use reactor when you need to share flows

ribelo19:11:19

:thinking_face:

leonoel19:11:26

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.