Fork me on GitHub
#reagent
<
2021-10-19
>
Fahd El Mazouni09:10:52

Hello ! Can anyone explain what's the purpose of the reaction argument in the Track constructor ? (it seems it always gets nil)

(deftype Track [f args ^:mutable reaction]
  IReactiveAtom
  IDeref
    (-deref [this]
      (if-some [r reaction]
        (-deref r)
        (cached-reaction #(apply f args) f args this nil)))
  IEquiv
    (-equiv [_ ^clj other]
      (and (instance? Track other) (= f (.-f other)) (= args (.-args other))))
  IHash
    (-hash [_] (hash [f args]))
  IPrintWithWriter
    (-pr-writer [a w opts] (pr-atom a w opts "Track" {:val (-deref a), :f f})))
(defn make-track [f args] (Track. f args nil))

lilactown15:10:54

in the -deref method, it will create a new reaction if one doesn’t already exist and store it on the reaction property of the object

Fahd El Mazouni06:10:43

ooooh yeah ! I see ! thanks

lilactown15:10:11

that’s what cached-reaction does