Reset the components of an entity without worrying about the current values or their entity-ids.
(defn reset-components [db eid attr new-comps] | |
(let [current-comps (-> (d/pull db [attr] eid) attr) | |
;; First we do a value based comparison ignoring component entity ids. | |
current-comp-vals (into #{} (map #(dissoc % :db/id) current-comps)) | |
new-comp-vals (into #{} new-comps) | |
deletions (clojure.set/difference current-comp-vals new-comp-vals) | |
additions (clojure.set/difference new-comp-vals current-comp-vals) | |
;; Then we retract components based on value comparison above. | |
deletion-txn (->> (filter (fn [m] (contains? deletions | |
(dissoc m :db/id))) | |
current-comps) | |
(map (fn [e] [:db.fn/retractEntity (:db/id e)]))) | |
addition-txn [{:db/id eid attr additions}]] | |
(concat deletion-txn addition-txn))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Say you have an entity with a component
You can reset this entity's component like so
This function will generate the appropriate addition/retraction transactions.