This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-05-13
Channels
- # announcements (34)
- # aws (1)
- # beginners (99)
- # boot (19)
- # calva (26)
- # cider (24)
- # cljdoc (8)
- # cljs-dev (29)
- # clojure (107)
- # clojure-dev (3)
- # clojure-europe (12)
- # clojure-finland (1)
- # clojure-italy (24)
- # clojure-nl (5)
- # clojure-spec (13)
- # clojure-sweden (3)
- # clojure-uk (36)
- # clojurescript (4)
- # community-development (14)
- # cursive (3)
- # data-science (6)
- # datascript (57)
- # figwheel-main (3)
- # fulcro (9)
- # graalvm (11)
- # hoplon (18)
- # jobs (1)
- # jobs-discuss (2)
- # joker (10)
- # leiningen (13)
- # off-topic (23)
- # other-languages (1)
- # pathom (24)
- # pedestal (5)
- # re-frame (6)
- # reagent (45)
- # reitit (3)
- # rewrite-clj (1)
- # spacemacs (2)
- # sql (23)
- # tools-deps (6)
- # vim (5)
Hope you don't find this too stupid to ask. I've been revising some re-frame docs and the more i read more i am curtain this is wrong 1)
(rf/reg-event-db
:abc
validate-spec
(fn [db [_]]
(ws/chsk-send! [:abc]
3000
(fn [reply]
(if (sente/cb-success? reply)
(rf/dispatch [:success])
(println "No reply for abc"))))
db))
2)
and (do (dispatch [1]) (dispatch [2]...
How can i change this?
for 2) it seems that using reg-event-fx
and dispatch-n
solves that issue
but what about the first?
Both of these work just fine but i would like to use re-frame properlyHere's one way you could convert it to use reg-event-fx
(rf/reg-event-fx
:abc
validate-spec
(fn [_ [event]]
{:dispatch [:ws-send [event]]}))
(rf/reg-event-fx
:ws-send
(fn [_ [_ v]]
(ws/chsk-send! v
3000
(fn [reply]
(if (sente/cb-success? reply)
(rf/dispatch [:success])
(println "No reply for " v))))))
You could also use an effect defined by reg-fx
as suggested below - I tend to refactor to reg-event-fx
first, then to reg-fx
later if it makes more sense, though it adds a bit of extra complexity when learning re-frame so it may be worth diving into it a little later.
The other thing I've realized is that nearly every time I use reg-event-db
, at some point later on I wish I had used reg-event-fx
- I don't know if this is the case for everyone though.
Sending message to a websocket is a (side)`effect`. In Re-Frame we aim to keep event-handlers free from side-effects. Side-effects can be extracted to reusable effects
. You can define effect handlers with reg-fx
. https://github.com/Day8/re-frame/blob/master/docs/Effects.md