Skip to content

Instantly share code, notes, and snippets.

@Jared314
Created February 18, 2014 19:11
Show Gist options
  • Save Jared314/9077746 to your computer and use it in GitHub Desktop.
Save Jared314/9077746 to your computer and use it in GitHub Desktop.
Om with core.async (1 channel per target)
(ns processtest.core
(:require [cljs.core.async :as async
:refer [<! >! chan put! 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]
(om/component
(apply dom/table nil
(for [x (partition-all (:height data) (:update-channels data))]
(apply dom/tr nil (om/build-all cell1 x))))))
(defn build-producer [m]
(let [cs (vec (repeatedly m #(hash-map :channel (chan))))]
(go (loop [i 0]
(let [g (str "group" (mod i 5))]
(dotimes [n (inc (rand-int m))]
(let [c (:channel (cs (rand-int m)))]
(>! c [(rand-int 10) g])))
(<! (timeout (rand-int 100)))
(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"))})
(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