Add local-store get default value, save ls keys with a safe prefix

This commit is contained in:
Daniel Compton 2017-08-25 11:00:36 +12:00
parent 85d274a528
commit 465712c1e4
2 changed files with 18 additions and 11 deletions

View File

@ -188,8 +188,7 @@
(defn render-trace-panel [] (defn render-trace-panel []
(let [filter-input (r/atom "") (let [filter-input (r/atom "")
filter-items (r/atom (or (localstorage/get "filter-items") filter-items (r/atom (localstorage/get "filter-items" []))
[]))
filter-type (r/atom :contains) filter-type (r/atom :contains)
input-error (r/atom false) input-error (r/atom false)
trace-detail-expansions (r/atom {:show-all? false :overrides {}})] trace-detail-expansions (r/atom {:show-all? false :overrides {}})]

View File

@ -4,15 +4,23 @@
[cljs.reader :as reader]) [cljs.reader :as reader])
(:refer-clojure :exclude [get])) (:refer-clojure :exclude [get]))
(def mech (goog.storage.mechanism.HTML5LocalStorage.)) (def storage (goog.storage.Storage. (goog.storage.mechanism.HTML5LocalStorage.)))
(defn- storage [] (defn- safe-key [key]
(goog.storage.Storage. mech)) "Adds a unique prefix to keys to ensure they don't collide with the host application"
(str "day8.re-frame.trace." key))
(defn get [key] (defn get
(when-let [value (.get (storage) key)] "Gets a re-frame-trace value from local storage."
(cljs.reader/read-string value))) ([key]
(get key nil))
([key not-found]
(let [value (.get storage (safe-key key))]
(if (undefined? value)
not-found
(reader/read-string value)))))
(defn save! [key value] (defn save!
(let [store (storage)] "Saves a re-frame-trace value to local storage."
(.set store key (pr-str value)))) [key value]
(.set storage (safe-key key) (pr-str value)))