This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-03-31
Channels
- # announcements (20)
- # asami (14)
- # aws (6)
- # babashka (15)
- # beginners (83)
- # biff (6)
- # calva (93)
- # cider (3)
- # clj-kondo (21)
- # cljdoc (106)
- # cljs-dev (32)
- # clojure (165)
- # clojure-dev (78)
- # clojure-europe (54)
- # clojure-italy (9)
- # clojure-nl (9)
- # clojure-norway (24)
- # clojure-uk (4)
- # clojurescript (6)
- # community-development (2)
- # conjure (2)
- # core-typed (14)
- # datahike (4)
- # datomic (2)
- # emacs (40)
- # events (1)
- # fulcro (11)
- # graalvm-mobile (29)
- # graphql (8)
- # honeysql (19)
- # java (1)
- # jobs (1)
- # lsp (232)
- # malli (5)
- # membrane (112)
- # nextjournal (11)
- # off-topic (63)
- # portal (12)
- # re-frame (6)
- # reagent (3)
- # reitit (4)
- # rewrite-clj (2)
- # shadow-cljs (25)
- # tools-deps (6)
A colleague asked how to use async apis in handlers. In this case async is needed to interrogate data before the event handler can apply business logic My approaches are • "two handlers + fx" but that feels fragmented/clumsy • "do async before dispatching" which can feel impoure as views are made responsible for some logic How about if event handlers could be registered like subs and include a signal fn.
(rf/reg-event-fx2
:file-upload
(fn signal [[_ file]] (.text file)) <-- this would wait for promise to resolve before calling handler
(fn compute [ctx text] ...))
That will break the expectation that the :fx
block is synchronous and not concurrent with anything else, so I doubt that it'll happen.
> • "two handlers + fx" but that feels fragmented/clumsy
This is pretty much the correct approach. Same exact way e.g. :http-xhrio
already works.
And you can use e.g. https://github.com/ingesolvoll/re-chain to make it less clumsy.
Thanks for the link. Readme certainly expresses the problem well.
Thinking out loud and trying to frame this better. Around an event we might want async before logic, during logic or after logic. After is covered by fx During logic is messy code. Probably can be teased apart (i.e. prepare inputs, run pure logic, trigger side effects) Before logic (data preparation) is the case I'm considering. Delaying the event handler execution a bit to allow some async to resolve seems clean. Could be cofx returning a promise or (my idea above) of a signal fn with access to the event vector.