Skip to content

Commit

Permalink
CLJS-760: break apart IAtom protocol into IReset & ISwap
Browse files Browse the repository at this point in the history
  • Loading branch information
swannodette committed Feb 17, 2014
1 parent 6e5f063 commit 3e6564a
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/cljs/cljs/core.cljs
Expand Up @@ -7061,9 +7061,12 @@ reduces them without incurring seq initialization"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Reference Types ;;;;;;;;;;;;;;;;

(defprotocol IAtom
(defprotocol IReset
(-reset! [o new-value]))

(defprotocol ISwap
(-swap! [o f] [o f a] [o f a b] [o f a b xs]))

(deftype Atom [state meta validator watches]
IEquiv
(-equiv [o other] (identical? o other))
Expand Down Expand Up @@ -7124,21 +7127,33 @@ reduces them without incurring seq initialization"
new-value))
(-reset! a new-value)))

;; generic to all refs
;; (but currently hard-coded to atom!)
(defn deref
[o]
(-deref o))

(defn swap!
"Atomically swaps the value of atom to be:
(apply f current-value-of-atom args). Note that f may be called
multiple times, and thus should be free of side effects. Returns
the value that was swapped in."
([a f]
(reset! a (f (.-state a))))
(if (instance? Atom a)
(reset! a (f (.-state a)))
(-swap! a (deref a))))
([a f x]
(reset! a (f (.-state a) x)))
(if (instance? Atom a)
(reset! a (f (.-state a) x))
(-swap! a (f (deref a) x))))
([a f x y]
(reset! a (f (.-state a) x y)))
([a f x y z]
(reset! a (f (.-state a) x y z)))
([a f x y z & more]
(reset! a (apply f (.-state a) x y z more))))
(if (instance? Atom a)
(reset! a (f (.-state a) x y))
(-swap! a (f (deref a) x y))))
([a f x y & more]
(if (instance? Atom a)
(reset! a (apply f (.-state a) x y more))
(-swap! a (f (deref a) x y more)))))

(defn compare-and-set!
"Atomically sets the value of atom to newval if and only if the
Expand All @@ -7149,13 +7164,6 @@ reduces them without incurring seq initialization"
(do (reset! a newval) true)
false))

;; generic to all refs
;; (but currently hard-coded to atom!)

(defn deref
[o]
(-deref o))

(defn set-validator!
"Sets the validator-fn for an atom. validator-fn must be nil or a
side-effect-free fn of one argument, which will be passed the intended
Expand Down

0 comments on commit 3e6564a

Please sign in to comment.