Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created July 1, 2013 19:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swannodette/5903968 to your computer and use it in GitHub Desktop.
Save swannodette/5903968 to your computer and use it in GitHub Desktop.
(ns async_match
(:require [cljs.core.async :refer [chan sliding-buffer]]
[clojure.string :as string])
(:require-macros
[cljs.core.async.macros :as m :refer [go alts!]]
[clojure.core.match.js :refer [match]]))
(def mc (chan (sliding-buffer 1)))
(def loc-div (.getElementById js/document "location"))
(def kc (chan (sliding-buffer 1)))
(def key-div (.getElementById js/document "key"))
(.addEventListener js/window "mousemove"
(fn [e]
(go
(>! mc {:type :mouse :loc [(.-x e) (.-y e)]}))))
(.addEventListener js/window "keyup"
(fn [e]
(go
(>! kc {:type :key :char (.fromCharCode js/String (.-keyCode e))}))))
(defn set-html [el s]
(aset el "innerHTML" s))
(defn handler [[e c]]
(match [e]
[{:type :mouse :loc loc}] (set-html loc-div (string/join ", " loc))
[{:type :key :char char}] (set-html key-div char)
:else nil))
(go
(while true
(handler (alts! [mc kc]) )))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment