Fork me on GitHub

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.


(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)


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


using a middleware


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.)


@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.


fasiha: depending on the specifics of what you’re doing, specter might be helpful


Beautiful, that might be the ticket, thanks @nullptr


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


@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.


@fasiha: hard to tell from your description but you might want to use synthread synthread can be useful when your event handlers get complex (make lots of nested changes to app-db)


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

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


@fasiha: try

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