Fork me on GitHub
#re-frame
<
2017-01-06
>
qqq05:01:46

I'm building an app with multiple different demos. Should they all use the same database? (a bit weird since they're "different demos", but all stuck in the same single page application)

vikeri11:01:05

@mattly @mikethompson I’m also in need of an elegant solution to accessing subscription values in handlers. Currently doing the hacky @(subscribe [:key]) in the handler itself… And the alternative, to subscribe in the component calling the dispatch seems cumbersome. The component would have to update each time a subscription changes when it in reality only needs the newest value when the dispatch function is called. Also, since I’m using React Native I dispatch from a lot of places that is not in a component (Callbacks from misc HW functions).

akiroz17:01:48

Same here, my current way of handling derrived data requiring computation is through an interceptor. Whenever the source data is updated, my interceptor will compute the derrived data and stick it into my app-db as well.

akiroz17:01:26

Would be nice if there's a better way to handle getting derrived data in both events and views but I don't really have any suggestions to offer.....

notanon17:01:27

interesting @akiroz can you ensure the interceptor runs before anything attempts to use the now outdated derived data?

akiroz17:01:10

yes, the interceptor has an :after function that does the computation right after your event handler so it's completely synchronous.

notanon17:01:38

thats awesome

notanon17:01:10

forgive the newbie questions 🙂 i just started looking at reframe a couple days ago.

akiroz17:01:18

just ensure that every event that modifies the source data has this interceptor injected, I spent almost an hour wondering why my derrived data is out of sync when I added a new event lol

akiroz17:01:39

so I don't think this is the most elegant solution

notanon17:01:29

what is preventing the event handler from calculating the correct derived data?

akiroz17:01:36

don't worry, I'm pretty much a noob myself. I'm 2 months into my first serious re-frame project.

notanon17:01:52

in the land of the blind, the one-eyed man is king 😉

akiroz17:01:34

you can actually do the computation inside the event handler as well, I just used an interceptor for convenience.

akiroz17:01:58

it's a state syncronization helper.

notanon17:01:15

it sounds like vikeri's use case could be a substantial performance improvement under specific scenarios

notanon17:01:28

if following your pattern

akiroz17:01:00

I think performance-wise both methods should be about the same because the computation is done whenever the source data is updated, only difference is that the derrived data is either stored in the app-db or a reactive atom.

notanon17:01:44

i see. i misread vikeri's message.

notanon18:01:55

(unrelated to above conversation) i'm reading through the re-frame documentation

Each time application state changes, a-query-fn will be called again to compute a new materialised view (a new computation over app state) and that new value will be given to any view function which is subscribed to :some-query-id. This view function, itself, will then also be called again to compute new DOM (because it depends on a query value which changed).

notanon18:01:46

does this mean every fn registered via reg-sub is called anytime anything is modified inside app-db?

notanon18:01:38

using the terminology of the page, ALL intermediate branches (materialized views) of the tree will be recomputed every time anything changes?

notanon18:01:36

i'm assuming at least the leaves (the views) are smart enough to not re-render if their input values are the same (like in reagent)?

mattly18:01:00

@notanon this is where signals come in

mattly18:01:35

a signal is a sort of way to hook up subscriptions to other subscriptions

mattly18:01:02

and then the derived subscriptions' functions only run when their inputs change

mattly18:01:14

I tend to re-make in my projects a simple subscription :get-in:

mattly18:01:29

so then if I have a subscription f.e. (rf/subscribe [:get-in [::some-key]]) that subscription will only change when the value at that key changes

mattly18:01:01

then let's say I've got a more specific subscription that involves some degree of computation

mattly18:01:27

the second function here will only get called when the values from the subscriptions in the first function change

mattly18:01:23

but you can use any registered subscription as a signal

mattly18:01:44

not just something that pulls values out of your app state, but other computations

mattly18:01:13

and those functions are run only once when the inputs change, and then the resulting value passed to any views or other subscriptions that subscribe to it

notanon18:01:51

is this a 3-arity version of reg-sub? i'm not sure i understand the code

notanon18:01:28

the second param is returning a vector containing the result of calling rf/subscribe?

notanon18:01:46

i must not have gotten to this part of the documentation yet, let me keep looking

notanon18:01:51

i guess the fn passed in as the third param will only be called when deref'ing the 2nd param's elements return different values?

mattly18:01:45

yeah, this is the 3-arity version

mattly18:01:59

the first function (second argument) can return either a vector of subscriptions or a subscription

mattly18:01:26

and then the second function receives the values from those subscriptions

notanon18:01:42

i see, i think. in your example input-value in the 2nd function is the result of the subscribe'ing to :get-in in the first function?

notanon18:01:37

it appears it's being deref'd before it's passed in?

mattly18:01:50

think of a subscription as a wrapper around a value that can change

mattly18:01:59

similar to an atom

mattly18:01:14

derefing is basically the act of obtaining its current value

mattly18:01:52

but unlike an atom, the reactive atoms used by reagent and thus re-frame offer a mechanism to tell things when the value changes

notanon19:01:52

yeah that blows my mind. i want to figure out how they're doing that, but for right now i've been accepting it as a black-box

mattly19:01:48

it's part of a general pattern people think of as Functional Reactive Programming or Publish/Subscribe, based on who you talk to

mattly19:01:10

there are differences in the details between those two patterns, but they share a general underlying idea that's useful to understand for this

chris.hendricks20:01:10

good afternoon - I'm new to working with re-frame, but I was one of those folks whose mind was melted by the original readme and I've been looking for a place to apply it ever since. While I love the narrative docs, I've been craving some detailed reference-style docs like those discussed in issue 216 (https://github.com/Day8/re-frame/issues/216)

chris.hendricks20:01:33

I was wondering about the etiquette for maybe taking a stab at that - I've read the contributing guidelines, but I'm a little gun-shy about starting work on a pull request when I'm pretty green to the project

chris.hendricks20:01:46

Anyway, any thoughts would be appreciated

chris.hendricks22:01:37

oh absolutely, it's not a question of being able to use the library

chris.hendricks22:01:55

more like I'd like to "give back" on something that i'd have liked to see

chris.hendricks22:01:13

and that docs issue seems like something i could help with, but don't want to run afoul of any etiquette (is someone else working it, will it cause too much work if i submit something that doesn't meet the intent, stuff like that)