Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datascript -> rum/render -> should-update #28

Closed
kurt-o-sys opened this issue Apr 20, 2015 · 4 comments
Closed

datascript -> rum/render -> should-update #28

kurt-o-sys opened this issue Apr 20, 2015 · 4 comments

Comments

@kurt-o-sys
Copy link

I'm playing around with rum, using the idea of https://github.com/tonsky/datascript-chat - I'm omtting the async/chan part for this example. The application is rather heavily query-based, and I want to implement a :should-update mixin, which will update the components only if the queries are changed (sounds pretty obvious).
I have something like:

(def root (rum/mount (body) (.-body js/document)))

(defonce init (fn [] 
   (ds/listen! conn (fn [tx-report]
       (rum/request-render root)))))

(init)

Any updates in the database trigger a rum/request-render. Now, the 'should-update' mixin looks like:

(rum/defc comp < {:should-update (fn [o n] ... )} [args]  
    (let [q (ds/q ...)]
        ... ))

However, the 'old' state and the 'new' state are not the old and new state of the transaction (or the component as such), but rather some intermediate states of the component (so it seems to me). The old value is not the 'previous query result' or 'previous state which led to that query result'. What I'd like is to compare the previous result of the query with the new result of the query.

What's the most logic/best way to do this using rum?

@tonsky
Copy link
Owner

tonsky commented Apr 21, 2015

Old and new are states of component. In simplest case, it you’re not using anything fancy, state just contains arguments this component was created with ({:rum/args args}). By comparing args it was created before and args it is created now, you can decide if you need to update.

Simple way to solve your problem is to call component with query result as argument:

(comp (ds/q ...))

and use rum/static mixin which does exactly that: compares old arguments value with new arguments value.

Did I answered your question?

@kurt-o-sys
Copy link
Author

Here's how I did it so far. The calling component passes an entity, not query result. The reason is I needed the entity as well. It seemed logic to me to pass the entity only, since that would/should be enough:

(rum/defc stagelist []
    [:section 
      (let [q (->> (ds/q '[:find ?e ?o  :where [?e :stage/order ?o]  [?e :stage/name ?n]] @conn)
        (sort-by second))]   
      (map #(stage (ds/entity @conn (first %))) q))]) 

The component that's being called (the one with the query):

(rum/defc stage [ent]  
    (let [n (name (:stage/name ent))
          id (:db/id ent) 
          q (ds/q `[:find [?e ...] :where [?e :item/stage ~id]] @conn)]   
    ... ))

The passed entity itself is not changing, but the result of the query is. I didn't add any mixin in this example. I nest and use this kind of 'pass entity and perform a query' rather often, or at least, I would like to. Since the query is something 'component specific', I'd rather keep in in the component, not in the caller. This way, it makes the code easier to reason about imo - the query is bounded to the component.

@tonsky
Copy link
Owner

tonsky commented Apr 21, 2015

Well, there’s no way around it. If you want to avoid unnecessary rendering you need a component with a lifecycle around the place. In your case you probably need second component that accepts query result and implements shouldComponentUpdate. Then just include that component inside stage:

(rum/defc stage-inner < rum/static [q n]
  ...)

(rum/defc stage [ent]  
  (let [n  (name (:stage/name ent))
        id (:db/id ent) 
        q  (ds/q `[:find [?e ...] :where [?e :item/stage ~id]] @conn)]
    (stage-inner q n)))

Remember that you’re not avoiding query. You only avoiding rendering of stage-inner. Worth it only if rendering is expensive.

@kurt-o-sys
Copy link
Author

Right, I started to realize that :). Of course, the query can't be avoided, rendering can... which was my goal (just to get a hang of rum). I rather like the inner component idea. It's simple, and logic. It'll be pretty easy to make a function or macro to generate both, and possibly adding some functionality in the outer/wrapper component.

Great, thx a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants