generated from PEZ/rn-rf-shadow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_storage.cljs
107 lines (92 loc) · 3.25 KB
/
async_storage.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(ns react-native.async-storage
(:require [cljs-bean.core :refer [->clj]]
["@react-native-async-storage/async-storage" :default async-storage]
[cognitect.transit :as transit]
goog.functions
[taoensso.timbre :as log]
[clojure.string :as str]))
;; NOTE: Taken from
;; https://github.com/status-im/status-mobile/blob/33e637ff715a2ca5ef4527ec392861fff32fc672/src/react_native/async_storage.cljs#L4
(def ^:private debounce-ms 250)
(def ^:private reader (transit/reader :json))
(def ^:private writer (transit/writer :json))
(defn clj->transit
[o]
(transit/write writer o))
(defn transit->clj
[o]
(try
(transit/read reader o)
(catch :default e
(log/error e))))
(defn set-item!
([k value] (set-item! k value identity))
([k value cb]
;; (log/info "[set-item!]" (str k) (clj->transit value))
(-> ^js async-storage
(.setItem (str k)
(clj->transit value))
(.then (fn [_] (cb)))
(.catch (fn [error]
(log/error "[async-storage]" error))))))
(defn set-item-factory
[]
(let [tmp-storage (atom {})
debounced (goog.functions/debounce (fn []
(doseq [[k v] @tmp-storage]
(swap! tmp-storage dissoc k)
(set-item! k v)))
debounce-ms)]
(fn [items]
(swap! tmp-storage merge items)
(debounced))))
(defn- inflate-map
"Transforms {:attributes/str ...} => {:attributes {:str ...}}"
[m]
(reduce-kv
(fn [acc k v]
(assoc-in acc (->> (str/split (str (symbol k)) "/") (map keyword) vec) v))
{}
m))
(defn get-items
[ks cb]
(-> ^js async-storage
(.multiGet (to-array (map str ks)))
(.then (fn [^js data]
(let [ds (filter second (->clj data))
res (->> ds ;; (->clj data)
(map (comp transit->clj second))
(zipmap (map #(keyword (str/replace-first (first %) ":" "")) ds)) ;; (zipmap ks)
inflate-map)]
;; (log/info "[async-storage] post-process" res)
(cb res))))
(.catch (fn [error]
(cb nil)
(log/error "[async-storage]" error)))))
(defn get-item
[k cb]
(-> ^js async-storage
(.getItem (str k))
(.then (fn [^js data]
(-> data
js->clj
transit->clj
cb)))
(.catch (fn [error]
(cb nil)
(log/error "[async-storage]" error)))))
(comment (-> ^js async-storage
(.getAllKeys)
(.then #(js/console.log "get-all-keys" %)))
(-> ^js async-storage
(.clear)
(.then #(js/console.log "clear" %)))
(-> ^js async-storage
(.multiGet #js [":spell-costs"])
(.then #(js/console.log "multi-get" %)))
(-> ^js async-storage
(.removeItem ":spell-costs")
(.then #(js/console.log "remove-item" %)))
(-> ^js async-storage
(.getItem ":languages/0")
(.then #(js/console.log "get-item" %))))