Instrumenting registered functions
I'm looking for a way to use spec to instrument my event handlers & subs.
I run stest/instrument in a :preload namespace. Since that ns requires all my handler code it happens after (re-frame/reg-event-fx :myfn myfn) executes so the uninstrumented fn is captured.
I could move all my re-frame/reg-event-fx calls to a different ns and (e.g. app.core) and make sure it loads after instrumentation has happened. Perhaps there are better techniques. I've seen examples in the documentation of using an interceptor to check the db value.
Just watched Alex Miller's ClojuTre talk. It includes some discussion of spec2. Some talk of defining spec's as part of defn (as metadata?). Not sure it's a game changer but could be interesting.
OkLetsPlay uses Orchestra's defn-spec behind the scenes, wrapping events, effects, and subs in spec. Then we just use Orchestra's complete instrumentation to catch any incorrect data flowing through the app. The result looks something like this (each randomly chosen from our FE code base):
; Event
(def-event ::view
([db ::d/db, match-id ::t.match/id, opponent-id ::t.user/id]
{:dispatch-n [[::found-active match-id]
[::bind match-id]
[::chat.event/initiate opponent-id false]]}))
; Effect
(def-effect ::show-overlay
([show? boolean?]
(if show?
(framework/open-window! "overlay")
(framework/close-window! "overlay"))))
; Sub
(def-sub ::all-known (s/nilable ::leaderboard.spec/all-known)
([db ::d/db, _ (event/args)]
(::leaderboard.spec/all-known db)))
@jeaye This looks really useful, any plans on open sourcing those macros?
@knubie Probably not, just since they're so integrated with OkLetsPlay's existing code. However, Orchestra comes with defn-spec, which is what actually offers that [arg ::spec] syntax for functions and is what those macros are using. So you should just be able to replace your existing re-frame reg-event-fx calls to use defn-spec functions. From there, wrapping it in a macro is trivial.
We will be open sourcing a different library related to this here soon, which aims to solve some problem's in re-frame's design with regard to events and callback hell. Stay tuned for that!
@jeaye Cool, looking forward to it!