   "Constructs a new runnable looping function which puts an event on the given
   output channel every period milliseconds. The loop can be terminated by
   interrupting the thread."
+  ^Runnable
   [period channel]
   (fn []
     (try



(defrecord TimerInput
  [period out thread])

(extend-type TimerInput
  component/Lifecycle

  (start
    [this]
    (println (str "Starting " this "..."))
    (when-not (:out this)
      (throw (IllegalStateException. "Cannot start TimerInput without output channel")))
    ; TODO: create sliding-buffer and mix into output?
    (if (:thread this)
      (do
        (println (str this " is already running"))
        this)
      (let [run (timer-loop (:period this) (:out this))]
        (assoc this :thread
          (doto (Thread. ^Runnable run "TimerInput")
            (.setDaemon true)
            (.start))))))


  (stop
    [this]
    (println (str "Stopping " this "..."))
    (when-let [^Thread thread (:thread this)]
      (.interrupt thread)
      (.join thread 1000))
    (assoc this :thread nil)))

(defn timer
  "Creates a new timer that will put an event on the output channel every period
  milliseconds."
  [period]
  {:pre [(pos? period)]}
  (TimerInput. period nil nil))
