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

dispatch-later doesn't allow dispatch-n #554

Closed
dijonkitchen opened this issue Oct 1, 2019 · 4 comments
Closed

dispatch-later doesn't allow dispatch-n #554

dijonkitchen opened this issue Oct 1, 2019 · 4 comments
Assignees

Comments

@dijonkitchen
Copy link
Contributor

Seems like non-idiomatic to have dispatch-n for multiple event dispatches, but not allow it in a dispatch-later. Instead, it's a vector of multiple maps that can each have their own millisecond delays: https://github.com/Day8/re-frame/blob/master/docs/API.md#built-in-effect-handlers

@danielcompton
Copy link
Contributor

There’s a large number of possible APIs that could go into re-frame. dispatch-later is generally pretty niche and already supports multiple dispatches. I’m not sure if there would be enough usage to have a separate dispatch-n-later API? You could always write this effect handler yourself, or write a function which created the dispatch-later style effect, but you give it a list of events, and a time. Maybe there are other use-cases I’m not thinking of though?

@dijonkitchen
Copy link
Contributor Author

It was just surprising that dispatch-n wasn't supported inside dispatch-later when it is used at the top level.

@smahood
Copy link
Contributor

smahood commented Jan 6, 2020

This reads like a slight miscommunication to me, I read the original issue as wanting to be able to use something like

{:dispatch-later [(when (> 3 5) {:ms 200 :dispatch [:conditioned-out]})
                  {:ms 100 :dispatch-n [[:another-one] [:another-one-two]]}]}

If I'm correct, here's a new effect that should cover that use case.

(rf/reg-fx
  :dispatch-later
  (fn [value]
    (doseq [{:keys [ms dispatch dispatch-n] :as effect} (remove nil? value)]
      (cond
        (not (number? ms))
        (console :error "re-frame: ignoring bad :dispatch-later value:" effect)

        (not (empty? dispatch))
        (set-timeout! #(router/dispatch dispatch) ms)

        (not (empty? dispatch-n))
        (if-not (sequential? dispatch-n)
          (console :error "re-frame: ignoring bad :dispatch-n value. Expected a collection, but got:" value)
          (doseq [event (remove nil? dispatch-n)] (set-timeout! #(router/dispatch event) ms)))

        :else
        (console :error "re-frame: ignoring bad :dispatch-later value:" effect)))))

Happy to make a PR if this is a desirable change to the built in :dispatch-later effect.

@superstructor
Copy link
Contributor

@danielcompton’s earlier comment correctly explains that there are many possible effects or combinations of effects. The standard ones don’t try to cover every case. Instead, they are intended to handle the common cases and their implementation shows how easy it is for you to create your own.

If your needs are slightly more exotic, I would suggest writing your own effect handler. Such effect handlers can typically be achieved in, maybe, 10 lines of code, and your custom version would provide you with exactly the combination of features you need.

Closing.

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

4 participants