Skip to content

Instantly share code, notes, and snippets.

@abp
Forked from bodil/README.md
Created July 31, 2013 11:51

Revisions

  1. @bodil bodil revised this gist Jul 31, 2013. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    core.async example thing
    ========================

    To run:

    * checkout https://github.com/clojure/core.async and install it using `lein install`
    * make a project directory for this code
    * put `omgcoreasync.cljs` in a `src` subdirectory, `project.clj` and `index.html` in the root
    * go `lein cljsbuild once` to build
    * load `index.html` in a browser and watch it go
  2. @bodil bodil created this gist Jul 31, 2013.
    13 changes: 13 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>core.async demo</title>
    <meta name="viewport" content="width=device-width">
    <script src="js/main.js"></script>
    </head>
    <body>
    <h1>horse_ebooks knows what you did</h1>
    </body>
    </html>
    29 changes: 29 additions & 0 deletions omgcoreasync.cljs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    (ns omgcoreasync
    (:require-macros [cljs.core.async.macros :refer [go alt!]])
    (:require [cljs.core.async :as async]))

    (def counter (atom 0))

    (defn parse-data [data]
    (let [data (js->clj data)
    children ((data "data") "children")]
    (map (fn [child] ((child "data") "title")) children)))

    (defn jsonp-get [url]
    (let [callback-name (str "mycallback" (swap! counter inc))
    channel (async/chan)]
    (aset js/window callback-name
    (fn [data]
    (async/put! channel data)))
    (let [script (.createElement js/document "script")]
    (aset script "src" (str url "?jsonp=" callback-name))
    (.appendChild document/head script))
    channel))

    (go
    (let [c (jsonp-get "http://www.reddit.com/r/clojure.json")
    d (jsonp-get "http://www.reddit.com/r/dogfort.json")
    h (jsonp-get "http://www.reddit.com/r/haskell.json")]
    (while true
    (alt!
    [c d h] ([val ch] (.write js/document (str (parse-data val)) ch))))))
    10 changes: 10 additions & 0 deletions project.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    (defproject omgcoreasync "0.1.0-SNAPSHOT"
    :dependencies [[core.async "0.1.0-SNAPSHOT"]]
    :plugins [[lein-cljsbuild "0.3.2"]]
    :cljsbuild {:builds
    [{:source-paths ["src"],
    :compiler
    {:output-to "js/main.js",
    :output-dir "js",
    :optimizations :simple,
    :jar true}}]})