Skip to content

Instantly share code, notes, and snippets.

@manicolosi
Created November 2, 2015 16:33
Show Gist options
  • Save manicolosi/f25db6c7feb4e73f45c9 to your computer and use it in GitHub Desktop.
Save manicolosi/f25db6c7feb4e73f45c9 to your computer and use it in GitHub Desktop.
;; Async Lifecycle
(defn lifecycle-channel [component]
(.-lifecycleChannel component))
(defn with-lifecycle [klass]
(let [ch (async/chan)
handle (fn [event]
(fn [& args]
(async/put! ch event)
(when-let [f (event klass)]
(apply f args))))]
(into
{:component-will-mount
(fn [this]
(set! (.-lifecycleChannel this) ch)
((handle :component-will-mount) this))
:component-will-unmount
(fn [this]
((handle :component-will-unmount) this)
(async/close! (lifecycle-channel this))) }
(for [event [:get-initial-state :component-will-receive-props
:should-component-update :component-did-mount
:component-will-update :component-did-update :reagent-render]]
[event (handle event)]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment