Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(ns async_match
(:require [cljs.core.async :refer [chan sliding-buffer]]
[clojure.string :as string])
[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]
(>! mc {:type :mouse :loc [(.-x e) (.-y e)]}))))
(.addEventListener js/window "keyup"
(fn [e]
(>! 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))
(while true
(handler (alts! [mc kc]) )))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.