|
@@ -1,5 +1,3 @@
|
1
|
|
-/* @flow */
|
2
|
|
-
|
3
|
1
|
import { AsyncStorage } from 'react-native';
|
4
|
2
|
|
5
|
3
|
/**
|
|
@@ -13,10 +11,6 @@ import { AsyncStorage } from 'react-native';
|
13
|
11
|
* asynchronously. If an asynchronous operation produces an error, it's ignored.
|
14
|
12
|
*/
|
15
|
13
|
export default class Storage {
|
16
|
|
- _items: Map<string, string>;
|
17
|
|
-
|
18
|
|
- _keyPrefix: ?string;
|
19
|
|
-
|
20
|
14
|
/**
|
21
|
15
|
* Initializes a new <tt>Storage</tt> instance. Loads all previously
|
22
|
16
|
* persisted data items from React Native's <tt>AsyncStorage</tt> if
|
|
@@ -25,15 +19,7 @@ export default class Storage {
|
25
|
19
|
* @param {string|undefined} keyPrefix - The prefix of the
|
26
|
20
|
* <tt>AsyncStorage</tt> keys to be persisted by this storage.
|
27
|
21
|
*/
|
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
|
|
-
|
|
22
|
+ constructor(keyPrefix) {
|
37
|
23
|
/**
|
38
|
24
|
* The prefix of the <tt>AsyncStorage</tt> keys persisted by this
|
39
|
25
|
* storage. If <tt>undefined</tt>, then the data items stored in this
|
|
@@ -71,8 +57,8 @@ export default class Storage {
|
71
|
57
|
// that it is technically possible to invoke setItem
|
72
|
58
|
// with a key before the key is loaded from
|
73
|
59
|
// AsyncStorage.
|
74
|
|
- if (!this._items.has(key)) {
|
75
|
|
- this._items.set(key, value);
|
|
60
|
+ if (!this.hasOwnProperty(key)) {
|
|
61
|
+ this[key] = value;
|
76
|
62
|
}
|
77
|
63
|
}
|
78
|
64
|
});
|
|
@@ -86,7 +72,7 @@ export default class Storage {
|
86
|
72
|
* @returns {void}
|
87
|
73
|
*/
|
88
|
74
|
clear() {
|
89
|
|
- for (const key of this._items.keys()) {
|
|
75
|
+ for (const key of Object.keys(this)) {
|
90
|
76
|
this.removeItem(key);
|
91
|
77
|
}
|
92
|
78
|
}
|
|
@@ -98,8 +84,8 @@ export default class Storage {
|
98
|
84
|
* @returns {string|null} The value associated with <tt>key</tt> or
|
99
|
85
|
* <tt>null</tt>.
|
100
|
86
|
*/
|
101
|
|
- getItem(key: string) {
|
102
|
|
- return this._items.has(key) ? this._items.get(key) : null;
|
|
87
|
+ getItem(key) {
|
|
88
|
+ return this.hasOwnProperty(key) ? this[key] : null;
|
103
|
89
|
}
|
104
|
90
|
|
105
|
91
|
/**
|
|
@@ -109,14 +95,10 @@ export default class Storage {
|
109
|
95
|
* name of.
|
110
|
96
|
* @returns {string} The name of the nth key in this storage.
|
111
|
97
|
*/
|
112
|
|
- key(n: number) {
|
113
|
|
- let i = 0;
|
|
98
|
+ key(n) {
|
|
99
|
+ const keys = Object.keys(this);
|
114
|
100
|
|
115
|
|
- for (const key in this._items.keys()) {
|
116
|
|
- if (i++ === n) {
|
117
|
|
- return key;
|
118
|
|
- }
|
119
|
|
- }
|
|
101
|
+ return n < keys.length ? keys[n] : null;
|
120
|
102
|
}
|
121
|
103
|
|
122
|
104
|
/**
|
|
@@ -125,8 +107,8 @@ export default class Storage {
|
125
|
107
|
*
|
126
|
108
|
* @returns {number}
|
127
|
109
|
*/
|
128
|
|
- get length(): number {
|
129
|
|
- return this._items.size;
|
|
110
|
+ get length() {
|
|
111
|
+ return Object.keys(this).length;
|
130
|
112
|
}
|
131
|
113
|
|
132
|
114
|
/**
|
|
@@ -135,8 +117,8 @@ export default class Storage {
|
135
|
117
|
* @param {string} key - The name of the key to remove.
|
136
|
118
|
* @returns {void}
|
137
|
119
|
*/
|
138
|
|
- removeItem(key: string) {
|
139
|
|
- this._items.delete(key);
|
|
120
|
+ removeItem(key) {
|
|
121
|
+ delete this[key];
|
140
|
122
|
typeof this._keyPrefix === 'undefined'
|
141
|
123
|
|| AsyncStorage.removeItem(`${String(this._keyPrefix)}${key}`);
|
142
|
124
|
}
|
|
@@ -149,9 +131,9 @@ export default class Storage {
|
149
|
131
|
* @param {string} value - The value to associate with <tt>key</tt>.
|
150
|
132
|
* @returns {void}
|
151
|
133
|
*/
|
152
|
|
- setItem(key: string, value: string) {
|
|
134
|
+ setItem(key, value) {
|
153
|
135
|
value = String(value); // eslint-disable-line no-param-reassign
|
154
|
|
- this._items.set(key, value);
|
|
136
|
+ this[key] = value;
|
155
|
137
|
typeof this._keyPrefix === 'undefined'
|
156
|
138
|
|| AsyncStorage.setItem(`${String(this._keyPrefix)}${key}`, value);
|
157
|
139
|
}
|