Fork me on GitHub
#pedestal
<
2018-03-12
>
nooga21:03:38

I’m writing a defbefore that uses a component, what’s the best way to pass the component inside?

mtnygard21:03:00

@nooga Something I usually end up doing is write an interceptor to attach the whole system map to the context.

mtnygard21:03:08

(defn attach-system [system-map] {:enter (fn [context] (assoc context :system system-map))})

mtnygard21:03:11

Something like that.

mtnygard21:03:32

Then it goes at the front of the interceptors for every route

dadair14:03:11

As an example with non-component systems, I’ve adopted the following pattern using Integrant:

(defmethod ig/init-key ::inject-bus [_ {:keys [bus]}]
  {:name ::inject-bus
   :enter (fn [ctx] (assoc ctx :bypass.events/bus bus))})
then I can pass that interceptor around the system map as needed

dadair14:03:56

so the bus component is closed over in the interceptor creator, which will inject the component into the context map when entered

nooga21:03:39

the other would be to capture the component (defn my-before [component] (helper/before (fn [context] ... component ...))

mtnygard21:03:13

Basically the same idea. You can decide how much to close over when creating the interceptor.

mtnygard21:03:47

FWIW, the "helpers" are kind of discouraged now. They really obscured the fact that interceptors can be just plain, simple maps.

nooga21:03:04

to be honest I just snatched some random example and now I’m trying to make it work 😄

nooga21:03:11

it might be an old example

mtnygard21:03:47

Very likely. It's hard to out SEO the older blog posts.