Fork me on GitHub
#re-frame
<
2016-03-07
>
fasiha03:03:21

For a toy project I'd like to dump the contents of the app state to localStorage—that'll be all the persistence for now. Is there a single place where I could add some code to serialize the db to localStorage at every modification (perhaps even with some debounce)? I realize I could add it to every handler, but that seems error-prone. I am also seeing some cljs libs designed to sync an atom to localStorage but I'm thinking for this really simple situation I'd like to do it myself.

fasiha03:03:24

(Then I'd just modify the :initialize-db handler (which is included in the re-frame-template lein template) to try to load from localStorage)

danielcompton04:03:15

@fasiha: see the todo mvc re-frame app, it does just this

danielcompton04:03:21

using a middleware

fasiha17:03:51

I probably should just ask on #C03S1L9DN but maybe there's a re-frame idiom for this: I often have to update two or more paths in app-state. I'm doing this right now with nested assoc-ins or update-ins. Is there a better/clearer/more performant way? (I'm seeing on StackOverflow that one could use reduce but that seems even less clear.)

danielcompton18:03:58

@nidu yep that’s one way of doing it. I might instead have b-id be part of re-frame state instead of a local atom though. The alternative (in my foggy early morning brain) is to have a subscription that depends on selected state and looks up the right selection based on that. So rather than passing b-id through, you would just subscribe to :c-of-b and :c-of-b subscription would update itself when it’s selection changes. The tradeoff here is that you end up writing ever more specific subscriptions, because each one needs to take all of it’s dynamic parameters from app-db up front.

nullptr19:03:09

fasiha: depending on the specifics of what you’re doing, specter might be helpful https://github.com/nathanmarz/specter

fasiha19:03:35

Beautiful, that might be the ticket, thanks @nullptr

nullptr19:03:18

cool, let me know how it works out if you try it

nidu20:03:30

@danielcompton: thanks for good explanation! I guess a lot of specific subscriptions is not that bad after all. And keeping selection info in app-db would also make app more reload-friendly i guess.

mikethompson21:03:18

@fasiha: hard to tell from your description but you might want to use synthread https://github.com/LonoCloud/synthread http://www.infoq.com/presentations/Macros-Monads synthread can be useful when your event handlers get complex (make lots of nested changes to app-db)

fasiha21:03:07

@nullptr @mikethompson sorry my description was vague: here's a small real example:

(assoc-in
  (assoc-in db [:gui :mouse-down-pos-pixel] curr-pix)
  [:gps :centerpt] new-degs)

danielcompton22:03:39

@fasiha: try

(-> db (assoc-in [:blah] v) (assoc-in [:foo] v2))