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

Make add/remove refs based on ref parent states and paths instead of contains? check, keep the valid refs after the to-cursor -> adapt cycle #364

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/om/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
(when-not (zero? (count refs))
(aset cstate "__om_refs"
(into #{}
(filter nil?
(remove nil?
(map
(fn [ref]
(let [ref-val (value ref)
Expand Down Expand Up @@ -808,6 +808,9 @@
[path] (fnil identity (atom {})))
path)]
(specify cursor
IAdapt
(-adapt [_ other]
(ref-cursor other))
ICursorDerive
(-derive [this derived state path]
(let [cursor' (to-cursor derived state path)]
Expand All @@ -831,17 +834,25 @@
(commit! cursor korks f)
(-refresh-deps! cursor))))))

(defn same-ref? [a b]
(and
(identical? (-state a) (-state b))
(= (-path a) (-path b))))

(defn have-ref? [refs ref]
(some #(same-ref? ref %) refs))

(defn add-ref-to-component! [c ref]
(let [state (.-state c)
refs (or (aget state "__om_refs") #{})]
(when-not (contains? refs ref)
(when-not (have-ref? refs ref)
(aset state "__om_refs" (conj refs ref)))))

(defn remove-ref-from-component! [c ref]
(let [state (.-state c)
refs (aget state "__om_refs")]
(when (contains? refs ref)
(aset state "__om_refs" (disj refs ref)))))
(when (have-ref? refs ref)
(aset state "__om_refs" (remove #(same-ref? ref %) refs)))))

(defn observe
"Given a component and a reference cursor have the component observe
Expand Down