|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+/* @flow */
|
|
|
2
|
+
|
|
|
3
|
+import { AsyncStorage } from 'react-native';
|
|
|
4
|
+
|
|
|
5
|
+/**
|
|
|
6
|
+ * A Web Sorage API implementation used for polyfilling
|
|
|
7
|
+ * <tt>window.localStorage</tt> and/or <tt>window.sessionStorage</tt>.
|
|
|
8
|
+ * <p>
|
|
|
9
|
+ * The Web Storage API is synchronous whereas React Native's builtin generic
|
|
|
10
|
+ * storage API <tt>AsyncStorage</tt> is asynchronous so the implementation with
|
|
|
11
|
+ * persistence is optimistic: it will first store the value locally in memory so
|
|
|
12
|
+ * that results can be served synchronously and then persist the value
|
|
|
13
|
+ * asynchronously. If an asynchronous operation produces an error, it's ignored.
|
|
|
14
|
+ */
|
|
|
15
|
+export default class Storage {
|
|
|
16
|
+ _items: Map<string, string>;
|
|
|
17
|
+
|
|
|
18
|
+ _keyPrefix: ?string;
|
|
|
19
|
+
|
|
|
20
|
+ /**
|
|
|
21
|
+ * Initializes a new <tt>Storage</tt> instance. Loads all previously
|
|
|
22
|
+ * persisted data items from React Native's <tt>AsyncStorage</tt> if
|
|
|
23
|
+ * necessary.
|
|
|
24
|
+ *
|
|
|
25
|
+ * @param {string|undefined} keyPrefix - The prefix of the
|
|
|
26
|
+ * <tt>AsyncStorage</tt> keys to be persisted by this storage.
|
|
|
27
|
+ */
|
|
|
28
|
+ constructor(keyPrefix: ?string) {
|
|
|
29
|
+ /**
|
|
|
30
|
+ * The data items stored in this storage.
|
|
|
31
|
+ *
|
|
|
32
|
+ * @private
|
|
|
33
|
+ * @type {Map}
|
|
|
34
|
+ */
|
|
|
35
|
+ this._items = new Map();
|
|
|
36
|
+
|
|
|
37
|
+ /**
|
|
|
38
|
+ * The prefix of the <tt>AsyncStorage</tt> keys persisted by this
|
|
|
39
|
+ * storage. If <tt>undefined</tt>, then the data items stored in this
|
|
|
40
|
+ * storage will not be persisted.
|
|
|
41
|
+ *
|
|
|
42
|
+ * @private
|
|
|
43
|
+ * @type {string}
|
|
|
44
|
+ */
|
|
|
45
|
+ this._keyPrefix = keyPrefix;
|
|
|
46
|
+
|
|
|
47
|
+ if (typeof this._keyPrefix !== 'undefined') {
|
|
|
48
|
+ // Load all previously persisted data items from React Native's
|
|
|
49
|
+ // AsyncStorage.
|
|
|
50
|
+ AsyncStorage.getAllKeys().then((...getAllKeysCallbackArgs) => {
|
|
|
51
|
+ // XXX The keys argument of getAllKeys' callback may or may not
|
|
|
52
|
+ // be preceded by an error argument.
|
|
|
53
|
+ const keys
|
|
|
54
|
+ = getAllKeysCallbackArgs[getAllKeysCallbackArgs.length - 1]
|
|
|
55
|
+ .filter(key => key.startsWith(this._keyPrefix));
|
|
|
56
|
+
|
|
|
57
|
+ AsyncStorage.multiGet(keys).then((...multiGetCallbackArgs) => {
|
|
|
58
|
+ // XXX The result argument of multiGet may or may not be
|
|
|
59
|
+ // preceded by an errors argument.
|
|
|
60
|
+ const result
|
|
|
61
|
+ = multiGetCallbackArgs[multiGetCallbackArgs.length - 1];
|
|
|
62
|
+ const keyPrefixLength
|
|
|
63
|
+ = this._keyPrefix && this._keyPrefix.length;
|
|
|
64
|
+
|
|
|
65
|
+ // eslint-disable-next-line prefer-const
|
|
|
66
|
+ for (let [ key, value ] of result) {
|
|
|
67
|
+ key = key.substring(keyPrefixLength);
|
|
|
68
|
+
|
|
|
69
|
+ // XXX The loading of the previously persisted data
|
|
|
70
|
+ // items from AsyncStorage is asynchronous which means
|
|
|
71
|
+ // that it is technically possible to invoke setItem
|
|
|
72
|
+ // with a key before the key is loaded from
|
|
|
73
|
+ // AsyncStorage.
|
|
|
74
|
+ if (!this._items.has(key)) {
|
|
|
75
|
+ this._items.set(key, value);
|
|
|
76
|
+ }
|
|
|
77
|
+ }
|
|
|
78
|
+ });
|
|
|
79
|
+ });
|
|
|
80
|
+ }
|
|
|
81
|
+ }
|
|
|
82
|
+
|
|
|
83
|
+ /**
|
|
|
84
|
+ * Removes all keys from this storage.
|
|
|
85
|
+ *
|
|
|
86
|
+ * @returns {void}
|
|
|
87
|
+ */
|
|
|
88
|
+ clear() {
|
|
|
89
|
+ for (const key of this._items.keys()) {
|
|
|
90
|
+ this.removeItem(key);
|
|
|
91
|
+ }
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ /**
|
|
|
95
|
+ * Returns the value associated with a specific key in this storage.
|
|
|
96
|
+ *
|
|
|
97
|
+ * @param {string} key - The name of the key to retrieve the value of.
|
|
|
98
|
+ * @returns {string|null} The value associated with <tt>key</tt> or
|
|
|
99
|
+ * <tt>null</tt>.
|
|
|
100
|
+ */
|
|
|
101
|
+ getItem(key: string) {
|
|
|
102
|
+ return this._items.has(key) ? this._items.get(key) : null;
|
|
|
103
|
+ }
|
|
|
104
|
+
|
|
|
105
|
+ /**
|
|
|
106
|
+ * Returns the name of the nth key in this storage.
|
|
|
107
|
+ *
|
|
|
108
|
+ * @param {number} n - The zero-based integer index of the key to get the
|
|
|
109
|
+ * name of.
|
|
|
110
|
+ * @returns {string} The name of the nth key in this storage.
|
|
|
111
|
+ */
|
|
|
112
|
+ key(n: number) {
|
|
|
113
|
+ let i = 0;
|
|
|
114
|
+
|
|
|
115
|
+ for (const key in this._items.keys()) {
|
|
|
116
|
+ if (i++ === n) {
|
|
|
117
|
+ return key;
|
|
|
118
|
+ }
|
|
|
119
|
+ }
|
|
|
120
|
+ }
|
|
|
121
|
+
|
|
|
122
|
+ /**
|
|
|
123
|
+ * Returns an integer representing the number of data items stored in this
|
|
|
124
|
+ * storage.
|
|
|
125
|
+ *
|
|
|
126
|
+ * @returns {number}
|
|
|
127
|
+ */
|
|
|
128
|
+ get length(): number {
|
|
|
129
|
+ return this._items.size;
|
|
|
130
|
+ }
|
|
|
131
|
+
|
|
|
132
|
+ /**
|
|
|
133
|
+ * Removes a specific key from this storage.
|
|
|
134
|
+ *
|
|
|
135
|
+ * @param {string} key - The name of the key to remove.
|
|
|
136
|
+ * @returns {void}
|
|
|
137
|
+ */
|
|
|
138
|
+ removeItem(key: string) {
|
|
|
139
|
+ this._items.delete(key);
|
|
|
140
|
+ typeof this._keyPrefix === 'undefined'
|
|
|
141
|
+ || AsyncStorage.removeItem(`${String(this._keyPrefix)}${key}`);
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ /**
|
|
|
145
|
+ * Adds a specific key to this storage and associates it with a specific
|
|
|
146
|
+ * value. If the key exists already, updates its value.
|
|
|
147
|
+ *
|
|
|
148
|
+ * @param {string} key - The name of the key to add/update.
|
|
|
149
|
+ * @param {string} value - The value to associate with <tt>key</tt>.
|
|
|
150
|
+ * @returns {void}
|
|
|
151
|
+ */
|
|
|
152
|
+ setItem(key: string, value: string) {
|
|
|
153
|
+ value = String(value); // eslint-disable-line no-param-reassign
|
|
|
154
|
+ this._items.set(key, value);
|
|
|
155
|
+ typeof this._keyPrefix === 'undefined'
|
|
|
156
|
+ || AsyncStorage.setItem(`${String(this._keyPrefix)}${key}`, value);
|
|
|
157
|
+ }
|
|
|
158
|
+}
|