Forked from manicolosi/reagent-lifecycle-async.cljs
Last active
November 3, 2015 18:04
Revisions
-
joelash revised this gist
Nov 3, 2015 . 1 changed file with 22 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,26 +1,42 @@ ;; Async Lifecycle (defn lifecycle-channel [component] (let [tap-ch (async/chan)] (-> (.-lifecycleChannel component) :mult (async/tap tap-ch)) tap-ch)) (defn with-lifecycle [klass] (let [ch (async/chan) mult (async/mult ch) tap-ch (async/chan) handle (fn [event & extra-args] (fn [& args] (async/put! ch event) (when-let [f (event klass)] (apply f (concat extra-args args)))))] (async/tap mult tap-ch) (into {:component-will-mount (fn [this] (set! (.-lifecycleChannel this) {:ch ch :mult mult}) ((handle :component-will-mount) this)) :component-will-unmount (fn [this] ((handle :component-will-unmount) this) (async/untap-all mult) (async/close! ch)) :reagent-render (fn [& args] (async/put! ch :reagent-render) (when-let [f (or (:reagent-render-with-lifecycle klass) (:reagent-render klass))] (apply f (cond->> args (:reagent-render-with-lifecycle klass) (cons tap-ch)))))} (for [event [:get-initial-state :component-will-receive-props :should-component-update :component-did-mount :component-will-update :component-did-update]] [event (handle event)])))) -
manicolosi created this gist
Nov 2, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ ;; 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)]))))