-
-
Save rightfold/64ba505ea4f1c6d67f36 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 so.hard.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 events-view [events] | |
(reify om/IRender | |
(render [_] (apply dom/ul nil (map #(dom/li nil (:description %)) events))))) | |
(defn get-events [chan] | |
(js/setInterval #(go (>! chan (random-event))) 1000)) | |
(defn main [state] | |
(let [chan (async/chan)] | |
(get-events chan) | |
(go-loop [] | |
(let [event (<! chan)] | |
(swap! state #(update-in % [:events] conj event))) | |
(recur))) | |
(om/root (fn [state owner] (events-view (:events state))) | |
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