Permalink
Browse files

make Refs bindable

* adds Refs to the Bindable protocol
* adds basic testing for binding from Atom to Ref and vice versa
* does not deal with ensembles of Refs in any way
  • Loading branch information...
1 parent 5d62142 commit 3bae3be0daf2d568be92e3ae4612c0c253ed8119 @ska2342 ska2342 committed with Feb 21, 2014
Showing with 27 additions and 1 deletion.
  1. +9 −0 src/seesaw/bind.clj
  2. +18 −1 test/seesaw/test/bind.clj
View
@@ -148,6 +148,15 @@
(fn [] (remove-watch this key))))
(notify [this v] (throw (IllegalStateException. "Can't notify an agent!")))
+ clojure.lang.Ref
+ (subscribe [this handler]
+ (let [key (keyword (gensym "bindable-ref-watcher"))]
+ (add-watch this key
+ (fn bindable-ref-watcher
+ [k r o n] (when-not (= o n) (handler n))))
+ (fn [] (remove-watch this key))))
+ (notify [this v] (dosync (ref-set this v)))
+
javax.swing.text.Document
(subscribe [this handler]
(ssc/listen this :document
View
@@ -172,7 +172,24 @@
true)
; Unfortunately, in Clojure 1.2, IllegalStateException gets wrapped by reset!
(catch RuntimeException e
- (= IllegalStateException (class (.getCause e))))))))))
+ (= IllegalStateException (class (.getCause e)))))))))
+
+ (testing "given a Ref"
+ (it "should pass along changes to the ref's value"
+ (let [start (ref nil)
+ end (atom nil)]
+ (bind start end)
+ (dosync
+ (alter start (constantly "foo")))
+ (expect (= "foo" @start))
+ (expect (= "foo" @end))))
+ (it "should pass update the ref's value when the source changes"
+ (let [start (atom nil)
+ end (ref nil)]
+ (bind start end)
+ (reset! start "foo")
+ (expect (= "foo" @start))
+ (expect (= "foo" @end))))) )
(describe b-do*
(it "executes a function with a single argument and ends a chain"

0 comments on commit 3bae3be

Please sign in to comment.