Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created January 28, 2010 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcromartie/289151 to your computer and use it in GitHub Desktop.
Save jcromartie/289151 to your computer and use it in GitHub Desktop.
(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