Skip to content

Question: are cursors more update-efficient than a massive atomic hash-map? #47

@alexandergunnarson

Description

@alexandergunnarson

I often tend to put my application state in an all-encompassing one-or-two-level-deep hash-map wrapped in an atom. Let's say that a DOM element :div#a reactively (i.e., via the rx macro) changes its color based on (-> @state :div#b :style :color). Now let's also say that :div#c reactively changes its color based on (-> @state :div#d :style :color). Div A depends on div B, and C on D, but [A and B] do not affect [C and D] at all, and vice versa.

Now suppose that (swap! state assoc-in [:div#b :style :color] :white) is called. Because the value of state is atomically modified, are the colors of divs A and C considered "dirty" and thus the values must be recalculated by calling the functions which rx created for each of them respectively? Or does freactive know that only div B's color was changed and so C's does not need to be recomputed?

This brings me to the question in the title: would cursors be more efficient in this case? Instead of writing code like so:

[:div#a {:style {:color (rx (-> @state :div#b :style :color))}}]

...should I write code like this?:

[:div#a {:style {:color (let [c (cursor state (comp :color :style :div#b)]
                          (rx @c)

That is, would cursor c only be invalidated if div B's color changed and not on every modification to state?

Thanks for your time!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions