-
Notifications
You must be signed in to change notification settings - Fork 10
/
cache.cljs
55 lines (45 loc) · 1.87 KB
/
cache.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(ns stylefy.impl.cache
(:require [dommy.core :as dommy]
[reagent.core :as r]
[garden.core :refer [css]]
[stylefy.impl.utils :as utils]
[garden.stylesheet :refer [at-media at-keyframes at-font-face]])
(:require-macros [reagent.ratom :refer [run!]]))
(def cache-prefix "stylefy_cache_")
(def cache-key-styles (str cache-prefix "styles"))
(def cache-key-created (str cache-prefix "created"))
(def cache-styles? (atom false))
(defn now-in-seconds []
(.floor js/Math (/ (.now js/Date) 1000)))
(defn read-cache-value
"Reads the cache if caching is used."
[key]
(when @cache-styles?
(when-let [cache-contents (.getItem (.-localStorage js/window) key)]
(cljs.reader/read-string cache-contents))))
(defn cache-expired? [cache-created expiration-age now]
(if (and cache-created expiration-age)
(< (+ cache-created expiration-age) now)
false))
(defn set-cache-created-time [time-created]
(.setItem (.-localStorage js/window) cache-key-created time-created))
(defn clear-styles []
(.setItem (.-localStorage js/window) cache-key-styles ""))
(defn use-caching! [cache-options]
(reset! cache-styles? true)
;; Cache is empty, set creation date.
(when-not (read-cache-value cache-key-created)
(.info js/console "Setting cache creation date.")
(set-cache-created-time (now-in-seconds)))
(when (cache-expired? (read-cache-value cache-key-created)
(:expires cache-options)
(now-in-seconds))
(clear-styles)
(.info js/console "stylefy cache expired. The cache has been cleared.")
(set-cache-created-time (now-in-seconds))))
(defn cache-styles
"Caches the given style if caching is used.
Throws QUOTA_EXCEEDED_ERR if the storage is full."
[styles]
(when (and @cache-styles? (map? styles))
(.setItem (.-localStorage js/window) cache-key-styles styles)))