Last active
August 29, 2015 14:04
-
-
Save pleasetrythisathome/38717e93b09b257393a9 to your computer and use it in GitHub Desktop.
Go block component lifecycle mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmixin go-block-aware | |
(init-state [this] | |
{:chans {:mounted (async/chan)}}) | |
(will-unmount [this] | |
(async/close! (om/get-state owner [:chans :mounted]))) | |
(go [owner read-chan callback] | |
(let [mounted (om/get-state owner [:chans :mounted])] | |
(go-loop [] | |
(when-some [v (first (async/alts! [read-chan exit-chan]))] | |
(callback v) | |
(recur)))))) | |
;; use with mult/tap | |
(defn create-mults [chans] | |
{:chans chans | |
:mults (zipmap (keys chans) (map mult (vals chans)))}) | |
(defn get-shared-chan [owner key] | |
(om/get-shared owner [:chans key])) | |
(defn tap-shared-chan | |
([owner key] (tap-shared-chan owner key (async/chan))) | |
([owner key ch] | |
(tap (om/get-shared owner [:mults key]) ch))) | |
(om/root app-view app-state {:target (.getElementById js/document "app") | |
:shared (create-mults {:nav (chan) | |
:events (chan)})}) | |
;; use in component | |
(defcomponentk a-component [owner] | |
(:mixins go-block-aware) | |
(did-mount [_] | |
(.go owner (tap-shared-chan owner :nav) (fn [v] ...))) | |
(render [_] | |
...)) | |
;; i like to submit tuples or longer vectors and destructure with a handler like so | |
(fn [[action & args] | |
(case action | |
:login (let [[id] args] | |
(do-login id))]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment