Last active
January 3, 2016 22:39
-
-
Save jarohen/8530211 to your computer and use it in GitHub Desktop.
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
| (:require [cljs.core.async :as a] | |
| [dommy.core :as d] | |
| [clidget.widget :refer [defwidget] :include-macros true]) | |
| (:require-macros [dommy.macros :refer [node]] | |
| [cljs.core.async.macros :refer [go-loop]])) | |
| (defwidget counter-widget [{:keys [counter]} events-ch] | |
| (node | |
| [:div | |
| [:h2 "counter is now: " counter] | |
| [:p | |
| (doto (node [:button "Increment counter"]) | |
| (d/listen! :click #(a/put! events-ch :inc-counter)))]])) |
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
| (defn watch-events! [events-ch !counter] | |
| (go-loop [] | |
| (when-let [event (a/<! events-ch)] | |
| (when (= :inc-counter event) | |
| (swap! !counter inc)) | |
| (recur)))) |
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
| (set! (.-onload js/window) | |
| (fn [] | |
| (let [!counter (atom 0) | |
| events-ch (doto (a/chan) | |
| ;; 'watch-events!' implemented below | |
| (watch-events! !counter))] | |
| (d/replace-contents! (.-body js/document) | |
| (counter-widget {:!counter !counter} events-ch)])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment