Skip to content

Instantly share code, notes, and snippets.

@ekaitz-zarraga
Last active Jan 24, 2019
Embed
What would you like to do?
(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