missionary 2025-10-16

Very basic question: ๐Ÿงต

Is it possible to have "mounting" be printed once here?

(def v (atom 0))
(def >v (m/signal (m/watch v)))

(def a
  (m/cp
    (if (< (m/?< >v) 3)
      (println "mounting")
      (println "unmounting"))))

(def cancel ((m/reduce {} {} a) {} {}))

(swap! v inc) ;; prints mounting
(swap! v inc) ;; prints mounting
(swap! v inc) ;; prints mounting
(swap! v inc) ;; prints unmounting

I only want side effects to evaluate if my conditional expression changes from true to false or false to true

x/y: I want to mount or unmount flow B based upon the value of flow A, and I don't want to pay for repeated mounting of flow B if the result of the conditional test doesn't change.

I feel like this is probably obvious and I'm sure electric does it, I just can't seem to figure it out right now.

the equality check is done by the switch, so you need an intermediate flow holding the conditional state

(def a
  (m/cp
    (if (m/?< (m/latest #(< % 3) >v))
      (println "mounting")
      (println "unmounting"))))

๐Ÿ™ 1

Ah, interesting. Thank you.