Permalink
Browse files

Add IAtom protocol

  • Loading branch information...
1 parent dc4ba2e commit 33692b79a114faf4bedc6d9ab38d25ce6ea4b295 @jamii jamii committed with swannodette Feb 2, 2014
Showing with 13 additions and 8 deletions.
  1. +13 −8 src/cljs/cljs/core.cljs
View
@@ -7039,6 +7039,9 @@ reduces them without incurring seq initialization"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Reference Types ;;;;;;;;;;;;;;;;
+(defprotocol IAtom
+ (-reset! [o new-value]))
+
(deftype Atom [state meta validator watches]
IEquiv
(-equiv [o other] (identical? o other))
@@ -7088,14 +7091,16 @@ reduces them without incurring seq initialization"
"Sets the value of atom to newval without regard for the
current value. Returns newval."
[a new-value]
- (let [validate (.-validator a)]
- (when-not (nil? validate)
- (assert (validate new-value) "Validator rejected reference state")))
- (let [old-value (.-state a)]
- (set! (.-state a) new-value)
- (when-not (nil? (.-watches a))
- (-notify-watches a old-value new-value)))
- new-value)
+ (if (instance? Atom a)
+ (let [validate (.-validator a)]
+ (when-not (nil? validate)
+ (assert (validate new-value) "Validator rejected reference state"))
+ (let [old-value (.-state a)]
+ (set! (.-state a) new-value)
+ (when-not (nil? (.-watches a))
+ (-notify-watches a old-value new-value))
+ new-value))
+ (-reset! a new-value)))
(defn swap!
"Atomically swaps the value of atom to be:

0 comments on commit 33692b7

Please sign in to comment.