Fork me on GitHub
#re-frame
<
2017-01-03
>
mattsfrey17:01:24

Hey I've been having trouble converting pre 0.8.0 middleware handlers to interceptors

mattsfrey17:01:24

is there any doc or guide to definitively change the old style middleware fn's to interceptors?

mikethompson19:01:24

@mattsfrey What I'd do: 1. Understand Interceptors by reading all the 'Event Handlers" section of the docs: https://github.com/Day8/re-frame/blob/master/docs/README.md 2. Look at the way the standard middleware was converted to interceptors (eg: debug and trim, etc) 3. Realize there is no mechanical way to do the conversion. It has to be done by hand. But, we should only be talking 10 lines of code per middleware/interceptor.

mikethompson19:01:41

Regarding point 2 above ... pick a particular interceptor, like enrich or debug and compare before and after ....

mattsfrey19:01:46

yeah I ended up reading the docs pretty throughly, had to rearrange some things due to the before/after mechanism

mattsfrey19:01:21

i.e. printing logs of changes I print the event name on the before and the diff on the after (the event name gets stripped from the trim-v interceptor and is thus unavailable on the after flow)

mattsfrey19:01:03

(def debug
  (re-frame/->interceptor
    :id :debug
    :before (fn [context]
              (let [{:keys [event]} (:coeffects context)]
                (log/group "Event: " (first event))
                (log/log event))
              context)
    :after (fn [context]
              (let [{:keys [db]} (:coeffects context)
                    new-db (-> context :effects :db)
                    diff (data/diff db new-db)]
                (log/debug "Before: " (first diff))
                (log/debug "After : " (second diff))
                (log/group-end)
                context))))

mattsfrey19:01:25

does this seem more or less like a legit pattern to accomplish this?

mikethompson19:01:42

Seem entirely reasonable. Be aware though that :effects might not have a :db

mattsfrey19:01:17

if the event handler returns nil?

mattsfrey19:01:47

(or one of the interceptors clears it out for some reason?)

mikethompson19:01:23

(reg-event-fx :some-id (fn [cofx event] {:http {:url "http://example.com/blah"}})) `

mikethompson19:01:41

That event handler only has a :http effect

mattsfrey19:01:02

ohhh ok, well this code base goes back about 7 months now, so we actually only have the old school pure db handlers

mattsfrey19:01:32

haven't used that new pattern yet^

mikethompson19:01:10

See the check on line 41

mattsfrey19:01:32

oh very cool

mattsfrey19:01:05

does skipping the log if there was no change improve performance at all or do you know?

mikethompson19:01:42

debug should only be used in developement, never production

mikethompson19:01:51

So it isn't about performance

mikethompson19:01:59

It is about quality of logs (in js/console)

mattsfrey19:01:29

yes we are only showing diffs in dev

mattsfrey19:01:48

was far too slow for production

mattsfrey19:01:02

lol, wow. this fn has color coded expanding pretty print stuff as well... just mad I spent like 2 hours figuring out how to reproduce a ghetto version of this myself.. good learning experience though I suppose. Thanks for the tips!

curlyfry19:01:07

@mattsfrey In 0.9.1, the trim-v interceptor puts the event name back 🙂

mikethompson19:01:06

Yes, was requested. Causing a problem?

curlyfry19:01:28

Just pointed out that it doesn't do that anymore!

mikethompson19:01:50

Got it. Thanks for the clarification

mikethompson19:01:15

================================= Tip of the day Learn to use enrich for validation and detecting warning/error states: https://github.com/Day8/re-frame/blob/master/src/re_frame/std_interceptors.cljc#L195-L227 =================================

mattsfrey20:01:55

our personal middleware setup has re-frame.core/trim-v as the last interceptor

mikethompson20:01:17

or right most?

mattsfrey20:01:38

(def middleware [(when ^boolean js/goog.DEBUG re-frame/debug)
                 dispatch-once-support
                 re-frame/trim-v])

mattsfrey20:01:45

thats what I'm using currently

mikethompson20:01:54

Should be fine

mattsfrey20:01:10

yeah, thats why the event print had to be done on :before though for us anyway

mattsfrey20:01:48

on :after (-> context :coeffects :event first) would be nil

mikethompson20:01:07

As @curlyfry pointed out above, I think any issues were fixed in 0.9,1

mikethompson20:01:24

Ie. event is restored to its full glory now, on the backwards sweep

timgilbert21:01:40

Say, I'm not seeing the 0.8.0 syntax sugar :<- [:some :subscription] stuff for (reg-sub) in the docs any more. Am I just missing it, or is it deprecated?

timgilbert21:01:59

(I actually prefer the two-fn form of reg-sub, personally, but we've got some old code that uses the :<- form, not sure if I should try to refactor it out while I'm in there messing around)

mikethompson22:01:29

Nothing has changed. Still a part of the docs via the annotated todomvc source:

timgilbert22:01:15

Oh, gotcha. Ok, thanks @mikethompson