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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/reagent/ratom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,25 @@
reaction))


(defn make-reaction-notify [f & args]
(let [reaction (apply make-reaction f args)]
;; override _run method of Reaction type so that it calls f with the `notify` function
(set! (.-_run reaction) (fn [check]
(let [oldstate (.-state reaction)
notify (fn [res] (when-not (.-nocache? reaction)
(set! (.-state reaction) res)
;; Use = to determine equality from reactions, since
;; they are likely to produce new data structures.
(when-not (or (nil? (.-watches reaction))
(= oldstate res))
(notify-w reaction oldstate res))))
res (if check
(._try-capture reaction (partial f notify))
(deref-capture (partial f notify) reaction))]

res)))
reaction))


(def ^:private temp-reaction (make-reaction nil))

Expand Down