Fork me on GitHub
#re-frame
<
2019-11-13
>
atticmaverick05:11:04

im having issues with name spaced keywords. im trying to use a subscription ::my-sub located in ns a.b.c i have a require

(:require [a.b.c :as my-ns])
but when i try to use it:
(subscribe [::my-ns/my-sub])
i get an error: clojure.lang.ExceptionInfo: a.b.c does not exist #:cljs.repl{:error :invalid-ns}. but if i fully qualify it:
(subscribe [:a.b.c/my-sub])
it works. any ideas?

atticmaverick05:11:05

i think i got it. i wasnt requiring the subscriptions in a more parent ns.

sudakatux11:11:43

how does one account for nil subscription

sudakatux11:11:27

meaning say i do this @(rf/subscribe [:server/response-headers :value]

sudakatux11:11:33

and that returns null

sudakatux11:11:52

it fails since it cant de-reference null

sudakatux11:11:07

whats the correct way to handle those cases

dominicm11:11:55

some-> and deref?

dominicm11:11:09

Although, I don't think it should ever be nil

dominicm11:11:33

It would only be nil if you haven't registered the subscription, which usually indicates you're missing a require

sudakatux11:11:47

so what i mean is

sudakatux11:11:14

(rf/reg-sub
  :server/response-headers
  :<- [:server/list]
  (fn [list [_ id]]
    (get-in list [id :args :response :headers] {})))

sudakatux11:11:40

if i do @(rf/subscribe [:server/response-headers :some-value]

sudakatux11:11:12

and its not there. then that returns something i cant de-reference

dominicm11:11:23

No it doesn't

dominicm11:11:40

You can return nil from a subscription

dominicm11:11:25

The only time rf/subscribe returns nil (and therefore can't be deref'd) is when the subscription is missing.

sudakatux11:11:01

hm strange. i "fixed" it when adding the missing value

sudakatux11:11:11

im clearly missing something then

sudakatux11:11:22

You are totally right. i hardcoded nil and it doesnt break

dominicm11:11:36

I expect you have an ordering problem

sudakatux11:11:16

thanks. turns out i was using a vector where a map should go

cmal12:11:47

re-frame-http-fx-alpha not work for me. and re-frame-http-fx-alpha-example lein dev get an error: Warning: lein-git-version git exited 128 {:exit 128, :out "", :err "fatal: No names found, cannot describe anything.\n"} . Seems there is a tag not been pushed.

superstructor13:11:01

Sorry about that @zyzy5730 I am fixing that now. Thanks for reporting the issue.

metehan13:11:47

When there are not active subscribers to subs, will they be still calculated? I have secondary level subscriptions which are expensive to calculate. I don't want them to work when there are not active views. How they work?

metehan13:11:21

This structure delivers efficiency. You see, all (currently instantiated) layer 2 subscriptions will run every time app-db changes in any way. All of them. Every time. And app-db changes on almost every event, so we want them to be computationally trivial.
documentation says like this. So what is the best way to avoid deeper layer subscription to run.

metehan13:11:29

My scenario is like this when user logs in I connect to a webscoket and load customers there will be like 20-30k customers and I am adding them to app db. Untill all this customers loaded I don't want to run deeper layer subsribtions.

superstructor13:11:46

Layer 2 runs every time app-db changes, Layer 3 only changes when input Layer 2 subs change. Use layer 3 subs. @m373h4n

superstructor13:11:20

Oh the scenario part of the question is a bit more complex. In short, just don't return a different value from the parent sub until you want to re-run the child. So you could for example do (if (< ... @m373h4n

superstructor13:11:57

The other part of the answer is that event handlers should be used for 'expensive computations' as much as reasonably possible. Subs are intended for making valules available to views only although its tempting to use them as a general purpose materialised view.

superstructor13:11:11

@zyzy5730 I think those tag issues are fixed now. Please ping me if you have any further issues.

metehan13:11:08

@superstructor thank you for the suggestion. So in this case i will add :loaded flag to app db I won't return anything from the layer2 untill it's true. It's acceptable approach right ?

metehan13:11:01

I had another idea save all these users to another place until loading complete then move all of them at once to app db. Which one would be better approach?

superstructor13:11:03

Sure that works but if any one of several input subs changes, the layer 3 will be re-run @m373h4n

superstructor13:11:41

Re event handler vs subs in this case I wouldn't say my answer is authoritative but I'd lean towards subs as it simply waiting for n items to load is not in of itself an expensive computation and its conceptually easier to understand.

metehan13:11:40

thank you

👍 4