                             :done []}))
 
 (defn- process-handler
-  "Returns a new deps map containing either processed handler or it placed in
-  the todo list"
+  "Returns a new deps map containing either processed handler or it
+  placed in the todo list"
   [dep-state key deps task]
   (apply assoc dep-state
          (if (set/superset? (:satisfied dep-state) deps)


(defn wait-until-deps-satisfied
  "Makes the current thread sleep until specified deps have been satisfied.
  Thread enters a sleep cycle sleeping for wait-time seconds before each dep check.
  If timeout is a positive value throws timeout exception if deps haven't been
  satisfied by timeout secs. The default wait-time is 0.1 seconds, and the
  default timeout is 10 seconds."
  ([deps] (wait-until-deps-satisfied deps 10 0.1))
  ([deps timeout] (wait-until-deps-satisfied deps timeout 0.1))
  ([deps timeout wait-time]
   (let [timeout (* 1000 timeout)
         wait-time (* 1000 wait-time)]
     (if (<= timeout 0)
       (while (not (deps-satisfied? deps))
         (Thread/sleep wait-time))
       (loop [sleep-time 0]
         (when (> sleep-time timeout)
           (throw (Exception. (str "The following deps took too long (" timeout
                                   " seconds) to be satisfied: " deps))))
         (when-not (deps-satisfied? deps)
           (Thread/sleep wait-time)
           (recur (+ sleep-time wait-time))))))))

                        [key deps task])])))

(defn- replace-handler
  "Replace all occurances of handers with the given key with the new
  handler and deps set"
  [dep-state key deps task]
  (let [replacer-fn #(if (= key (first %))
                       [key deps task]
                       %)]
    {:satisfied (dep-state :satisfied)
     :todo      (map replacer-fn (dep-state :todo))
     :done      (map replacer-fn (dep-state :done))}))

(defn- key-known?
  "Returns true or false depending on whether this key is associated
  with a handler in either the completed or todo lists."
  [dep-state key]
  (some #(= key (first %)) (concat (:done dep-state) (:todo dep-state))))

(defn- on-deps*
  "If a handler with this key has already been registered, just replace
  the handler - either in todo or completed. If the key is unknown, then
  either execute the handler if the deps are satisfied or add it to the
  todo list"
  [dep-state key deps task]
  (if (key-known? dep-state key)
    (replace-handler dep-state key deps task)
