mpdairy / posh

A luxuriously simple and powerful way to make front-ends with DataScript and Reagent in Clojure.
Eclipse Public License 1.0
460 stars 45 forks source link

Pull queries do not update if their nested entities change #26

Open seantempesta opened 7 years ago

seantempesta commented 7 years ago

Ran into this bug today. I had a (pull conn '[*] eid) that wasn't updating when a nested entity within that pull changed. I'm guessing it's because the datom match pattern is just for the specified eid (and not all returned entities)?

Anyway, I setup an example showing the bug here: https://github.com/seantempesta/posh-bug

The jist of it is this:

(def schema {;; Contact component (shared across entities)
             :contact/email {:db/cardinality :db.cardinality/one}
             :contact/phone {:db/cardinality :db.cardinality/one}

             ;; Users
             :user/username {:db/unique      :db.unique/identity
                             :db/cardinality :db.cardinality/one}
             :user/contact  {:db/valueType   :db.type/ref
                             :db/cardinality :db.cardinality/one
                             :db/isComponent true}})
(def conn (d/create-conn schema))
(posh! conn)

(transact! conn [{:db/id         -1
                  :user/username "seantempesta"
                  :user/contact  {:db/id         -2
                                  :contact/phone "555-5555"
                                  :contact/email "sean.tempesta@gmail.com"}}])

;; -------------------------
;; Views

(defn home-page []
  (let [user (pull conn '[*] [:user/username "seantempesta"])
        phone (pull conn '[*] 2)]
    [:div [:h2 "Welcome to posh-bug!"]

     [:div [:h4 {} "User Entity"]
      [:h5 {} @user]]

     [:div [:h4 {} "Phone Entity"]
      [:h5 {} @phone]]

     [:input {:type     "button" :value "Remove phone number"
              :on-click #(transact! conn [[:db/retract 2 :contact/phone "555-5555"]])}]]))
screen shot 2017-04-30 at 11 44 37 am
seantempesta commented 7 years ago

So, I'm really not familiar enough with Posh's internals to fix this. I thought I saw somewhere that Posh might bring back manual specification of listen patterns? At least then I could work around this bug.

seantempesta commented 7 years ago

Temporary workaround. I found if you explicitly expand isComponent pull patterns the pull analyze picks up the changes.

So, in the example above, you'd have to do something like this:

(pull conn '[:db/id, :user/username, {:user/contact [:db/id, :contact/email, :contact/phone]}] 2)
metasoarous commented 7 years ago

Yes; it should certainly be looking at isComponent attributes. I'm flagging as bug. Thanks for reporting.

eoliphan commented 6 years ago

@seantempesta was that all you had to do? I've tried the explicit expansion, still no joy for my reactions

seantempesta commented 6 years ago

@eoliphan: It worked for my scenario at that time. No idea if it's working now as I'm no longer using Posh.

eoliphan commented 6 years ago

@seantempesta ok, thanks. Yeah, i'm looking at alternatives as well, I really wanted to use DS as I've got Datomic on the server, but probably will just go with specter and maps, etc. for now until I can maybe look at figuring out the posh code and doing some hacking.

tkachenko1503 commented 4 years ago

Hello guys! I've got the same issue with nested components. Are there some ways to solve this issue? I can try to help with it if someone has an idea how to fix this bug.