Fork me on GitHub
#reitit
<
2018-02-20
>
carkh01:02:48

hi, i've been reading the documentation and i must say the library looks nice and the documentation is very torough

carkh01:02:59

i have a question though

carkh01:02:42

if i want to define some part of my route tree in an other namespace, it all works

carkh01:02:50

but what if i recompile this other namespace ?

carkh01:02:03

I'm stuck with the older version of it in my routes

carkh01:02:17

i understand that the "reloaded workflow" can overcome that

carkh01:02:30

but that's quite unlispy

carkh01:02:08

in my own (very much less well thought out) routing library i accept vars as sub-routes

carkh01:02:41

and resolve these in real time ...is this possible at all with all the transformation you're performing on the route tree ?

carkh01:02:27

There must be some obvious way to do this, and i didn't understand it

ikitommi06:02:02

@carkh if you don’t want to use the reloaded, you need to re-create the router for all requests, as it compiles the routes to be fast - and use Vars. Something like:

(defn app [request]
  ((ring/ring-handler (ring/ring-router [#'routes1 #'routes2])) request))

ikitommi07:02:27

Shouldn’t use that in prod thou, might take some microseconds to compile it at demand.

carkh07:02:39

That only troubles me a development time, so your solution might be it

ikitommi07:02:17

the route conflict resolution is also happening at router creation-time, a good reason to re-create the router after any route changes.

carkh07:02:38

The thing is that I'm doing pretty large projects ... I like to have my routes follow the code and make a tree that spans over many namespaces

carkh07:02:17

one of these projects is as old as clojure 1.0 ... there was no reloaded workflow back then

carkh07:02:28

and i can't retrofit it

carkh07:02:24

that's not usually a problem, but when i recompile a file, i'd love to see the changes =)

carkh07:02:49

before that i was using common lisp ...and i tried having a single route file that woudl dispatch to other parts of the code ... but that get unwieldy after a while

ikitommi07:02:18

(defn create-router [routes reload?]
  (if reload? #(r/router routes) (constantly routes)))

(def router (create-router ["/foo" #'routes] true))

(r/match-by-path (router) "/foo/bar")

ikitommi07:02:58

something like that.

ikitommi07:02:59

Just one router at the top, the partial routes can be normal route structure, e.g. (def routes [“/inc” ::inc])

ikitommi07:02:21

oh, the there is no Expand for Var.

ikitommi08:02:07

ok, need to fix that. Could you write an issue to support Vars in router?

carkh08:02:39

I'm not sure that's necessary ... I've been able to do what I wanted without vars ... but your solution only works for one level deep

carkh08:02:21

what if the other namespace tries to refer to yet another namespace

carkh08:02:57

I'm not sure that's possible at all... and that's no big deal... I understand now why

carkh08:02:12

maybe something like a segment router might be forced upon seeing a var and then resolve the var on the fly ?

carkh08:02:29

i posted the issue, but not quite sure it makes sense at all in current context

ikitommi08:02:36

@carkh answered, realised it should work with functions. As a bonus you can create routes programmatically based on function parameters. e.g. pass in context of some sort and modify routes based on that.

carkh08:02:39

sounds like it shoudl work, let me try it

carkh09:02:18

i think there's no need to make the top router (prod-router and dev-router) functions, we're needlessly creating an object each time

carkh09:02:26

and there is a little bug in your solution

carkh09:02:59

but that works !

ikitommi09:02:40

great! I think the constantly creates the instance once and reuses that after that. So, it’s only done once. e.g. (constantly (fire-missiles!)) is a bad idea 🙂

carkh09:02:31

the parameter to constantly is created only once

carkh09:02:49

but we're creating a closure each time we call it aren't we ?

carkh09:02:03

you're making me doubt now =)

carkh09:02:00

no you're right actually

carkh09:02:05

ok then =)

ikitommi09:02:05

(def prod-router (create-router routes false)) => it creates the closure once. It’s an extra function call in prod, but that should be a nanosec.

carkh09:02:27

right sorry about that... thanks a bunch !

ikitommi09:02:52

np! that (fixed) example could be put to the docs…

carkh09:02:59

might want to add that recipe somewhere in the docs

ikitommi09:02:06

please do 🙂

carkh09:02:06

beat me to it

carkh09:02:00

i wouldn't know where to begin doing such thing

ikitommi09:02:00

(defn slow2 []
  (Thread/sleep 2000)
  2)

;; slow
(def a (constantly (slow2)))

;; nanos
(a)

carkh09:02:56

yes, but in your example we were calling constantly ...constantly ! we came to the same conclusion, use it at top level

ikitommi09:02:28

oh, true, cool!

ikitommi09:02:20

the docs are under /docs and the README has the guide to run gitbook if you have time to create a page for that, under advanced maybe?

ikitommi09:02:37

would be lovely (heading for 3 weeks vacation tomorow)

carkh09:02:25

ah, have a good vacation then ! sorry to bother you while you must be packaging for the trip =)

carkh09:02:30

I'll do it, seems easy enough