Skip to content

Instantly share code, notes, and snippets.

Created Feb 18, 2014
What would you like to do?
Om with core.async cleanup
(ns processtest.core
(:require [cljs.core.async :refer [<! >! chan timeout]]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true])
(:require-macros [cljs.core.async.macros :refer [go]]))
(defn cell1 [data owner]
(init-state [_] {:value 0 :class ""})
(render-state [_ state]
(dom/td #js {:className (:class state)} (:value state)))
(will-mount [_]
(let [c (:channel data)]
(go (while true
(let [[v g] (<! c)]
(om/set-state! owner :value v)
(om/set-state! owner :class g))))))))
(defn table1 [data owner]
(->> (:update-channels data)
(partition-all (:height data))
(map #(apply dom/tr nil (om/build-all cell1 %)))
(apply dom/table nil)
(defn build-producer [m]
(let [cs (vec (repeatedly m #(hash-map :channel (chan))))]
(go (loop [i 0]
(let [g (str "group" (mod i 6))]
(dotimes [n (inc (rand-int m))]
(let [c (:channel (cs (rand-int m)))]
(>! c [(rand-int 10) g]))))
(<! (timeout (rand-int 20)))
(when (< i 1000)
(recur (inc i)))))
(def width 20)
(def height 20)
(om/root table1 {:update-channels (build-producer (* height width))
:height height
:width width}
{:target (. js/document (getElementById "big-table"))})
<!doctype html>
table {
font-family: courier;
font-size: 8px;
line-height: 1em !important;
.group0 { color: #000; }
.group1 { color: #f00; }
.group2 { color: #0f0; }
.group3 { color: #00f; }
.group4 { color: #ff0; }
.group5 { color: #0ff; }
<div id="big-table"></div>
<script src="js/main.js" type="text/javascript"></script>
(defproject processtest "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url ""
:license {:name "Eclipse Public License"
:url ""}
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/core.async ""]
[org.clojure/clojurescript "0.0-2156"]
[om "0.4.1"]]
:plugins [[lein-cljsbuild "1.0.2"]]
:cljsbuild {:builds [{:source-paths ["src-cljs"]
:compiler {:output-dir "resources/public/js"
:output-to "resources/public/js/main.js"
:optimizations :advanced
:pretty-print false
;;:source-map "resources/public/js/"
:preamble ["react/react.min.js"]
:externs ["react/externs/react.js"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment