Fork me on GitHub
#re-frame
<
2019-10-16
>
eskemojoe00720:10:35

I have a subscription that doesn't seem to be updating, and I'm a bit off into the weeds with my signal function within the subscription: I have an app-db that is structured like {:games {<game-id> {:game/players [<player 1> <player 2> etc]}}}. I have some subscriptions that look like:

(rf/reg-sub
 :games/games
 (fn [db _]
   (:games db)))

(rf/reg-sub
 :games/game
 :<- [:games/games]
 (fn [games [_ game-id]]
   (get games game-id)))

(rf/reg-sub
 :game/players
 (fn [[_ game-id] _]
   (rf/subscribe [:games/game game-id]))
 (fn [game _]
   (get game :game/players)))
I've updated the App-db, and the subscription to :game/players has the old information. Is there a normal cause for this? I'm new to the signal functions.

eskemojoe00720:10:54

Subscription still looks like:

eskemojoe00720:10:18

Where App-db looks like:

eskemojoe00720:10:45

Show's the frisk diff of the event in question

lilactown20:10:27

@david.folkner I don’t think you should be calling subscribe inside of a subscription quite like that

lilactown20:10:55

you should use a signal function I believe

lilactown20:10:57

oh apologies, you are!

eskemojoe00721:10:30

Yeah. It works if I refresh or change some other state in the app-db, but if you look at the pictures in the thread, the subscription is out of date.

eskemojoe00721:10:14

Does it have anything to do with where/how I call the actual subscribe (rf/subscribe [:game/players <game-id>]) ?

Vachi21:10:58

looks like you are passing a parameter in the signal , does that work? I haven't seen it in the docs

Vachi21:10:22

yeah , that's the return, but you are destructuring the parameters and consuming it in the signal-fn

Vachi21:10:40

(rf/reg-sub
 :game/players
 (fn [[_ game-id] _]
   (rf/subscribe [:games/game game-id]))
 (fn [game _]
   (get game :game/players)))  

eskemojoe00721:10:48

But maybe I've extrapolated. To me that's the entire reason to write the signal function.

Vachi21:10:57

I mean this one

(fn [[_ game-id] _]
   (rf/subscribe [:games/game game-id])) 
can you use the parameters or is nil?

eskemojoe00721:10:50

I thought it was good to build out subscriptions in a cascading way like I have. I could clearly re-write it as

(rf/reg-sub
 :game/players
 :<- [:games/games]
 (fn [games [_ game-id]]
   (get-in games [game-id :player])))
`

eskemojoe00721:10:19

But then I'm recomputing everytime any game changes, not just my specific game.

Vachi21:10:29

yeah, it would be better if it was possible, but I think it is not possible

eskemojoe00721:10:07

Hmmm...It works if any other changed to the DB made after, but not if that's the only change.

Vachi21:10:15

;; one sugar pair
                         2 (let [[marker vec] input-args]
                             (when-not (= :<- marker)
                               (console :error err-header "expected :<-, got:" marker))
                             (fn inp-fn
                               ([_] (subscribe vec))
                               ([_ _] (subscribe vec)))) 

Vachi21:10:30

from the source, it looks like that you can

Vachi21:10:50

:<- [:games game-id]

eskemojoe00721:10:26

So that is exactly the same as I'm doing?

Vachi21:10:46

I think it is no possible, since you can't pass the below game-id to the signal function

Vachi21:10:59

(fn inp-fn
                               ([_] (subscribe vec))
                               ([_ _] (subscribe vec))))  

Vachi21:10:51

it just calls subscribe vec , but the vec has no idea of the value of game-id, because that is supplied in the compute-fn

Vachi21:10:04

you need some how to get the value of game-id and put in the vector of the signal-fn

eskemojoe00721:10:24

Which I've done I thought?

Vachi21:10:55

(fn [[_ game-id] _] <- the parameter is ignored
   (rf/subscribe [:games/game game-id <- this is nil ])) 

lilactown21:10:42

an easy way to verify would be to console.log the parameters passed to the signal function

lilactown21:10:46

the docs are not clear at all about this

eskemojoe00721:10:48

Signal function takes in [query-vec dynamic-vec] where the computation function takes in [db query-function]

Vachi21:10:18

yeah, put a console.log in the signal fn, my guess is that is nil

eskemojoe00721:10:56

Not nil. that is working properly.

eskemojoe00721:10:24

It works great, just doesn't think it needs to update when the app-db changes. Then when the app-db changes more, its smart enough to go update them.

eskemojoe00721:10:22

For example: if I dispatch the player changes, app-db gets updated but subscriptions dont. Then if I dispatch something completely different (like a route) the subscription updates.

Vachi21:10:57

uhmm, interesting

eskemojoe00721:10:07

Yeah...I'm baffled.

eskemojoe00721:10:19

Since that part of re-frame is pretty automatic.

lilactown21:10:20

could be a bug

eskemojoe00721:10:06

You know what...the base games/games isn't updating properly either.

😮 4
Vachi21:10:01

silly question , but did you deref them in the view? if it's lazyseq, can you try to wrap in a doall?

eskemojoe00722:10:38

My god. I feel dumb.