Fork me on GitHub
#re-frame
<
2018-09-11
>
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 https://github.com/Day8/re-frame/blob/master/docs/Subscribing-To-External-Data.md 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?

enforser22:09:40

Could you put the loading marker in the view?

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

enforser22:09:54

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

mikerod23:09:32

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

mikerod23:09:52

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