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

batch listener notifications #11

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion core/src/refx/hooks.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
(react/useMemo
(fn []
[(fn [callback]
(let [key (str "use-sub-" (swap! use-sub-counter inc))]
(let [key {::subs/index (swap! use-sub-counter inc)}]
(subs/-add-listener sub key callback)
#(subs/-remove-listener sub key)))
(fn []
Expand Down
50 changes: 48 additions & 2 deletions core/src/refx/subs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,48 @@
(doseq [[_ sub] @sub-cache]
(dispose! sub)))

(defonce ^:private listeners-state
(letfn [(comparator [a b]
(compare (::index a) (::index b)))]
(atom {:counter 0 :pending (sorted-map-by comparator)})))

(defn- invoke-listener
"This function is responsible for ensuring that signal listeners
(from DynamicSubs) are called before triggering regular listeners
(eg: added via use-sub hook). Listeners are triggered in the order they
were registered. This function ensures that one db update will only trigger
a single render."
[listener-key listener-fn]
(let [listener-fn-this-tick (atom nil)]
(swap! listeners-state (fn [state]
(let [new-state (update state :counter inc)]
(if (signal? listener-key)
;; For the case of DynamicSub, we need to call its
;; listener this tick to trigger dependent subs
(do (reset! listener-fn-this-tick listener-fn)
new-state)
(update new-state :pending assoc listener-key listener-fn)))))

(when-let [f @listener-fn-this-tick]
(f))

(interop/next-tick
(fn []
(let [{:keys [counter pending]}
(swap! listeners-state update :counter dec)]

(when (zero? counter)
(doseq [[listener-key _] pending]
;; Triggering a listener-fn can result in a subsequent sub's
;; remove-listener to be called (which will remove it from pending).
;; This check ensure it's still pending.
(let [listener-fn (atom nil)]
(swap! listeners-state (fn [state]
(reset! listener-fn (get-in state [:pending listener-key]))
(update state :pending dissoc listener-key)))
(when-let [f @listener-fn]
(f))))))))))

(deftype Listeners [^:mutable listeners]
Object
(empty? [_] (empty? listeners))
Expand All @@ -84,8 +126,8 @@
(remove [_ k]
(set! listeners (dissoc listeners k)))
(notify [_]
(doseq [[_ f] listeners]
(f))))
(doseq [[k f] listeners]
(invoke-listener k f))))

(defn- make-listeners []
(Listeners. nil))
Expand Down Expand Up @@ -122,6 +164,8 @@
(-add-listener [_ k f]
(.add listeners k f))
(-remove-listener [this k]
(when-not (signal? k)
(swap! listeners-state update :pending dissoc k))
(.remove listeners k)
(when (.empty? listeners)
(sub-orphaned this)))
Expand Down Expand Up @@ -200,6 +244,8 @@
(-add-listener [_ k f]
(.add listeners k f))
(-remove-listener [this k]
(when-not (signal? k)
(swap! listeners-state update :pending dissoc k))
(.remove listeners k)
(when (.empty? listeners)
(sub-orphaned this)))
Expand Down
36 changes: 36 additions & 0 deletions core/test/refx/subs_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,39 @@
(is (empty? @subs/sub-cache))
(done))
10)))))

(deftest listener-ordering
(let [source (atom 0)]
(subs/register :a (constantly source) identity)
(subs/register :b (constantly source) identity)
(subs/register :c (constantly (subs/sub [:b (subs/sub [:a])])) identity)
(subs/register :d (constantly [(subs/sub [:b]) (subs/sub [:c])]) identity)
(let [sub-a (subs/sub [:a])
sub-b (subs/sub [:b])
sub-c (subs/sub [:c])
sub-d (subs/sub [:d])
listener-count (atom 0)
listener-calls (atom [])
remove-listener-fns (atom '())
add-listener! (fn [sub]
(let [key {::subs/index (swap! listener-count inc)}]
(subs/-add-listener sub key #(swap! listener-calls conj key))
(swap! remove-listener-fns conj #(subs/-remove-listener sub key))))
remove-listeners! (fn []
(doseq [f @remove-listener-fns]
(f)))]
(add-listener! sub-a)
(add-listener! sub-b)
(add-listener! sub-c)
(add-listener! sub-d)
(reset! source 1)
(async done
(js/setTimeout (fn []
(remove-listeners!)
(is (= @listener-calls
Copy link
Collaborator Author

@dehli dehli Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without these changes, this test fails and listener-calls has a value of:

[{::subs/index 4}
 {::subs/index 3}
 {::subs/index 1}
 {::subs/index 4}
 {::subs/index 2}]

Note that order isn't guaranteed and 4 occurs twice which would translate to an extra render.

[{::subs/index 1}
{::subs/index 2}
{::subs/index 3}
{::subs/index 4}]))
(done))
10)))))