Skip to content

Instantly share code, notes, and snippets.

@ekaitz-zarraga
Last active January 24, 2019 17:00
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 ekaitz-zarraga/3831126dadbfffec31c59709e6f6bcca to your computer and use it in GitHub Desktop.
Save ekaitz-zarraga/3831126dadbfffec31c59709e6f6bcca to your computer and use it in GitHub Desktop.
(ns asktodon.storage
(:require [clojure.core.async :as a]))
(defonce client-data-file "client-data.edn")
(defn- load-client-data
"Load client data file, supposed to be done only once."
[client-data-file]
(try
(read-string (slurp client-data-file))
(catch Exception e
(do
(println "[WARNING] Unable to read client data: starting empty.")
{}))))
(defn- listen-client-data
"Intercept client-data atom and update it to file."
[client-data-file]
(let [channel (a/chan 10)]
(a/go-loop []
(when-some [data (a/<! channel)]
(try
(spit client-data-file data)
(catch Throwable t
(println "Unable to write the file" t)))
(recur)))
channel))
(defonce client-data-update-chan (listen-client-data client-data-file))
(defonce client-data
(add-watch
(atom (load-client-data client-data-file))
identity
#(a/put! client-data-update-chan %4)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment