Fork me on GitHub
#re-frame
<
2016-03-12
>
fasiha03:03:25

So, middleware. I grok that when you have multiple middleware, re-frame runs them in the opposite order as comp, i.e., first one in the list first. But how do after middleware fit into that pattern? E.g., in the todo-mvc example (https://github.com/Day8/re-frame/blob/4b3e59950f90c46d873aaab0777c1217a1aeead0/examples/todomvc/src/todomvc/handlers.cljs#L27) two of those middleware are after and the other two, trim-v and path are (I suspect) not-`after`. The after middlewares must operate on the output of the base handler (which gets its input from the not-`after` middlewares) right?

cky03:03:34

@fasiha: after middleware run the rest of the middleware chain, then runs the thing you passed.

cky03:03:54

Therefore the execution sequence of after middleware is right-to-left.

fasiha03:03:52

@cky: thanks, your prose explanation is much more understandable to me than the highly-nested code implementing after 😛 I guess then my question is: in the link I pasted above to the todomvc re-frame example, a vector of middlewares is (in pseudocode): [after f, path, after g, trim-v]. From what I understand so far, both after middlewares could be moved to the end of the vector (preserving their order, so in that case, check-schema-mw would still be left of ->ls) and functionality would be exactly the same?

fasiha03:03:59

I'm trying to understand why those four middlewares there were ordered the way they are, and just how much flexibility there is in reordering that vector. I.e., the two afters could have come first, last, interspersed as they are now…

cky03:03:24

@fasiha: To my understanding, the execution order is: path, trim-v, todos->ls!, then check-and-throw.

fasiha03:03:09

@cky: Excellent—that makes sense. Though it would probably be more sensible to write to localStorage (`todos->ls!`) only after the schema validation passed (`check-and-throw`).

fasiha03:03:09

Perhaps a todo (for me): add the twist about after middlewares being ordered the opposite as non-after ones to the wiki

fasiha20:03:59

I posted a question about re-frame-template (https://github.com/Day8/re-frame-template/issues/24) when using +handler option (to use compojure), about autoloading the compojure handler as the file is changed. Cross-posting here in case that's something everyone can do—trying out this Clojure-in-both-front/back-end thing simple_smile