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

Please add :first-dispatch-n #14

Open
jeaye opened this issue Dec 21, 2017 · 1 comment
Open

Please add :first-dispatch-n #14

jeaye opened this issue Dec 21, 2017 · 1 comment

Comments

@jeaye
Copy link

jeaye commented Dec 21, 2017

I have implemented this in re-frame-rules, a fork re-frame-async-flow-fx which tackles the problem of state tracking and cyclical flows by providing a declarative, non-stateful approach to binding rules. My goal is to get as much of these small improvements back into re-frame-async-flow-fx so we don't have forks lying around.

A new fn get-dispatch-n, as shown here: https://github.com/jeaye/re-frame-rules/blob/36811b3dfc3f56079209ed0f9341cb99fa79fa36/src/com/jeaye/re_frame_rules.cljs#L80-L87

Used in two places:

  1. massage-rule: https://github.com/jeaye/re-frame-rules/blob/36811b3dfc3f56079209ed0f9341cb99fa79fa36/src/com/jeaye/re_frame_rules.cljs#L99

  2. make-rules-event-handler: https://github.com/jeaye/re-frame-rules/blob/36811b3dfc3f56079209ed0f9341cb99fa79fa36/src/com/jeaye/re_frame_rules.cljs#L127

The use case is, in case it isn't obvious, for firing off a number of events and then handling their responses as they come in. This is much more useful in a non-stateful rule set, since you're just saying "When this happens, do this" and when it happens doesn't matter. Real-world example:

(def initialize-rules
  {:id ::initialize
   :first-dispatch-n [[::subscribe-for-refreshes]
                      [::do-refresh-token]
                      [::do-find-notification]]
   :rules [{:when :seen-any?
            :events [::no-notification-found
                     ::notification-found]
            :dispatch [::success-initialize]
            :unbind? true}
           {:when :seen-any?
            :events [::fail-refresh-token
                     ::fail-find-notification]
            :dispatch [::fail-initialize]
            :unbind? true}]})

(reg-spec-event-fx
  ::do-initialize
  [db ::d/app-db]
   {::rules/bind initialize-rules})

This would also be an indirect solution to #12.

@bhb
Copy link
Contributor

bhb commented Jan 4, 2018

👍

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

No branches or pull requests

2 participants