0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-21 22:36:12 -05:00
penpot/frontend/src/uxbox/util/storage.cljs

68 lines
1.6 KiB
Text
Raw Normal View History

;; 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]))
(defn- persist
[alias value]
(when-not (or (= *target* "nodejs")
(not (exists? js/window)))
(let [key (name alias)
value (t/encode value)]
(.setItem js/localStorage key value))))
(defn- load
[alias]
(when-not (or (= *target* "nodejs")
(not (exists? js/window)))
2016-04-15 23:39:04 +03:00
(let [data (.getItem js/localStorage (name alias))]
(try
(t/decode data)
(catch :default e
(js/console.error "Error on loading data from local storage." e)
nil)))))
(defn- make-storage
[alias]
(let [data (atom (load alias))]
(add-watch data :sub #(persist alias %4))
(reify
Object
(toString [_]
(str "Storage" (pr-str @data)))
ICounted
(-count [_]
(count @data))
ISeqable
(-seq [_]
(seq @data))
IReset
(-reset! [self newval]
(reset! data newval))
ISwap
(-swap! [self f]
2016-04-15 23:39:04 +03:00
(swap! data f))
(-swap! [self f x]
2016-04-15 23:39:04 +03:00
(swap! data f x))
(-swap! [self f x y]
2016-04-15 23:39:04 +03:00
(swap! data f x y))
(-swap! [self f x y more]
2016-04-15 23:39:04 +03:00
(apply swap! data f x y more))
ILookup
(-lookup [_ key]
(get @data key nil))
(-lookup [_ key not-found]
(get @data key not-found)))))
(def storage
(make-storage "uxbox"))