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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 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 {:index (swap! use-sub-counter inc)}]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Switched the key so it can be easily sortable since use-sub-11 would come before use-sub-2 when using default string comparison.

(subs/-add-listener sub key callback)
#(subs/-remove-listener sub key)))
(fn []
Expand Down
42 changes: 40 additions & 2 deletions core/src/refx/subs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,44 @@
(doseq [[_ sub] @sub-cache]
(dispose! sub)))

(defonce ^:private listeners-state
(letfn [(comparator [a b]
(compare (:index b) (:index a)))]
(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 [listener-fns (atom nil)]
(swap! listeners-state (fn [state]
(let [{:keys [counter pending] :as new-state}
(update state :counter dec)]
(if (zero? counter)
(do (reset! listener-fns pending)
(update new-state :pending empty))
new-state))))
(doseq [[_ f] @listener-fns]
(f)))))))

(deftype Listeners [^:mutable listeners]
Object
(empty? [_] (empty? listeners))
Expand All @@ -84,8 +122,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
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 {: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)
(remove-listeners!)
(async done
(js/setTimeout (fn []
(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.

'({:index 1}
{:index 2}
{:index 3}
{:index 4})))
(done))
10)))))