mirror of
https://github.com/penpot/penpot.git
synced 2025-03-12 07:41:43 -05:00
♻️ Refactor storage namespace (frontend).
This commit is contained in:
parent
8835216ca9
commit
4018e4df79
1 changed files with 25 additions and 56 deletions
|
@ -19,65 +19,34 @@
|
||||||
[v]
|
[v]
|
||||||
(ex/ignoring (t/decode v)))
|
(ex/ignoring (t/decode v)))
|
||||||
|
|
||||||
(def local
|
|
||||||
{:get #(decode (.getItem ^js js/localStorage (name %)))
|
|
||||||
:set #(.setItem ^js js/localStorage (name %1) (t/encode %2))})
|
|
||||||
|
|
||||||
(def session
|
|
||||||
{:get #(decode (.getItem ^js js/sessionStorage (name %)))
|
|
||||||
:set #(.setItem ^js js/sessionStorage (name %1) (t/encode %2))})
|
|
||||||
|
|
||||||
(defn- persist
|
(defn- persist
|
||||||
[alias storage value]
|
[storage prev curr]
|
||||||
(when-not (is-worker?)
|
(run! (fn [key]
|
||||||
(tm/schedule-on-idle
|
(let [prev* (get prev key)
|
||||||
(fn [] ((:set storage) alias value)))))
|
curr* (get curr key)]
|
||||||
|
(when (not= curr* prev*)
|
||||||
|
(tm/schedule-on-idle
|
||||||
|
#(if (some? curr*)
|
||||||
|
(.setItem ^js storage (t/encode key) (t/encode curr*))
|
||||||
|
(.removeItem ^js storage (t/encode key)))))))
|
||||||
|
|
||||||
|
(into #{} (concat (keys curr)
|
||||||
|
(keys prev)))))
|
||||||
|
|
||||||
(defn- load
|
(defn- load
|
||||||
[alias storage]
|
[storage]
|
||||||
(when-not (is-worker?)
|
(let [len (.-length ^js storage)]
|
||||||
((:get storage) alias)))
|
(reduce (fn [res index]
|
||||||
|
(let [key (.key ^js storage index)
|
||||||
(defn- make-storage
|
val (.getItem ^js storage key)]
|
||||||
[alias storage]
|
(try
|
||||||
(let [data (atom (load alias storage))]
|
(assoc res (t/decode key) (t/decode val))
|
||||||
(add-watch data :sub #(persist alias storage %4))
|
(catch :default e
|
||||||
(reify
|
res))))
|
||||||
Object
|
{}
|
||||||
(toString [_]
|
(range len))))
|
||||||
(str "Storage" (pr-str @data)))
|
|
||||||
|
|
||||||
ICounted
|
|
||||||
(-count [_]
|
|
||||||
(count @data))
|
|
||||||
|
|
||||||
ISeqable
|
|
||||||
(-seq [_]
|
|
||||||
(seq @data))
|
|
||||||
|
|
||||||
IReset
|
|
||||||
(-reset! [self newval]
|
|
||||||
(reset! data newval))
|
|
||||||
|
|
||||||
ISwap
|
|
||||||
(-swap! [self f]
|
|
||||||
(swap! data f))
|
|
||||||
(-swap! [self f x]
|
|
||||||
(swap! data f x))
|
|
||||||
(-swap! [self f x y]
|
|
||||||
(swap! data f x y))
|
|
||||||
(-swap! [self f x y more]
|
|
||||||
(apply swap! data f x y more))
|
|
||||||
|
|
||||||
ILookup
|
|
||||||
(-lookup [_ key]
|
|
||||||
(get @data key nil))
|
|
||||||
(-lookup [_ key not-found]
|
|
||||||
(get @data key not-found)))))
|
|
||||||
|
|
||||||
|
|
||||||
(defonce storage
|
(defonce storage (atom (load js/localStorage)))
|
||||||
(make-storage "app" local))
|
(add-watch storage :persistence #(persist js/localStorage %3 %4))
|
||||||
|
|
||||||
(defonce cache
|
|
||||||
(make-storage "cache" session))
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue