Last active
January 11, 2016 23:21
-
-
Save currentoor/dcdfbe4d8e99513a4135 to your computer and use it in GitHub Desktop.
Reset the components of an entity without worrying about the current values or their entity-ids.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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
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.