Skip to content

Instantly share code, notes, and snippets.

@maxthoursie
Created August 22, 2014 19:23
Show Gist options
  • Save maxthoursie/66530eaad63b05dd9f76 to your computer and use it in GitHub Desktop.
Save maxthoursie/66530eaad63b05dd9f76 to your computer and use it in GitHub Desktop.
Ex10 with :start-stop event
(defn ex10 [animals]
(let [start-stop-button (by-id "ex10-button-start-stop")
prev-button (by-id "ex10-button-prev")
next-button (by-id "ex10-button-next")
start-stop (events->chan start-stop-button EventType.CLICK
(chan 1 (map (constantly :start-stop))))
prev (events->chan prev-button EventType.CLICK
(chan 1 (map (constantly :previous))))
next (events->chan next-button EventType.CLICK
(chan 1 (map (constantly :next))))
max-idx (dec (count animals))
set-html! (partial set-html! "ex10-card")]
(go
;; wait to start
(<! start-stop)
;; start listening to key events now
(let [keys (keys-chan)
actions (async/merge [start-stop prev next keys])]
(set! (.-innerHTML start-stop-button) "Stop!")
(loop [idx 0]
(style-buttons! idx max-idx prev-button next-button)
(set-html! (nth animals idx))
;; wait for next action
(condp = (<! actions)
:start-stop (do
(events/removeAll js/window EventType.KEYDOWN)
(disable-buttons! [start-stop-button prev-button next-button])
(set-html! ""))
:previous (if (pos? idx)
(recur (dec idx))
(recur idx))
:next (if (< idx max-idx)
(recur (inc idx))
(recur idx))
(recur idx)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment