Skip to content

Instantly share code, notes, and snippets.

@rightfold
Created January 3, 2015 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/64ba505ea4f1c6d67f36 to your computer and use it in GitHub Desktop.
Save rightfold/64ba505ea4f1c6d67f36 to your computer and use it in GitHub Desktop.
(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