From 1091ffa27af1bb5eb2dabacdb5a630fc96db1223 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Sun, 12 Jun 2016 13:26:16 +0300 Subject: [PATCH] Make state initialization parametrizable. --- src/uxbox/data/auth.cljs | 4 ++-- src/uxbox/state.cljs | 24 +++++++----------------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/uxbox/data/auth.cljs b/src/uxbox/data/auth.cljs index ebdbf1359..61f941ea8 100644 --- a/src/uxbox/data/auth.cljs +++ b/src/uxbox/data/auth.cljs @@ -49,7 +49,7 @@ (defrecord Login [username password] rs/UpdateEvent (-apply-update [_ state] - (merge state (dissoc (st/get-initial-state) :route))) + (merge state (dissoc (st/initial-state) :route))) rs/WatchEvent (-apply-watch [this state s] @@ -72,7 +72,7 @@ rs/UpdateEvent (-apply-update [_ state] (swap! storage dissoc :uxbox/auth) - (merge state (dissoc (st/get-initial-state) :route))) + (merge state (dissoc (st/initial-state) :route))) rs/WatchEvent (-apply-watch [_ state s] diff --git a/src/uxbox/state.cljs b/src/uxbox/state.cljs index 129965e6b..bf51b006f 100644 --- a/src/uxbox/state.cljs +++ b/src/uxbox/state.cljs @@ -15,14 +15,13 @@ (enable-console-print!) (defonce state (atom {})) +(defonce loader (atom false)) (def auth-l (-> (l/key :auth) (l/focus-atom state))) -(def loader (atom false)) - -(defn- get-initial-state +(defn initial-state [] {:dashboard {:project-order :name :project-filter "" @@ -40,19 +39,10 @@ :projects-by-id {} :pages-by-id {}}) -(defn- on-error - [error] - ;; Disable loader in case of error. - (reset! loader false)) - -(rs/add-error-watcher :test on-error) - -(defonce stream - (rs/init (get-initial-state))) - (defn init "Initialize the state materialization." - [] - (as-> stream $ - (rx/dedupe $) - (rx/to-atom $ state))) + ([] (init initial-state)) + ([& callbacks] + (-> (reduce #(merge %1 (%2)) nil callbacks) + (rs/init) + (rx/to-atom state))))