Add storage namespacing

Allows separate global properties from user specific properties
This commit is contained in:
Andrey Antukh 2024-09-10 16:32:00 +02:00 committed by Alonso Torres
parent 042b3a71d8
commit c8caca77a3
14 changed files with 144 additions and 85 deletions

View file

@ -11,7 +11,7 @@
[app.common.logging :as log]
[app.config :as cfg]
[app.util.globals :as globals]
[app.util.storage :refer [storage]]
[app.util.storage :as storage]
[cuerdas.core :as str]
[goog.object :as gobj]
[okulary.core :as l]
@ -76,7 +76,7 @@
cfg/default-language))))
(defonce translations #js {})
(defonce locale (l/atom (or (get @storage ::locale)
(defonce locale (l/atom (or (get storage/global ::locale)
(autodetect))))
(defn init!
@ -93,7 +93,7 @@
(if (or (nil? lname)
(str/empty? lname))
(let [lname (autodetect)]
(swap! storage dissoc ::locale)
(swap! storage/global dissoc ::locale)
(reset! locale lname))
(let [supported (into #{} (map :value supported-locales))
lname (loop [locales (seq (parse-locale lname))]
@ -102,7 +102,7 @@
locale
(recur (rest locales)))
cfg/default-language))]
(swap! storage assoc ::locale lname)
(swap! storage/global assoc ::locale lname)
(reset! locale lname))))
(deftype C [val]

View file

@ -10,7 +10,8 @@
[app.common.transit :as t]
[app.util.functions :as fns]
[app.util.globals :as g]
[cuerdas.core :as str]))
[cuerdas.core :as str]
[okulary.util :as ou]))
;; Using ex/ignoring because can receive a DOMException like this when
;; importing the code as a library: Failed to read the 'localStorage'
@ -19,26 +20,27 @@
(ex/ignoring (unchecked-get g/global "localStorage")))
(defn- encode-key
[k]
[prefix k]
(assert (keyword? k) "key must be keyword")
(let [kns (namespace k)
kn (name k)]
(str "penpot:" kns "/" kn)))
(str prefix ":" kns "/" kn)))
(defn- decode-key
[k]
(when (str/starts-with? k "penpot:")
(let [k (subs k 7)]
[prefix k]
(when (str/starts-with? k prefix)
(let [l (+ (count prefix) 1)
k (subs k l)]
(if (str/starts-with? k "/")
(keyword (subs k 1))
(let [[kns kn] (str/split k "/" 2)]
(keyword kns kn))))))
(defn- lookup-by-index
[result index]
[prefix result index]
(try
(let [key (.key ^js local-storage index)
key' (decode-key key)]
key' (decode-key prefix key)]
(if key'
(let [val (.getItem ^js local-storage key)]
(assoc! result key' (t/decode-str val)))
@ -46,39 +48,95 @@
(catch :default _
result)))
(defn- load
[]
(when (some? local-storage)
(defn- load-data
[prefix]
(if (some? local-storage)
(let [length (.-length ^js local-storage)]
(loop [index 0
result (transient {})]
(if (< index length)
(recur (inc index)
(lookup-by-index result index))
(persistent! result))))))
(lookup-by-index prefix result index))
(persistent! result))))
{}))
(defonce ^:private latest-state (load))
(defn create-storage
[prefix]
(let [initial (load-data prefix)
curr-data #js {:content initial}
last-data #js {:content initial}
watches (js/Map.)
(defn- on-change*
[curr-state]
(let [prev-state latest-state]
(try
(run! (fn [key]
(let [prev-val (get prev-state key)
curr-val (get curr-state key)]
(when-not (identical? curr-val prev-val)
(if (some? curr-val)
(.setItem ^js local-storage (encode-key key) (t/encode-str curr-val))
(.removeItem ^js local-storage (encode-key key))))))
(into #{} (concat (keys curr-state)
(keys prev-state))))
(finally
(set! latest-state curr-state)))))
on-change*
(fn [curr-state]
(let [prev-state (unchecked-get last-data "content")]
(try
(run! (fn [key]
(let [prev-val (get prev-state key)
curr-val (get curr-state key)]
(when-not (identical? curr-val prev-val)
(if (some? curr-val)
(.setItem ^js local-storage (encode-key prefix key) (t/encode-str curr-val))
(.removeItem ^js local-storage (encode-key prefix key))))))
(into #{} (concat (keys curr-state)
(keys prev-state))))
(finally
(unchecked-set last-data "content" curr-state)))))
(defonce on-change
(fns/debounce on-change* 2000))
on-change
(fns/debounce on-change* 2000)]
(defonce storage (atom latest-state))
(add-watch storage :persistence
(fn [_ _ _ curr-state]
(on-change curr-state)))
(reify
IAtom
IDeref
(-deref [_] (unchecked-get curr-data "content"))
ILookup
(-lookup [coll k]
(-lookup coll k nil))
(-lookup [_ k not-found]
(let [state (unchecked-get curr-data "content")]
(-lookup state k not-found)))
IReset
(-reset! [self newval]
(let [oldval (unchecked-get curr-data "content")]
(unchecked-set curr-data "content" newval)
(on-change newval)
(when (> (.-size watches) 0)
(-notify-watches self oldval newval))
newval))
ISwap
(-swap! [self f]
(let [state (unchecked-get curr-data "content")]
(-reset! self (f state))))
(-swap! [self f x]
(let [state (unchecked-get curr-data "content")]
(-reset! self (f state x))))
(-swap! [self f x y]
(let [state (unchecked-get curr-data "content")]
(-reset! self (f state x y))))
(-swap! [self f x y more]
(let [state (unchecked-get curr-data "content")]
(-reset! self (apply f state x y more))))
IWatchable
(-notify-watches [self oldval newval]
(ou/doiter
(.entries watches)
(fn [n]
(let [f (aget n 1)
k (aget n 0)]
(f k self oldval newval)))))
(-add-watch [self key f]
(.set watches key f)
self)
(-remove-watch [_ key]
(.delete watches key)))))
(defonce global (create-storage "penpot-global"))
(defonce user (create-storage "penpot-user"))

View file

@ -10,11 +10,11 @@
(:require
[app.config :as cfg]
[app.util.dom :as dom]
[app.util.storage :refer [storage]]
[app.util.storage :as storage]
[beicon.v2.core :as rx]
[rumext.v2 :as mf]))
(defonce theme (get @storage ::theme cfg/default-theme))
(defonce theme (get storage/global ::theme cfg/default-theme))
(defonce theme-sub (rx/subject))
(defonce themes #js {})
@ -27,7 +27,7 @@
(when (not= theme v)
(when-some [el (dom/get-element "theme")]
(set! (.-href el) (str "css/main-" v ".css")))
(swap! storage assoc ::theme v)
(swap! storage/global assoc ::theme v)
(set! theme v)
(rx/push! theme-sub v)))