Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Created May 2, 2014 21:58
Show Gist options
  • Save loganlinn/270d65c49557edf77081 to your computer and use it in GitHub Desktop.
Save loganlinn/270d65c49557edf77081 to your computer and use it in GitHub Desktop.
WithChannelsMixin
(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