re-frame

https://github.com/Day8/re-frame/blob/master/docs/README.md https://github.com/Day8/re-frame/blob/master/docs/External-Resources.md
genekim 2020-07-13T15:54:33.434900Z

Drats — accidentally deleted my message. Reposting. Ever want to write a simple application, but writing a CLJS front end and a CLJ backend just seem like overkill? And the idea of having to learn Java Swing or JavaFX seems too daunting, but so too does the idea of writing a curses/terminal app (which doesn't even have an event loop)? @smith.adriane wrote something so cool, the membrane library — it lets you write a curses/terminal app that runs in your terminal or in a Swing application, but on top of re-frame. He helped me write a simple app yesterday that captures and displays keystrokes (hacked into the membrane sample app, which is the canonical TODO MVC app.) I'm so excited by this, because there's a whole class of simple applications that this seems perfect for! Thank you, @smith.adriane and @mikethompson !

genekim 2020-07-13T15:54:50.435Z

Here was his announcement of the project yesterday — so fun!!! https://clojurians.slack.com/archives/C073DKH9P/p1594229800342600

genekim 2020-07-13T15:56:26.436100Z

And a screenshot of the ridiculous simple app I'm so happy about! :) https://clojurians.slack.com/archives/C073DKH9P/p1594623576408700?thread_ts=1594229800.342600&channel=C073DKH9P&message_ts=1594623576.408700

genekim 2020-07-13T15:58:15.437100Z

Further discussions in #membrane!

phronmophobic 2020-07-13T16:01:54.437300Z

Thanks for the kind words @genekim and also thanks to @mikethompson for re-frame. If this is something people are interested in, I could use some help designing re-frame compatible versions of basic reusable components like textboxes

Rob Knight 2020-07-13T16:41:42.446900Z

I'm struggling with a design question. In my app I have a concept of "actions" that the user can take, which are determined dynamically based on things like selections made in the UI and data fetched from remote services. I have a network of subscriptions which aggregates this nicely into a single list of actions, and a UI which can show the user a searchable list of these actions. Subscriptions mean that the list updates dynamically, as it should. I also want to provide keyboard shortcuts for actions, and I've been looking at re-pressed as a library for this. Problem is that re-pressed requires me to "declare" the active keyboard shortcuts via an event (`set-keydown-rules`). So instead of having a data-driven reactive workflow as I do with the action list UI, I have to trigger events when the relevant data changes, if the change means that a new keyboard shortcut is now valid (or an old one is not). My ideal solution is simply to be able to trigger an event when a subscription changes. It seems like the easiest way to do this is to have a Reagent form-3 component which subscribes to the relevant subscription, and triggers an event in the :component-did-update handler, while returning nil from the render function. Is there a better design pattern for this?

ggiraldez 2020-07-13T17:05:21.447100Z

I have not used it (yet), but I think you want track! https://reagent-project.github.io/docs/master/reagent.core.html#var-track.21

ggiraldez 2020-07-13T17:08:02.447300Z

removes the need of a react component and is more explicit in intent

Rob Knight 2020-07-13T17:14:15.447500Z

Oh, that makes sense. Thanks! I was starting from the JS React-ish concept of renderless components, which felt like an un-Reagent/Re-frameish way of doing things.

gadfly361 2020-07-13T17:19:45.447700Z

Regarding re-pressed, yeah, the recommended approach is to dispatch the set-keydown-rules event anytime the rules change. There is no limit to how frequent you can dispatch that even, so I think you can achieve the dynamic effect you want.

Rob Knight 2020-07-13T17:37:36.447900Z

Yes, absolutely! My problem was just that I wanted to re-use the work I had already done in generating the action list, which is bundled up in a subscription, so I needed to figure out the right way of triggering an event on a subscription change.

👍 1
Rob Knight 2020-07-13T18:54:36.448400Z

Does track! work with re-frame subscriptions? I'm trying it but not seeing the results I would expect.

ggiraldez 2020-07-13T19:22:04.448600Z

re-frame subscriptions are reagent reactions, so yes, it should work

ggiraldez 2020-07-13T19:22:43.448800Z

just make sure to deref the subscription inside the function

Rob Knight 2020-07-13T19:23:07.449Z

Yeah, that's what I'm doing.

Rob Knight 2020-07-13T19:23:36.449200Z

(defn active-tracker []
  (let [active (re-frame/subscribe [::active])]
    (prn @active)))

(defonce tracker (r/track! active-tracker))

Rob Knight 2020-07-13T19:24:25.449400Z

It prints once, but doesn't do anything when the subscription updates.

ggiraldez 2020-07-13T19:25:27.449600Z

hmmm... my guess is it doesn't work because you are creating the reaction inside the tracking function

Rob Knight 2020-07-13T19:26:27.449800Z

Oh, hmm. Where else am I supposed to create it?

ggiraldez 2020-07-13T19:27:40.450Z

(let [active (re-frame/subscribe [::active])
     tracker (fn [] (println @active))]
  (r/track! tracker))

ggiraldez 2020-07-13T19:27:47.450200Z

that should work

Rob Knight 2020-07-13T19:27:58.450400Z

Thanks, I'll try that

ggiraldez 2020-07-13T19:29:22.450600Z

you will need to keep a reference of the result of track! for later disposal though... the example I gave you will leak the reference

Rob Knight 2020-07-13T19:30:54.450800Z

This seems to behave the same way as the original version 😞

ggiraldez 2020-07-13T19:31:52.451Z

maybe something like:

(defonce tracker (let [active (re-frame/subscribe [::active])] (r/track! (fn [] (println @active))))

ggiraldez 2020-07-13T19:33:25.451200Z

although probably an atom would be more appropriate to hold the reference

Rob Knight 2020-07-13T19:37:17.451400Z

I'm doing that, but the basic functionality just doesn't seem to work. I must be missing something obvious but I can't work out what.

Rob Knight 2020-07-13T20:32:48.451600Z

Ah, during re-frame setup/auto-reload I have a call to re-frame/clear-subscription-cache! and this breaks the subscription. As it's not part of a view/component, it doesn't get recreated. That was the problem.

👍 1
adam 2020-07-13T23:27:30.452900Z

Any good open source re-frame project to get idea about to to properly structure the components, etc?

phronmophobic 2020-07-13T23:34:10.453Z

@genekim suggested https://github.com/day8/re-com to me yesterday and it looks pretty good and has lots of examples: https://github.com/day8/re-com/blob/master/src/re_com/misc.cljs

phronmophobic 2020-07-13T23:34:35.453400Z

for specifically flexible and re-usable components

adam 2020-07-13T23:43:09.453800Z

Thanks @smith.adriane