Fork me on GitHub
#re-frame
<
2018-01-19
>
caleb.macdonaldblack05:01:07

Any way to input the db as input signal to a sub with out creating a db sub?

Miloslav05:01:53

still can't wrap my head around subscriptions

Miloslav06:01:11

At my router, this works:

(re-frame/reg-sub
 ::active-panel
 (fn [db _]
   (:active-panel db)))

(defn main-panel []
  (let [active-panel (re-frame/subscribe [::active-panel])]
    [pages/panels @active-panel]))
but this doesn't:
(def state->pouch_sub (keyword (str pouchDatabaseName "-sub")))

  (re-frame/reg-sub
    state->pouch_sub
    (fn [db [_ _]]
      (:charlie db)))
      
  (let [freshStateData (re-frame/subscribe [state->pouch_sub])]
    (.log js/console @freshStateData "subscription")))
It seems pretty much the same. Same trivial subscription syntax, same let, same @ for dereferencing. But the first piece of code updates the active page live just like it have to be, while second doesn't work at all, it's being executed just when i hit Ctrl + S and figwheel reloads page

Miloslav06:01:57

I've tried to change generated keyword to something static like :hello, but it doesn't work anyways

Miloslav06:01:41

I tried reg-sub-raw:

(re-frame/reg-sub-raw
    state->pouch_sub
    (fn [app-db _]
      (reaction (get-in @app-db [(keyword pouchDatabaseName)]))))
But it works just like reg-sub: one time one page reload, it doesn't work live

Empperi06:01:51

@melvoloskov you need something for React in order for reaction to work which is the basic building block behind reagent atom and re-frame subscriptions

Empperi06:01:10

Meaning, you need a React component

Empperi06:01:15

All the magic gets binded to React under the hood which does it's magic via virtual DOM, if you do not add anything to the DOM it can't do it's magic

Miloslav06:01:05

But how do I execute arbitrary function on db changes?

Miloslav06:01:53

Thanks a lot

Empperi06:01:02

Well it depends on your use case

Empperi06:01:39

Usually in browser stuff gets initiated by three ways: user interaction (you have DOM), server side data (you save data to db, UI components react to this) or via scheduled operation

Empperi06:01:18

Basically, if you want to do something else than show stuff in UI on db change then use effects

mikethompson06:01:01

@melvoloskov > But how do I execute arbitrary function on db changes? You generally don't need to. If you do: - you can use Track in the library given above. - you can use an effect handler to also do dirty work.

Empperi06:01:28

Or you invoke an effect which then does your thing

Empperi06:01:51

Right you edited and added that :)

mikethompson06:01:09

yeah, sorry, i did

Empperi06:01:10

Typing via phone is slow

Miloslav06:01:56

Thanks! I'll use utils

Miloslav06:01:02

You guys save me a lot of time

adammiller21:01:25

@melvoloskov You do have another option which is to utilize the add-watch function in clojure.core. Since re-frame's app-db is an atom you can register your own functions to watch it and execute function when it changes. I probably wouldn't use that method for anything other than logging or debugging though.