Created
May 2, 2014 21:58
-
-
Save loganlinn/270d65c49557edf77081 to your computer and use it in GitHub Desktop.
WithChannelsMixin
This file contains hidden or 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
(ns with-channels | |
(:require | |
[om.core :as om] | |
[cljs.core.async :as async])) | |
(defn get-channels [owner] | |
(assert (.-state owner) "Component state not found. Are you calling from getInitialState?") | |
(aget (.-state owner) "__with-channels_channels")) | |
(defn chan | |
([owner] (chan owner nil)) | |
([owner buf-or-n] | |
(let [chs (get-channels owner) | |
ch (async/chan buf-or-n)] | |
(.push chs ch) | |
ch))) | |
(def WithChannelsMixin | |
#js {:getInitialState | |
(fn [] | |
#js {"__with-channels_channels" #js []}) | |
:componentWillUnmount | |
(fn [] | |
(this-as this | |
(doseq [ch (array-seq (get-channels this))] | |
(async/close! ch))))}) | |
(def WithChannels | |
(let [obj (om/specify-state-methods! (clj->js om/pure-methods))] | |
(aset obj "mixins" #js [WithChannelsMixin]) | |
(js/React.createClass obj))) | |
(defn with-channels-ctor [obj] (WithChannels. obj)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment