Created
July 1, 2013 19:52
-
-
Save swannodette/5903968 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
(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