mirror of
https://github.com/penpot/penpot.git
synced 2025-05-11 00:56:38 +02:00
Add new transit based and nodejs friendly storage abstraction.
This commit is contained in:
parent
95c4f2fbc1
commit
dba3c8764a
6 changed files with 77 additions and 18 deletions
|
@ -26,7 +26,6 @@
|
|||
[funcool/promesa "1.1.1"]
|
||||
[funcool/beicon "1.2.0"]
|
||||
[funcool/cuerdas "0.7.2"]
|
||||
[funcool/hodgepodge "0.1.4"]
|
||||
[bidi "2.0.6"]]
|
||||
:plugins [[lein-ancient "0.6.7"]]
|
||||
:clean-targets ^{:protect false} ["resources/public/js" "target"]
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||
|
||||
(ns uxbox.data.auth
|
||||
(:require [hodgepodge.core :refer [local-storage]]
|
||||
[beicon.core :as rx]
|
||||
(:require [beicon.core :as rx]
|
||||
[promesa.core :as p]
|
||||
[uxbox.repo :as rp]
|
||||
[uxbox.rstore :as rs]
|
||||
|
@ -17,7 +16,8 @@
|
|||
[uxbox.locales :refer (tr)]
|
||||
[uxbox.data.projects :as udp]
|
||||
[uxbox.data.users :as udu]
|
||||
[uxbox.data.messages :as udm]))
|
||||
[uxbox.data.messages :as udm]
|
||||
[uxbox.util.storage :refer (storage)]))
|
||||
|
||||
;; --- Logged In
|
||||
|
||||
|
@ -32,7 +32,7 @@
|
|||
|
||||
rs/EffectEvent
|
||||
(-apply-effect [this state]
|
||||
(assoc! local-storage :uxbox/auth data)))
|
||||
(swap! storage assoc :uxbox/auth data)))
|
||||
|
||||
(defn logged-in?
|
||||
[v]
|
||||
|
@ -82,7 +82,7 @@
|
|||
|
||||
rs/EffectEvent
|
||||
(-apply-effect [this state]
|
||||
(dissoc! local-storage :uxbox/auth)))
|
||||
(swap! storage dissoc :uxbox/auth)))
|
||||
|
||||
(defn logout
|
||||
[]
|
||||
|
|
|
@ -7,15 +7,15 @@
|
|||
|
||||
(ns uxbox.locales
|
||||
"A i18n foundation."
|
||||
(:require [hodgepodge.core :refer [local-storage]]
|
||||
[cuerdas.core :as str]
|
||||
[uxbox.locales.en :as locales-en]))
|
||||
(:require [cuerdas.core :as str]
|
||||
[uxbox.locales.en :as locales-en]
|
||||
[uxbox.util.storage :refer (storage)]))
|
||||
|
||||
(defonce +locales+
|
||||
{:en locales-en/+locales+})
|
||||
|
||||
(defonce +locale+
|
||||
(get local-storage ::locale :en))
|
||||
(get storage ::locale :en))
|
||||
|
||||
;; A marker type that is used just for mark
|
||||
;; a parameter that reprsentes the counter.
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
(:require [clojure.walk :as walk]
|
||||
[httpurr.client.xhr :as http]
|
||||
[httpurr.status :as http.status]
|
||||
[hodgepodge.core :refer (local-storage)]
|
||||
[promesa.core :as p :include-macros true]
|
||||
[beicon.core :as rx]
|
||||
[uxbox.util.transit :as t]
|
||||
|
@ -19,9 +18,6 @@
|
|||
|
||||
(goog-define url "http://127.0.0.1:5050/api")
|
||||
|
||||
(def ^:private +storage+
|
||||
local-storage)
|
||||
|
||||
(defn- conditional-decode
|
||||
[{:keys [body headers] :as response}]
|
||||
(if (= (get headers "content-type") "application/transit+json")
|
||||
|
|
|
@ -6,10 +6,10 @@
|
|||
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
|
||||
|
||||
(ns uxbox.state
|
||||
(:require [hodgepodge.core :refer [local-storage]]
|
||||
[beicon.core :as rx]
|
||||
(:require [beicon.core :as rx]
|
||||
[lentes.core :as l]
|
||||
[uxbox.rstore :as rs]))
|
||||
[uxbox.rstore :as rs]
|
||||
[uxbox.util.storage :refer (storage)]))
|
||||
|
||||
(defonce state (atom {}))
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
|||
{:dashboard {:project-order :name
|
||||
:project-filter ""}
|
||||
:route nil
|
||||
:auth (:uxbox/auth local-storage)
|
||||
:auth (:auth storage nil)
|
||||
:clipboard #queue []
|
||||
:profile nil
|
||||
:workspace nil
|
||||
|
|
64
src/uxbox/util/storage.cljs
Normal file
64
src/uxbox/util/storage.cljs
Normal file
|
@ -0,0 +1,64 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(ns uxbox.util.storage
|
||||
(:require [uxbox.util.transit :as t]))
|
||||
|
||||
(enable-console-print!)
|
||||
|
||||
(defn- persist
|
||||
[alias value]
|
||||
(let [key (name value)
|
||||
value (t/encode value)]
|
||||
(.setItem js/localStorage key value)))
|
||||
|
||||
(defn- load
|
||||
[alias]
|
||||
(println "load" *target*)
|
||||
(if (= *target* "nodejs")
|
||||
{}
|
||||
(when-let [data (.getItem js/localStorage (name alias))]
|
||||
(try
|
||||
(t/decode data)
|
||||
(catch js/Error e
|
||||
(js/console.log (.-stack e))
|
||||
{})))))
|
||||
|
||||
(defn make-storage
|
||||
[alias]
|
||||
(let [data (atom (load alias))]
|
||||
(when (not= *target* "nodejs")
|
||||
(add-watch data :sub #(persist alias %4)))
|
||||
(reify
|
||||
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]
|
||||
(-swap! data f x y more))
|
||||
|
||||
ILookup
|
||||
(-lookup [_ key]
|
||||
(-lookup @data key nil))
|
||||
(-lookup [_ key not-found]
|
||||
(-lookup @data key not-found)))))
|
||||
|
||||
(def storage (make-storage "uxbox"))
|
Loading…
Add table
Add a link
Reference in a new issue