Skip to content

Instantly share code, notes, and snippets.

@BorisKourt
Last active January 1, 2016 08:19
Show Gist options
  • Save BorisKourt/8117764 to your computer and use it in GitHub Desktop.
Save BorisKourt/8117764 to your computer and use it in GitHub Desktop.
Working with Async and Enfocus. Looks like this is ready to use!
(ns enasync.site
(:require [enfocus.core :as ef]
[enfocus.effects :as effects]
[enfocus.events :as ev]
[cljs.core.async :refer [<! !> chan pub sub put!]])
(:require-macros [enfocus.macros :as em]
[cljs.core.async.macros :refer [go go-loop]]))
(def log #(.log js/console %))
;; The following three lines make the bus do its think, courtesy of Creighton
(def event-bus (chan))
(def event-bus-pub (pub event-bus :topic))
(defn sub-event-bus [topic ch]
(sub event-bus-pub topic ch))
(defn aslisten
"Passes element clicks to a specified channel"
[ch topic type]
(fn [el] ;; Supports event chaning in Enfocus?
(let [out ch]
(ef/at el (ev/listen type
#(let [target (.-currentTarget %)]
;;(.preventDefault %) ;;<- if you, like me, weren't sure where to put this if needed.
(put! out {:topic topic :data target}))))
out)))
;; The element to watch
(ef/at ".site-heading h1 a" (aslisten event-bus :menu-click :click))
;; Do something when the event-bus has this event.
(let [event-stream (chan)]
(sub-event-bus :menu-click event-stream)
(go (while true
(log (<! event-stream)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment