Skip to content

Instantly share code, notes, and snippets.

@rm-hull
Last active January 4, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rm-hull/8617445 to your computer and use it in GitHub Desktop.
Save rm-hull/8617445 to your computer and use it in GitHub Desktop.
Demonstration of using Swannodette's OM: reactively capturing a stream of mouse move events.
(ns examples.mouse.core
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [goog.events :as events]
[cljs.core.async :as async :refer [>! <! put! chan]]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[dommy.core :refer [insert-after!]])
(:use-macros [dommy.macros :only [sel1 node]])
(:import [goog.events EventType]))
; Adjusted slightly from the original to insert an "app" div
(->>
(sel1 :#canvas-area)
(insert-after! (node [:div#app])))
(defn listen [el type]
(let [out (chan)]
(events/listen el type #(put! out %))
out))
(om/root
{:mouse nil}
(fn [app node]
(reify
om/IWillMount
(will-mount [_]
(let [mouse-chan
(async/map
(fn [e] [(.-clientX e) (.-clientY e)])
[(listen js/window EventType/MOUSEMOVE)])]
(go (while true
(om/update! app assoc :mouse (<! mouse-chan))))))
om/IRender
(render [_]
(dom/p nil
(when-let [pos (:mouse app)]
(pr-str (:mouse app)))))))
(.getElementById js/document "app"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment