Minor improvements on rstore.

This commit is contained in:
Andrey Antukh 2016-03-19 13:50:05 +02:00
parent 91a06f4d7e
commit 32dfaebbb9

View file

@ -56,7 +56,8 @@
(-apply-update [func state] (-apply-update [func state]
(func state))) (func state)))
(defonce bus (rx/bus)) (defonce ^:private bus (rx/bus))
(defonce stream (rx/map identity bus))
(defn emit! (defn emit!
"Emits an event or a collection of them. "Emits an event or a collection of them.
@ -64,25 +65,24 @@
([event] ([event]
(rx/push! bus event)) (rx/push! bus event))
([event & events] ([event & events]
(run! #(rx/push! bus %) (into [event] events)))) (run! emit! (cons event events))))
(defrecord SwapState [f]
UpdateEvent
(-apply-update [_ state]
(f state)))
(defn swap (defn swap
"A helper for just apply some function to state "A helper for just apply some function to state
without a need to declare additional event." without a need to declare additional event."
[f] [f]
(reify (->SwapState f))
UpdateEvent
(-apply-update [_ state]
(f state))))
(defn reset (defn reset
"A event that resets the internal state with "A event that resets the internal state with
the provided value." the provided value."
[state] [v]
(reify (->SwapState (fn [_] v)))
UpdateEvent
(-apply-update [_ _]
state)))
(enable-console-print!) (enable-console-print!)
@ -90,8 +90,7 @@
"Initializes the stream event loop and "Initializes the stream event loop and
return a stream with model changes." return a stream with model changes."
[state] [state]
(let [stream (rx/map identity bus) (let [watch-s (rx/filter watch? stream)
watch-s (rx/filter watch? stream)
effect-s (rx/filter effect? stream) effect-s (rx/filter effect? stream)
update-s (rx/filter update? stream) update-s (rx/filter update? stream)
state-s (->> update-s state-s (->> update-s