-
-
Save mmower/88353e592f169bef7df9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; my basic attempt at doing core.async in clojurescript adapted from | |
; http://rigsomelight.com/2013/07/18/clojurescript-core-async-todos.html | |
; | |
; project.clj | |
(defproject jsa "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.0-SNAPSHOT"] | |
[org.clojure/clojurescript "0.0-1859"] | |
[jayq "2.4.0"]] | |
:plugins [[lein-cljsbuild "0.3.2"]] | |
:cljsbuild {:builds [{:source-paths ["src/cljs"] | |
:compiler {:pretty-print true | |
:output-to "resources/public/compiled.js", | |
:externs ["resources/public/jquery-1.10.2.js"] | |
:optimizations :simple}}]} | |
:repositories {"sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"}) | |
; jsa/core.cljs | |
(ns jsa.core | |
(:require [cljs.core.async :as async :refer [chan put! <! go]] | |
[jayq.core :refer [$ append ajax inner css $deferred | |
when done resolve pipe on bind attr | |
offset] :as jq])) | |
(.write js/document "<p>Hello CLJS world!</p>") | |
(defn click-chan | |
[selector msg-name] | |
(let [rc (chan)] | |
(on ($ "body") :click selector {} | |
(fn [e] | |
(jq/prevent e) | |
(put! rc msg-name))))) | |
(defn app-loop [] | |
(let [in-chan (click-chan "a.click-me" :got-click)] | |
(go | |
(loop [count 1] | |
(when-let [msg (<! in-chan)] | |
(.write js/document (str "<p>Got " msg " click-" count "</p>")) | |
(recur (inc count))))))) | |
(app-loop) | |
; brower console | |
Uncaught Error: Assert failed: <! used not in (go ...) block | |
nil compiled.js:14625 | |
cljs.core.async._LT__BANG_ compiled.js:14625 | |
(anonymous function) compiled.js:16457 | |
jsa.core.app_loop compiled.js:16464 | |
(anonymous function) compiled.js:16466 | |
; analysis | |
; | |
; my only explicit use of <! is in the app-loop function and within a (go ...) block. | |
; | |
; searching I came across: http://martintrojer.github.io/clojure/2013/07/17/non-tailrecursive-functions-in-coreasync/ | |
; which seems to be saying that core.async can handle loop/recur forms. | |
; | |
; i'm left rather puzzled but my experience of clojure, clojurescript, and core.async is minimal so far ... probably | |
; i've done something stupid. any help much appreciated. | |
; | |
; matt | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment