Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature proposal: add reaction that allows the user to explicitly notify watchers #263

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

grav
Copy link
Contributor

@grav grav commented Sep 20, 2016

This pull request is an extension to the api reagent.ratom/make-reaction-notify, that allows creation of reactions that allow for explicit notification of watchers.

This way, it is possible to create a reaction that for instance wraps asynchronicity:

(def input (r/atom 42))

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

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

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

Here, the squareroot-of-input reaction will fire when input changes, and then notify the result reaction when the xhr request is done.

It's possible to call notify several times in a function. It's not a new feature for FRP libraries (I've used it in RxJS and Reactive Cocoa), I just couldn't find anything similar in Reagent.

The implementation is very crude, so it's more a kind of proposal for a feature than a final implementation.

@grav grav changed the title Feature proposal: add reaction that allows the user to explicitly notify users Feature proposal: add reaction that allows the user to explicitly notify watchers Sep 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant