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

Adds function to simplify creating new event types #779

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Sardtok
Copy link

@Sardtok Sardtok commented Mar 8, 2023

Adds the function reg-event to re-frame.core which takes a handling interceptor instead of a handler as its last argument. This makes it easier to create event registering functions with all standard interceptors added to the chain, even if changes are made to the standard interceptor chain.

reg-event-db, -fx and -ctx now wrap reg-event, passing their interceptor wrapped handlers.

Adds the function reg-event to re-frame.core which takes a handling interceptor
instead of a handler as its last argument. This makes it easier to create event
registering functions with all standard interceptors added to the chain,
even if changes are made to the standard interceptor chain.

reg-event-db, -fx and -ctx now wrap reg-event, passing their interceptor wrapped handlers.
@Sardtok
Copy link
Author

Sardtok commented Mar 8, 2023

This is a suggestion to make creating custom event types a tiny bit easier.

Here's an example use case. Let's say we want to catch exceptions and add exception traces. If we don't want to add the standard chain ourselves (adds maintenance risk, although a low risk as this isn't code that changes often), right now we could write this as:

(defn- interceptor->with-exception-tracing [f]
  (fn [context]
    (try (f context)
         (catch :default e
           (trace/merge-trace! {:tags {:exception e}})
           (throw e)))))

(defn- wrap-exception-interceptor
  [id]
  (let [interceptors (into [] (get-in @registrar/kind->id->handler [:event id]))
        last-interceptor (peek interceptors)
        wrapped-interceptor (update last-interceptor :before interceptor->with-exception-tracing)]
    (swap! registrar/kind->id->handler assoc-in [:event id]
           (conj (pop interceptors) wrapped-interceptor))))

(defn my-reg-event-db
  ([id handler]
   (my-reg-event-db id nil handler))
  ([id interceptors handler]
   (re-frame/reg-event-db id interceptors handler)
   (wrap-exception-interceptor id)))

If we don't mind adding the interceptors ourselves, we could shorten this a bit, but this adds some risk that changes to the standard interceptor chain aren't caught, and the event will act in a non-standard way:

(defn my-reg-event-db
  ([id handler]
   (my-reg-event-db id nil handler))
  ([id interceptors handler]
   (events/register id [cofx/inject-db fx/do-fx inject-global-interceptors interceptors
                        (-> handler
                            std-interceptors/db-handler->interceptor
                            (update :before interceptor->with-exception-tracing))])))

With the suggested edit the final registration is simpler:

(defn my-reg-event-db
  ([id handler]
   (my-reg-event-db id nil handler))
  ([id interceptors handler]
   (re-frame/reg-event id interceptors
                       (-> handler
                           std-interceptors/db-handler->interceptor
                           (update :before interceptor->with-exception-tracing)))))

Might not be a huge difference, but it feels a bit more ergonomic. And if changes are made to the standard interceptor chain, they are only made in one place.

@kimo-k kimo-k force-pushed the master branch 6 times, most recently from f8b5e1d to 9dee549 Compare November 2, 2023 17:36
@kimo-k kimo-k force-pushed the master branch 11 times, most recently from 69d0aa1 to f196eee Compare November 21, 2023 03:44
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

Successfully merging this pull request may close these issues.

None yet

1 participant