   (when debug
     (swap! -running + (- (count new) (count old))))
   new)
+
+(defn- add-w [this key f]
+  (let [w (.-watches this)]
+    (set! (.-watches this) (check-watches w (assoc w key f)))))
+
+(defn- remove-w [this key]
+  (let [w (.-watches this)]
+    (set! (.-watches this) (check-watches w (dissoc w key)))))
+
+(defn- pr-atom [a writer opts s]
+  (-write writer (str "#<" s " "))
+  (pr-writer (binding [*ratom-context* nil] (-deref a)) writer opts)
+  (-write writer ">"))
 
 
 ;;; Atom

      new-value))

  ISwap
  (-swap! [a f]
    (-reset! a (f state)))
  (-swap! [a f x]
    (-reset! a (f state x)))
  (-swap! [a f x y]
    (-reset! a (f state x y)))
  (-swap! [a f x y more]
    (-reset! a (apply f state x y more)))

  IMeta
  (-meta [_] meta)

  IPrintWithWriter
  (-pr-writer [a writer opts]
    (-write writer "#<Atom: ")
    (pr-writer state writer opts)
    (-write writer ">"))

  IWatchable
  (-notify-watches [this oldval newval]
    (reduce-kv (fn [_ key f]
                 (f key this oldval newval)
                 nil)
               nil watches))
  (-add-watch [this key f]
    (set! watches (check-watches watches (assoc watches key f))))
  (-remove-watch [this key]
    (set! watches (check-watches watches (dissoc watches key))))

  IHash
  (-hash [this] (goog/getUid this)))

(defn- cached-reaction [f key obj destroy]
  (if-some [r (get cached-reactions key)]
    (-deref r)
    (if-not (nil? *ratom-context*)
      (let [r (make-reaction
               f :on-dispose (fn [x]
                               (set! cached-reactions
                                     (dissoc cached-reactions key))
                               (when-not (nil? obj)
                                 (set! (.-reaction obj) nil))
                               (when-not (nil? destroy)
                                 (destroy x))
                               nil))
            v (-deref r)]
        (set! cached-reactions (assoc cached-reactions key r))
        (when-not (nil? obj)
          (set! (.-reaction obj) r))
        v)
      (f))))
