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

Instrumenting registered functions #552

Open
olivergeorge opened this issue Sep 28, 2019 · 5 comments
Open

Instrumenting registered functions #552

olivergeorge opened this issue Sep 28, 2019 · 5 comments

Comments

@olivergeorge
Copy link
Contributor

olivergeorge commented Sep 28, 2019

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.

@olivergeorge
Copy link
Contributor Author

olivergeorge commented Oct 1, 2019

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.

@jeaye
Copy link

jeaye commented Dec 16, 2019

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)))

@knubie
Copy link

knubie commented May 22, 2020

@jeaye This looks really useful, any plans on open sourcing those macros?

@jeaye
Copy link

jeaye commented May 22, 2020

@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!

@knubie
Copy link

knubie commented May 23, 2020

@jeaye Cool, looking forward to it!

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

3 participants