diff --git a/project.clj b/project.clj index 51169f171..f8d5b1abe 100644 --- a/project.clj +++ b/project.clj @@ -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"] diff --git a/src/uxbox/data/auth.cljs b/src/uxbox/data/auth.cljs index 47079e3cc..e484b2b73 100644 --- a/src/uxbox/data/auth.cljs +++ b/src/uxbox/data/auth.cljs @@ -6,8 +6,7 @@ ;; Copyright (c) 2015-2016 Juan de la Cruz (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 [] diff --git a/src/uxbox/locales.cljs b/src/uxbox/locales.cljs index 630b624ae..201ea4a8e 100644 --- a/src/uxbox/locales.cljs +++ b/src/uxbox/locales.cljs @@ -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. diff --git a/src/uxbox/repo/core.cljs b/src/uxbox/repo/core.cljs index 85a212376..30e55c956 100644 --- a/src/uxbox/repo/core.cljs +++ b/src/uxbox/repo/core.cljs @@ -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") diff --git a/src/uxbox/state.cljs b/src/uxbox/state.cljs index 69c47d11a..048a053f9 100644 --- a/src/uxbox/state.cljs +++ b/src/uxbox/state.cljs @@ -6,10 +6,10 @@ ;; Copyright (c) 2015-2016 Juan de la Cruz (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 diff --git a/src/uxbox/util/storage.cljs b/src/uxbox/util/storage.cljs new file mode 100644 index 000000000..17b9468d3 --- /dev/null +++ b/src/uxbox/util/storage.cljs @@ -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 + +(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"))