Repository navigation
Expand file tree
/
Copy pathAsyncStorage.web.js
More file actions
228 lines (169 loc) · 4.65 KB
/
Copy pathAsyncStorage.web.js
File metadata and controls
228 lines (169 loc) · 4.65 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Copyright (c) 2015-present, Alibaba Group Holding Limited.
* All rights reserved.
*
* Copyright (c) 2015, Facebook, Inc. All rights reserved.
*
* @providesModule ReactAsyncStorage
*/
'use strict';
import Promise from 'ReactPromise';
var localStorage = window.localStorage;
/**
* Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
*
* @returns obj3 a new object based on obj1 and obj2
*/
function merge(obj1, obj2) {
var obj3 = {};
for (var attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (var attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
}
// AsyncStorage Object
var AsyncStorage = {};
// both methods in localStorage and AsyncStorage
var bothMethods = [
'getItem', 'setItem', 'removeItem', 'clear'
];
/**
* Add 'getItem', 'setItem', 'removeItem', 'clear' to AsyncStorage
*
* Every method returns a `Promise` object.
*/
bothMethods.forEach(function(item) {
var promiseMethod = function() {
var args = arguments;
return new Promise(function(resolve, reject) {
try {
var result = localStorage[item].apply(localStorage, args);
resolve(result);
} catch (err) {
// Maybe in the browser private mode will cause this problem
reject(err);
}
});
};
AsyncStorage[item] = promiseMethod;
});
/**
* Merges existing value with input value, assuming they are stringified json. Returns a `Promise` object.
*/
AsyncStorage.mergeItem = function(key, value) {
return new Promise(function(resolve, reject) {
try {
var oldValue = localStorage.getItem(key);
var oldObj = JSON.parse(oldValue);
var newObj = JSON.parse(value);
// merge define in the file end
var mergedObj = merge(oldObj, newObj);
localStorage.setItem(key, JSON.stringify(mergedObj));
resolve();
} catch (err) {
reject(err);
}
});
};
/**
* Gets *all* keys known to the system, for all callers, libraries, etc. Returns a `Promise` object.
*/
AsyncStorage.getAllKeys = function() {
var keys = [];
for (var i = 0, len = localStorage.length; i < len; ++i) {
keys.push(localStorage.key(i));
}
return new Promise(function(resolve, reject) {
resolve(keys);
});
};
/**
* The following batched functions are useful for executing a lot of
* operations at once, allowing for native optimizations and provide the
* convenience of a single callback after all operations are complete.
*
* These functions return arrays of errors, potentially one for every key.
* For key-specific errors, the Error object will have a key property to
* indicate which key caused the error.
*/
/**
* multiGet invokes callback with an array of key-value pair arrays that
* matches the input format of multiSet. Returns a `Promise` object.
*
* multiGet(['k1', 'k2'], cb) -> cb([['k1', 'val1'], ['k2', 'val2']])
*/
AsyncStorage.multiGet = function() {
var keys = [].splice.call(arguments);
var results = null;
return new Promise(function(resolve, reject) {
try {
results = keys.map(function(key) {
return [
key, localStorage.getItem(key)
];
});
resolve(results);
} catch (err) {
reject(err);
}
});
};
/**
* multiSet and multiMerge take arrays of key-value array pairs that match
* the output of multiGet, e.g. Returns a `Promise` object.
*
* multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
*/
AsyncStorage.multiSet = function() {
var args = [].splice.call(arguments);
return new Promise(function(resolve, reject) {
try {
args.forEach(function(item) {
return localStorage.setItem(item[0], item[1]);
});
resolve();
} catch (err) {
reject(err);
}
});
};
/**
* Delete all the keys in the `keys` array. Returns a `Promise` object.
*/
AsyncStorage.multiRemove = function() {
var keys = [].splice.call(arguments);
return new Promise(function(resolve, reject) {
try {
keys.forEach(function(key) {
return localStorage.removeItem(key);
});
resolve();
} catch (err) {
reject(err);
}
});
};
/**
* Merges existing values with input values, assuming they are stringified
* json. Returns a `Promise` object.
*
* Not supported by all native implementations.
*/
AsyncStorage.multiMerge = function() {
var self = this;
var args = [].splice.call(arguments);
return new Promise(function(resolve, reject) {
try {
var promiseQueue = args.map(function(arg) {
return self.mergeItem(arg[0], arg[1]);
});
resolve(Promise.all(promiseQueue));
} catch (err) {
reject(err);
}
});
};
module.exports = AsyncStorage;