Fork me on GitHub
#re-frame
<
2020-05-14
>
lilactown18:05:15

there’s not really a good way to have a subscription that says, “update only if <some predicate>”

p-himik18:05:45

Not sure what you mean. Can you give an example?

lilactown18:05:11

I dunno, say you have an app db like:

{:number 0}
an event like:
(reg-event-db :inc
  (fn [db _]
    (update db :number inc)))
and a couple subs:
(reg-sub :even
  ,,,)

(reg-sub :odd
  ,,,)
is it possible to have :even only update when :number is even, and :odd only update when :number is odd?

lilactown18:05:22

I think this is really a question of whether reagent is capable of this

isak18:05:52

you can use an interceptor to get state to flow into the DB rather than out

isak18:05:26

Then the subs could just listen to that new derived state

lilactown18:05:08

I’m not sure I follow

isak18:05:02

For example, for any event handler that may change the number, add an on-changes interceptor that will write another piece of state

lilactown18:05:40

I’m really not chasing any particular use case for this, rather asking about the capability

lilactown18:05:02

there’s definitely ways to get around this by storing the evens and odds in other places

lilactown18:05:24

but it sounds like there’s not a way to directly do something where a change occurs and you “skip” it based on some logic

isak18:05:52

Ok. The closest concept I think of is having a dynamic graph, as explained in this talk (Incr.bind instead of Incr.map): https://www.janestreet.com/tech-talks/seven-implementations-of-incremental/

isak18:05:14

But I think re-frame/reagent only has ".map"

lilactown18:05:31

yeah. it’s really getting into things like rxjs and it’s many operators

lilactown18:05:34

great, thanks!

isak18:05:31

Have you found that you miss it? I've thought about it but haven't noticed the pain of not having it yet

lilactown18:05:15

I have in the past, yeah

4
p-himik19:05:39

Would

(reg-sub :even
  (fn [db _] (even? (:number db))))
not do what you want? Maybe I misunderstand the initial question. It's unclear what you mean by "have a sub update".

lilactown19:05:17

I want :even to over time be: 0 2 4 6

lilactown19:05:32

so it will only change when :number is even

lilactown19:05:54

your sub will change anytime :number changes and will be true false true false ….

p-himik19:05:07

So when :number transitions from 0 to 1, :even will still be 0?

p-himik19:05:36

I see. Yep, that's completely out of scope of subs.

👍 4
lilactown23:05:24

@U08JKUHA9 that talk is wonderful

mikethompson00:05:50

(reg-sub :seven: 

 ;; signal function 
 (fn []  (subscribe [:number])

 ;; computation function 
 (fn [number] 
   (if even? number (dec number)))

mikethompson00:05:13

It isn't natural, but its possible.

lilactown00:05:55

that relies on the fact that the previous value can be trivially computed from the current

mikethompson00:05:14

yeah, it relies on being able to compute the value you want from what's in app-db currently

mikethompson00:05:42

I think what you want is to have a sequence of arb values

mikethompson00:05:08

The more re-frame-y way would be to put that sequence in app-db itself. And to manage it via the logic of event handlers

lilactown00:05:33

yeah could do that. that relies on keeping some amount of old values in the app-db and managing that yourself, if you don’t want to store an arbitrarily long seq

mikethompson00:05:07

Or just storing the current :even` and :odd

lilactown00:05:35

means you have to move that logic into events/effects

lilactown00:05:49

the way I’ve started thinking about it: what if reagent allowed transducers on reactions, similar to channels

mikethompson00:05:08

That is the reframe way. We want events to be causal We want subs to calcualte a materialsed view

mikethompson00:05:29

Your materialised view is a bit on the edge

lilactown00:05:46

is filtering not a materialized view?

mikethompson00:05:21

But you aren't doing filtering

mikethompson00:05:50

Or, you are doing filtering, but you are doing it on history

mikethompson00:05:07

Which is why it works if you store the history in app-db

Wilson Velez20:05:26

do you recommend using re-frame-test or separate the content of the handlers and subscriptions to functions and test those?

aisamu20:05:47

I think of those as unit vs "integration", it depends on the specific goal. Testing a complete async-flow was simpler with re-frame-test, but straightforward handlers don't really need it. From a general standpoint, I prefer to unit test the content of the handlers because that's a smaller surface area.