missionary object lifecycle

{:deps {org.clojure/clojure {:mvn/version "1.11.1"}
        missionary/missionary {:mvn/version "b.27-SNAPSHOT"}
        com.hyperfiddle/rcf {:mvn/version "20220902-130636"}
        compliment/compliment {:mvn/version "0.3.9"}}}
Extensible Data Notation
(require
  '[hyperfiddle.rcf :as rcf :refer [tests ! % with]]
  '[missionary.core :as m])
(hyperfiddle.rcf/enable!)
8.5s

(defn ConstantObject
  "build a continuous flow that emits a constant on construction, and then remains constant until
  cancelled"
  [x trace!]
  (->> (m/observe
         (fn constructor [emit!]
           (trace! ::mount)
           (emit! x)
           (fn destructor [] (trace! ::unmount))))
      (m/relieve {})
      (m/latest identity)))
0.1s
(tests "object lifecycle"
  (def !x (atom 0))
  (def <x (m/cp (let [x (m/?< (m/watch !x))]
                  (if (even? x)
                    ; object is destroyed when the if switches, and then recreated next time
                    (m/?< (ConstantObject x !))
                    ::off))))
  (def it (<x #(! ::notify) #(! ::terminate)))
  % := ::notify
  @it := 0
  % := ::mount
  (swap! !x inc)
  % := ::notify
  @it := ::off
  % := ::unmount
  (swap! !x inc)
  % := ::notify
  @it := 2
  % := ::mount
  (it)
  % := ::notify
  @it thrown? missionary.Cancelled
  % := ::unmount
  % := ::terminate)
1.5s
Runtimes (1)