Created
July 15, 2011 15:02
-
-
Save jcromartie/1084860 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn watch | |
"Returns (running) thread which observes the value of f every delay | |
milliseconds, and calls the callback fn if the value changes. callback | |
will be called with the old and new values from f" | |
([f callback delay] | |
(let [proc (fn [] | |
(loop [x1 ::watch-init] | |
(let [x2 (f)] | |
(when (not= x1 x2) | |
(callback x1 x2)) | |
(Thread/sleep delay) | |
(recur x2)))) | |
thread (Thread. proc)] | |
(.start thread) | |
thread)) | |
([f callback] | |
(watch f callback 5000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment