Skip to content

Instantly share code, notes, and snippets.

@Jared314
Created February 18, 2014 21:05
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 Jared314/9080068 to your computer and use it in GitHub Desktop.
Save Jared314/9080068 to your computer and use it in GitHub Desktop.
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]
(reify
om/IInitState
(init-state [_] {:value 0 :class ""})
om/IRenderState
(render-state [_ state]
(dom/td #js {:className (:class state)} (:value state)))
om/IWillMount
(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)
om/component))
(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)))))
cs))
(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>
<html>
<head>
<title></title>
<style>
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; }
</style>
</head>
<body>
<div id="big-table"></div>
<script src="js/main.js" type="text/javascript"></script>
</body>
</html>
(defproject processtest "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/core.async "0.1.267.0-0d7780-alpha"]
[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/main.js.map"
: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