kit icon indicating copy to clipboard operation
kit copied to clipboard

Autoreload on file change or on evalutation

Open elmehalawi opened this issue 2 years ago • 12 comments

Is it possible to autoreload or auto-reset when a file changes, or when form is evaluated in a namespace?

elmehalawi avatar Jun 29 '22 21:06 elmehalawi

The closest would be to call (user/refresh) from the REPL.

yogthos avatar Jun 29 '22 21:06 yogthos

Does that need to be called on each change?

elmehalawi avatar Jun 29 '22 21:06 elmehalawi

Yeah, you'd have to call it explicitly when you want the REPL state refreshed.

yogthos avatar Jun 29 '22 23:06 yogthos

Is it possible to have this called on file change, or when something is evaluated in the repl?

elmehalawi avatar Jun 29 '22 23:06 elmehalawi

Generally, that wouldn't be the desired behavior. You shouldn't have to reload the entire REPL state any time you evaluate something in the REPL. Typically, it's best to do a full reload explicitly with a command instead of having it run automatically as it's a slower operation and it can break the REPL state if files aren't in a good state.

yogthos avatar Jun 30 '22 15:06 yogthos

That was my first thought initially too. But in my project if I evaluate a function that returns some hiccup, that change isn't reflected when I reload the browser page. Does that mean my project is misconfigured, or is this the expected behavior?

elmehalawi avatar Jun 30 '22 20:06 elmehalawi

This is more of a reitit problem than kit. Once routes are defined, the handler is bound to the route. Even if you recompile the handler function, reitit will still use the old one.

One workaround is that you define your routes to use functions.

For example, instead of the following:

(defn ui-routes [_opts]
  [["/" {:get home}]
   ["/clicked" {:post clicked}]])

you wrap your handlers with function in your route definitions

(defn ui-routes [_opts]
  [["/" {:get #(home %)}]
   ["/clicked" {:post #(clicked %)}]])

This is not elegant, but it works.

The right fix would be to implement a solution from reitit directly in kit. Basically, having one non-efficient but reloadable router for development, and one non-reloadable but efficient for production.

markokocic avatar Jul 01 '22 10:07 markokocic

I think that would be a useful change to make. @nikolap thoughts?

yogthos avatar Jul 01 '22 14:07 yogthos

Great find and makes sense to me, thanks for the research and recommendation.

Would welcome a PR. Else I can look at it in late-July.

nikolap avatar Jul 02 '22 09:07 nikolap

Haven't forgotten this issue! Only just found the time to look at it :)

nikolap avatar Aug 15 '22 09:08 nikolap

Do we still need to figure out a fix here?

yogthos avatar Sep 12 '22 13:09 yogthos

I haven't tested master, but for my day to day workflow I still need to reset on each and every change.

elmehalawi avatar Sep 13 '22 16:09 elmehalawi