Skip to content

Instantly share code, notes, and snippets.

@prio
Last active December 28, 2015 18:58
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 prio/7546572 to your computer and use it in GitHub Desktop.
Save prio/7546572 to your computer and use it in GitHub Desktop.
Port of flpjax example to core.async
(ns calc.core
(:require [dommy.core :as dom]
[cljs.core.async :refer [>! <! chan put!]])
(:require-macros [cljs.core.async.macros :refer [alt! go-loop]])
(:use-macros [dommy.macros :only [sel sel1]]))
(defn setup [id]
(let [el (sel1 id)
out (chan)
put-value-on-channel! #(put! out (js/parseFloat (dom/value el)))]
(dom/listen! el :blur put-value-on-channel!)
(put-value-on-channel!)
out))
(defn ^:export start []
(let [n1 (setup :#n1)
n2 (setup :#n2)
sum (sel1 :#sum)]
(go-loop [x (<! n1) y (<! n2)]
(dom/set-html! sum (+ x y))
(alt!
n1 ([x'] (recur x' y))
n2 ([y'] (recur x y'))))))
<html>
<body onload="calc.core.start()">
<h3>Demo</h3>
<input type="test" id="n1" value="0"/> +
<input type="text" id="n2" value="0"/> =
<span id="sum">0</span>
<script src="out/goog/base.js" type="text/javascript"></script>
<script src="calc.js" type="text/javascript"></script>
<script type="text/javascript">goog.require("calc.core");</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment