oakes / odoyle-rum

The Unlicense
71 stars 4 forks source link

How do we inspect state? #2

Open drewverlee opened 3 years ago

drewverlee commented 3 years ago

I'll lead with an example.

Let's say i want to add a new feature to the todo app example. I start my REPL, the server and figwheel builds dev which produces the expected browser application. I open the file with the rules and start to add a new one by pulling in the all-todos state/triple in the :what block. But i'm not quite sure what's in it.

Ever the optimist, I select the all-todos symbol and eval it, hoping to get back a clojure data structure. Instead, i get all-todos is not defined in this context. That context being by clj repl. Next, I assume I can use query the @*session for this information, but it returns the initial state, not what i'm seeing in the browser. In this case, it wasn't hard for me to infer the state from the code and specs, but I feel there must be a better way.

To me, ideally, the symbols (e.g all-todo) would be inspectable. This might be a context issue on my end, but I thought the session was being backed on the server, so clj would be correctly tracking the state. Though given it says it's undefined, i suppose it's because it's a macro and there is no link from the symbol to any state in the ns.

More confusing is why @*session is showing me the default state. I don't imagine it's because i started the build on a future as atoms are across threads.

Let me know if you have any ideas!

oakes commented 3 years ago

The reason you can't just eval all-todos in a REPL is because it isn't a top-level var. You would have to define it using def to get that. In odoyle, if you want to get any data from the session, you need to get it using query-all. In the todo example, you could do this:

(first (o/query-all @*session ::get-all-todos))

After inserting a few todos, that looks like this for me:

{:all-todos [{:id 0, :text "wash the car", :done false} {:id 1, :text "buy groceries", :done false}]}

The ::get-all-todos rule just has all-todos, so that's the only thing you'll see in the results. So any time you want to inspect data in the session, you need to make a rule that brings that data into its :what block.

drewverlee commented 3 years ago

Thanks @oakes, that's my understanding too. It seems I was evaling my cljc file in a context that didn't make, for lack of a clearer understanding, make any sense.

Currently what works for me (e.g getting the same output you demonstrate above). Is having a .dir-locals.el file

((clojure-mode . ((cider-clojure-cli-aliases . "-A:cljs")))
 (clojurescript-mode . ((cider-clojure-cli-aliases . "-A:fig"))))

So that emacs can manage to inject necessary middleware deps for Nrepl (like piggieback which lets cider understand cljs). This way I run cider-jack-in-clj, which with the main-opts, should start the webserver.

Then modifying the dev.clj file so that it just starts the server (not the figwheel build) e.g removing figwheel from that file altogether. Then using cider-jack-in-cljs it will trigger the main-opt (clojure -m figwheel.main -b dev r" which starts the fighwheel server in such a way that it seems to communicate with nrepl correctly.