Last active
January 24, 2019 17:00
-
-
Save ekaitz-zarraga/3831126dadbfffec31c59709e6f6bcca 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
(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