         (let [[v c] (alts! [screen-state-chan timer-chan cursor-blink-chan stop-playback-chan])]
           (condp = c
             timer-chan (let [t (+ start-at (elapsed-time))]
-                         (dispatch [:update-state assoc :current-time t])
+                         (dispatch state [:update-state assoc :current-time t])
                          (recur cursor-blink-chan))
             cursor-blink-chan (do
-                                (dispatch [:update-state assoc-in [:cursor :on] v])
+                                (dispatch state [:update-state assoc-in [:cursor :on] v])
                                 (recur cursor-blink-chan))
             screen-state-chan (if v
                                 (do
-                                  (dispatch [:update-state #(-> % (update-screen v) reset-blink)])
+                                  (dispatch state [:update-state #(-> % (update-screen v) reset-blink)])
                                   (recur (make-cursor-blink-chan)))
-                                (dispatch [:finished]))
+                                (dispatch state [:finished]))
             stop-playback-chan nil))) ; do nothing, break the loop
-      (dispatch [:update-state reset-blink]))
+      (dispatch state [:update-state reset-blink]))
     (-> state
         (update-screen (screen-state-at (:frames state) start-at))
         (assoc :stop stop-fn))))


(defn handle-toggle-play
  "Toggles the playback. Fetches frames if they were not loaded yet."
  [state dispatch]
  (if (contains? state :frames)
    (if (contains? state :stop)
      (stop-playback state)
      (start-playback state dispatch))
    (fetch-asciicast state dispatch)))

(defn handle-seek
  "Jumps to a given position (in seconds)."
  [state dispatch [position]]
  (let [new-time (* position (:duration state))
        screen-state (screen-state-at (:frames state) new-time)
        playing? (contains? state :stop)]

  [state]
  (let [events (chan)
        mouse-moves (chan (dropping-buffer 1))
        user-activity (activity-chan mouse-moves 3000)]
    (go-loop []
      (let [[event-name & _ :as event] (<! events)]
        (if (= event-name :mouse-move)
          (put! mouse-moves true)
          (swap! state process-event event))
        (recur)))
    (go-loop []
      (swap! state assoc :show-hud (<! user-activity))
      (recur))
    (when (:auto-play @state)
      (put! events [:toggle-play]))
    (swap! state assoc :event-ch events)))

(defn create-player-with-state
  "Creates the player with given state, mounting top Reagent component in DOM."
  [state dom-node]
  (let [view-event-handler (fn [event]
                             (dispatch @state event)
                             nil)]
    (start-event-loop! state)
    (reagent/render-component [view/player state view-event-handler] dom-node)
    nil)) ; TODO: return JS object with control functions (play/pause) here

(defn create-player
