Skip to content

Instantly share code, notes, and snippets.

Last active Jan 24, 2019
What would you like to do?
(: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."
(read-string (slurp client-data-file))
(catch Exception e
(println "[WARNING] Unable to read client data: starting empty.")
(defn- listen-client-data
"Intercept client-data atom and update it to file."
(let [channel (a/chan 10)]
(a/go-loop []
(when-some [data (a/<! channel)]
(spit client-data-file data)
(catch Throwable t
(println "Unable to write the file" t)))
(defonce client-data-update-chan (listen-client-data client-data-file))
(defonce client-data
(atom (load-client-data client-data-file))
#(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