Created
January 28, 2010 21:16
-
-
Save jcromartie/289151 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- save-agent [_ path data] | |
(atomic-spit path data) | |
(println (.getTime (Date.)) "wrote" path)) | |
(defn saved-ref [path default] | |
"Returns a ref backed by the file at path, with the default value if path could not be loaded" | |
(let [value (read-string (clojure.contrib.duck-streams/slurp* path)) | |
r (ref (or value default)) | |
a (agent nil)] | |
(add-watch r a (fn [_ _ _ new-val] (send-off a save-agent path new-val))) | |
r)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment