-
-
Save rightfold/66ea8d6514aa740032d0 to your computer and use it in GitHub Desktop.
This file contains 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 hello.core | |
(:require [cljs.core.async :as async, :refer [<! >!]] | |
[om.core :as om, :include-macros true] | |
[om.dom :as dom, :include-macros true]) | |
(:require-macros [cljs.core.async.macros :refer [go go-loop]])) | |
(defn random-event [] | |
{:time (js/Date.) | |
:description (if (< 0.1 (.random js/Math)) "ping ok" "ping error")}) | |
(defn event-view [event owner] | |
(reify om/IRender | |
(render [_] (dom/li nil (str (:time event) (:description event)))))) | |
(defn events-view [events owner] | |
(reify om/IRender | |
(render [_] | |
(apply dom/ul nil (om/build-all event-view (take-last 10 events)))))) | |
(defn event-stats-view [event-stats owner] | |
(reify om/IRender | |
(render [_] | |
(apply dom/table nil | |
(map #(dom/tr nil (dom/th nil (% 0)) (dom/td nil (% 1))) event-stats))))) | |
(defn get-events [chan] | |
(js/setInterval #(go (>! chan (random-event))) 1000)) | |
(defn conj-from-chan [atom path chan] | |
(go-loop [] | |
(let [value (<! chan)] | |
(swap! atom #(update-in % path conj value)) | |
(recur)))) | |
(defn stats [events] | |
(frequencies (map :description events))) | |
(defn main-view [state owner] | |
(reify om/IRender | |
(render [_] | |
(dom/div nil | |
(om/build events-view (:events state)) | |
(om/build event-stats-view (stats (:events state))))))) | |
(defn main [state] | |
(let [chan (async/chan)] | |
(get-events chan) | |
(conj-from-chan state [:events] chan)) | |
(om/root main-view | |
state | |
{:target (.getElementById js/document "application")})) | |
(js/addEventListener "load" #(main (atom {:events []}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment