Fork me on GitHub
Alex H21:09:46

ok, so this might be a stupid question - is it safe to manipulate app-db in any way from a reg-sub-raw subscription?

Alex H21:09:52

I'm following and kicking off some backend requests, effectively, from the subscription handler. I'd like to get a loading marker of sorts. I was planning on putting it inside an ratom within the subscription handler, but there's some issues with how data comes back - if the data coming back ends up triggering (A) a re-frame dispatch and (B) a synchronous callback, it's likely that the callback clearing the loading state (B) happens before the dispatch merging the actual data into the app-db (A) occurs, leaving me in a brief state where the subscription doesn't say it's loading anymore, but the data isn't (yet) in the app-db either

Alex H21:09:54

If the loading marker could be in the app-db, that would work just fine, in that the dispatch'ed event can clear it as it merges in the data. However, that means I somehow need to set the loading marker in the app-db from the subscription handler itself

Alex H21:09:01

am I missing something?


Could you put the loading marker in the view?

(defn view
  (if-let [data @(re-frame/subscribe [:key ...])]
    [display-data data]


otherwise, I'd make the dispatch callback remove the loading at the same time as it associates the data in app-db

Imagine the app-db is something like
{:loading? true
 :data nil}

;; My callback dispatch (which accepts the fetched data)
(defn handle-callback
  [{:keys [db]} [_ data]]
  {:db (assoc db :loading? false :data data)}


@alex340 yeah shouldn’t try to change db from a subs. They are read only


If you dispatch events from it , you can do the changes in a event handler.