This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-08-24
Channels
- # announcements (3)
- # beginners (128)
- # boot (2)
- # braveandtrue (97)
- # calva (13)
- # cider (4)
- # cljdoc (12)
- # cljs-dev (16)
- # clojure (78)
- # clojure-germany (8)
- # clojure-italy (5)
- # clojure-nl (1)
- # clojure-spec (59)
- # clojure-uk (29)
- # clojurescript (46)
- # core-async (9)
- # cursive (63)
- # data-science (3)
- # datomic (22)
- # devcards (1)
- # duct (7)
- # emacs (4)
- # flambo (2)
- # fulcro (37)
- # instaparse (6)
- # jobs-discuss (38)
- # juxt (1)
- # off-topic (35)
- # om-next (1)
- # parinfer (7)
- # re-frame (14)
- # reagent (6)
- # reitit (21)
- # rum (1)
- # shadow-cljs (74)
- # spacemacs (8)
- # specter (1)
- # sql (3)
- # testing (2)
- # unrepl (2)
- # yada (6)
Is there a good way of intercepting every dispatch
to tack on some data (or metadata)? I'm thinking of tying each event I dispatch
to the current user id and wrapping my own reg-event-db
and reg-event-fx
to ensure the event is handled only if the user id still matches. I've been seeing a few manifestations of the same issue: the user logs out and various things (from :dispatch-later
to HTTP responses to Firebase callbacks) end up triggering, for the first user, when I log in as a new user.
I could just use my own dispatch
, but then I need to worry about accidentally using re-frame's. I'm hoping there's a more supported way, but I haven't found anything in the docs or issues for it. Seems like I need to either define my own wrapper dispatch
or redefine re-frame's.
the reframe core is rather approachable. you could also just write your own dispatcher fn and ignore the internals. that dispatcher handles metadata collection, then calls reframe/dispatch. imo you should always be using {your-domain}.re_frame namespace as a lib wrapper anyhow, for reasons like this (and others).
@jeaye I think the recommended way of intercepting every dispatch would be something like this: https://github.com/Day8/re-frame/blob/master/docs/FAQs/GlobalInterceptors.md
@jeaye @gadfly361 or create a function called emit
which does what you need to the event argument, then calls dispatch
And then you use emit
everywhere
Perhaps you'd also need to write your own effect handler for :emit
(instead of :dispatch
). Maybe.
Hi again, a friendly bump of my question: What's the best way to deal with async API call and coeffects? I'm trying to write something similar to https://github.com/gothinkster/clojurescript-reframe-realworld-example-app/blob/master/src/conduit/db.cljs#L44-L50 ... in this code, getting a logged user from localStorage
involves a synchronous api call, we can just assoc
... In my case, I'm trying to get a user session via AWS Cognito JS SDK, that is async ... https://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-user-identity-pools-javascript-examples.html#using-amazon-cognito-identity-user-pools-javascript-example-retrieving-current-user
my understanding is that the interceptor will be executed before a handler is called, but the AWS Cognito code will set the value async, via a callback, since it also attempts to refresh the session if the token is invalid (an HTTP call to a service) ... could it be the case that my handler code gets executed without a session because the callback has not been called?
@U0GTGHX1D: Not having used the AWS Cognito JS SDK myself, your concern is nevertheless valid. The entire event chain is processed synchronously so the callback may not have had a chance to be called.
Rather than co-effects, a general approach for callbacks might be:
Event #1 produces an effect that creates the callback (ie: .getSession
). In the callback, dispatch event #2 with whatever data is available in the callback context.
So specifically, something like:
(f/disaptch [:with-user [:event arg1 arg2]])
(f/reg-fx :with-user
(fn [event]
(.getSession user-pool
(fn [err session]
... (f/dispatch (conj event session))))))
(f/reg-event-fx :with-user
(fn [_ [_ event]]
{:with-user event}))
(f/reg-event-fx :event
(fn [_ [_ arg1 arg2 session]]
...))
Just one approach, there may be others.@U0HJK8682 thanks for the reply, I think I'll follow this approach
does anyone have a way of adding docs to reframe events/subs etc?