Skip to content

Reaction with explicit / possibly async notify when new value is available #616

@Deraen

Description

@Deraen

Copying from: #263

(def input (r/atom 42))

(def squareroot-of-input
  (reagent.ratom/make-reaction-notify
    (fn [notify]
      (-> (js/fetch (str "http://heavymath.com/squareroot/" @input))
          (.json)
          (.then (fn [res] (notify res))))))

(def result (reaction (* 2 @squareroot-of-input)))

(defn view []
  [:p (str "The double square-root of " @input " is " @result)])

The idea here is that explicitly updating the reaction result with an fn call allows the update to happen later, async sometime after the reaction is triggered.

Notify could be called multiple times per one trigger?

Potential problems

If notify can be called multiple times, how do we prevent callbacks from previously "started" "side effects " from being triggered?
- Input value is 1 and starts a process that will notify reaction every x seconds
- If the input changes to 2, would we need to stop the first process?
- This might hint that the reaction model is at a low level for this kind of feature.

Other solutions?

Consider overlap and is this still needed?

  • React hooks have also become available since the original proposal was created in 2016.
  • Re-frame?
  • Check examples from JS FRP libs how they do this

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions