From 9619fcbc1fd3c847a94e6b4e04999ea8a6c2c14a Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 4 Sep 2024 14:22:30 +0200 Subject: [PATCH] :zap: Make efficiency improvements to use-shared-state hook --- frontend/src/app/main/ui/hooks.cljs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/frontend/src/app/main/ui/hooks.cljs b/frontend/src/app/main/ui/hooks.cljs index bb05d2b1c..14bff1559 100644 --- a/frontend/src/app/main/ui/hooks.cljs +++ b/frontend/src/app/main/ui/hooks.cljs @@ -294,19 +294,21 @@ `key` for new values." [key default] (let [id (mf/use-id) - state (mf/use-state (get @storage key default)) + state* (mf/use-state #(get @storage key default)) + state (deref state*) stream (mf/with-memo [id] (->> mbc/stream (rx/filter #(not= (:id %) id)) (rx/filter #(= (:type %) key)) (rx/map deref)))] - (mf/with-effect [@state key id] - (mbc/emit! id key @state) - (swap! storage assoc key @state)) + (mf/with-effect [state key id] + (mbc/emit! id key state) + (swap! storage assoc key state)) - (use-stream stream (partial reset! state)) - state)) + (use-stream stream (partial reset! state*)) + + state*)) (defonce ^:private intersection-subject (rx/subject)) (defonce ^:private intersection-observer