forked from react-native-async-storage/async-storage
/
AsyncStorage.native.js
414 lines (373 loc) · 12.2 KB
/
AsyncStorage.native.js
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
* @jsdoc
*/
'use strict';
import RCTAsyncStorage from './RCTAsyncStorage';
if (!RCTAsyncStorage) {
throw new Error(`[@RNC/AsyncStorage]: NativeModule: AsyncStorage is null.
To fix this issue try these steps:
• Run \`react-native link @react-native-community/async-storage\` in the project root.
• Rebuild and restart the app.
• Run the packager with \`--reset-cache\` flag.
• If you are using CocoaPods on iOS, run \`pod install\` in the \`ios\` directory and then rebuild and re-run the app.
• If this happens while testing with Jest, check out docs how to integrate AsyncStorage with it: https://github.com/react-native-community/async-storage/blob/master/docs/Jest-integration.md
If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-community/react-native-async-storage/issues
`);
}
type ReadOnlyArrayString = $ReadOnlyArray<string>;
type MultiGetCallbackFunction = (
errors: ?$ReadOnlyArray<Error>,
result: ?$ReadOnlyArray<ReadOnlyArrayString>,
) => void;
type MultiRequest = {|
keys: $ReadOnlyArray<string>,
callback: ?MultiGetCallbackFunction,
keyIndex: number,
resolve: ?(result?: Promise<?$ReadOnlyArray<ReadOnlyArrayString>>) => void,
reject: ?(error?: any) => void,
|};
function checkValidInput(usedKey: string, value: any) {
const isValuePassed = arguments.length > 1;
if (typeof usedKey !== 'string') {
console.warn(
`[AsyncStorage] Using ${typeof usedKey} type for key is not supported. This can lead to unexpected behavior/errors. Use string instead.\nKey passed: ${usedKey}\n`,
);
}
if (isValuePassed && typeof value !== 'string') {
if (value == null) {
throw new Error(
`[AsyncStorage] Passing null/undefined as value is not supported. If you want to remove value, Use .remove method instead.\nPassed value: ${value}\nPassed key: ${usedKey}\n`,
);
} else {
console.warn(
`[AsyncStorage] The value for key "${usedKey}" is not a string. This can lead to unexpected behavior/errors. Consider stringifying it.\nPassed value: ${value}\nPassed key: ${usedKey}\n`,
);
}
}
}
/**
* `AsyncStorage` is a simple, unencrypted, asynchronous, persistent, key-value
* storage system that is global to the app. It should be used instead of
* LocalStorage.
*
* See http://reactnative.dev/docs/asyncstorage.html
*/
const AsyncStorage = {
_getRequests: ([]: Array<MultiRequest>),
_getKeys: ([]: Array<string>),
_immediate: (null: ?number),
/**
* Fetches an item for a `key` and invokes a callback upon completion.
*
* See http://reactnative.dev/docs/asyncstorage.html#getitem
*/
getItem: function(
key: string,
callback?: ?(error: ?Error, result: string | null) => void,
): Promise<string | null> {
return new Promise((resolve, reject) => {
checkValidInput(key);
RCTAsyncStorage.multiGet([key], function(errors, result) {
// Unpack result to get value from [[key,value]]
const value = result && result[0] && result[0][1] ? result[0][1] : null;
const errs = convertErrors(errors);
callback && callback(errs && errs[0], value);
if (errs) {
reject(errs[0]);
} else {
resolve(value);
}
});
});
},
/**
* Sets the value for a `key` and invokes a callback upon completion.
*
* See http://reactnative.dev/docs/asyncstorage.html#setitem
*/
setItem: function(
key: string,
value: string,
callback?: ?(error: ?Error) => void,
): Promise<null> {
return new Promise((resolve, reject) => {
checkValidInput(key, value);
RCTAsyncStorage.multiSet([[key, value]], function(errors) {
const errs = convertErrors(errors);
callback && callback(errs && errs[0]);
if (errs) {
reject(errs[0]);
} else {
resolve(null);
}
});
});
},
/**
* Removes an item for a `key` and invokes a callback upon completion.
*
* See http://reactnative.dev/docs/asyncstorage.html#removeitem
*/
removeItem: function(
key: string,
callback?: ?(error: ?Error) => void,
): Promise<null> {
return new Promise((resolve, reject) => {
checkValidInput(key);
RCTAsyncStorage.multiRemove([key], function(errors) {
const errs = convertErrors(errors);
callback && callback(errs && errs[0]);
if (errs) {
reject(errs[0]);
} else {
resolve(null);
}
});
});
},
/**
* Merges an existing `key` value with an input value, assuming both values
* are stringified JSON.
*
* **NOTE:** This is not supported by all native implementations.
*
* See http://reactnative.dev/docs/asyncstorage.html#mergeitem
*/
mergeItem: function(
key: string,
value: string,
callback?: ?(error: ?Error) => void,
): Promise<null> {
return new Promise((resolve, reject) => {
checkValidInput(key, value);
RCTAsyncStorage.multiMerge([[key, value]], function(errors) {
const errs = convertErrors(errors);
callback && callback(errs && errs[0]);
if (errs) {
reject(errs[0]);
} else {
resolve(null);
}
});
});
},
/**
* Erases *all* `AsyncStorage` for all clients, libraries, etc. You probably
* don't want to call this; use `removeItem` or `multiRemove` to clear only
* your app's keys.
*
* See http://reactnative.dev/docs/asyncstorage.html#clear
*/
clear: function(callback?: ?(error: ?Error) => void): Promise<null> {
return new Promise((resolve, reject) => {
RCTAsyncStorage.clear(function(error) {
const err = convertError(error);
callback && callback(err);
if (err) {
reject(err);
} else {
resolve(null);
}
});
});
},
/**
* Gets *all* keys known to your app; for all callers, libraries, etc.
*
* See http://reactnative.dev/docs/asyncstorage.html#getallkeys
*/
getAllKeys: function(
callback?: ?(error: ?Error, keys: ?ReadOnlyArrayString) => void,
): Promise<ReadOnlyArrayString> {
return new Promise((resolve, reject) => {
RCTAsyncStorage.getAllKeys(function(error, keys) {
const err = convertError(error);
callback && callback(err, keys);
if (err) {
reject(err);
} else {
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.
*/
/**
* Flushes any pending requests using a single batch call to get the data.
*
* See http://reactnative.dev/docs/asyncstorage.html#flushgetrequests
* */
flushGetRequests: function(): void {
const getRequests = this._getRequests;
const getKeys = this._getKeys;
this._getRequests = [];
this._getKeys = [];
RCTAsyncStorage.multiGet(getKeys, function(errors, result) {
// Even though the runtime complexity of this is theoretically worse vs if we used a map,
// it's much, much faster in practice for the data sets we deal with (we avoid
// allocating result pair arrays). This was heavily benchmarked.
//
// Is there a way to avoid using the map but fix the bug in this breaking test?
// https://github.com/facebook/react-native/commit/8dd8ad76579d7feef34c014d387bf02065692264
const map = {};
result &&
result.forEach(([key, value]) => {
map[key] = value;
return value;
});
const reqLength = getRequests.length;
for (let i = 0; i < reqLength; i++) {
const request = getRequests[i];
const requestKeys = request.keys;
const requestResult = requestKeys.map(key => [key, map[key]]);
request.callback && request.callback(null, requestResult);
request.resolve && request.resolve(requestResult);
}
});
},
/**
* This allows you to batch the fetching of items given an array of `key`
* inputs. Your callback will be invoked with an array of corresponding
* key-value pairs found.
*
* See http://reactnative.dev/docs/asyncstorage.html#multiget
*/
multiGet: function(
keys: Array<string>,
callback?: ?MultiGetCallbackFunction,
): Promise<?$ReadOnlyArray<ReadOnlyArrayString>> {
if (!this._immediate) {
this._immediate = setImmediate(() => {
this._immediate = null;
this.flushGetRequests();
});
}
const getRequest: MultiRequest = {
keys: keys,
callback: callback,
// do we need this?
keyIndex: this._getKeys.length,
resolve: null,
reject: null,
};
const promiseResult = new Promise((resolve, reject) => {
getRequest.resolve = resolve;
getRequest.reject = reject;
});
this._getRequests.push(getRequest);
// avoid fetching duplicates
keys.forEach(key => {
if (this._getKeys.indexOf(key) === -1) {
this._getKeys.push(key);
}
});
return promiseResult;
},
/**
* Use this as a batch operation for storing multiple key-value pairs. When
* the operation completes you'll get a single callback with any errors.
*
* See http://reactnative.dev/docs/asyncstorage.html#multiset
*/
multiSet: function(
keyValuePairs: Array<Array<string>>,
callback?: ?(errors: ?$ReadOnlyArray<?Error>) => void,
): Promise<null> {
return new Promise((resolve, reject) => {
keyValuePairs.forEach(([key, value]) => {
checkValidInput(key, value);
});
RCTAsyncStorage.multiSet(keyValuePairs, function(errors) {
const error = convertErrors(errors);
callback && callback(error);
if (error) {
reject(error);
} else {
resolve(null);
}
});
});
},
/**
* Call this to batch the deletion of all keys in the `keys` array.
*
* See http://reactnative.dev/docs/asyncstorage.html#multiremove
*/
multiRemove: function(
keys: Array<string>,
callback?: ?(errors: ?$ReadOnlyArray<?Error>) => void,
): Promise<null> {
return new Promise((resolve, reject) => {
keys.forEach(key => checkValidInput(key));
RCTAsyncStorage.multiRemove(keys, function(errors) {
const error = convertErrors(errors);
callback && callback(error);
if (error) {
reject(error);
} else {
resolve(null);
}
});
});
},
/**
* Batch operation to merge in existing and new values for a given set of
* keys. This assumes that the values are stringified JSON.
*
* **NOTE**: This is not supported by all native implementations.
*
* See http://reactnative.dev/docs/asyncstorage.html#multimerge
*/
multiMerge: function(
keyValuePairs: Array<Array<string>>,
callback?: ?(errors: ?$ReadOnlyArray<?Error>) => void,
): Promise<null> {
return new Promise((resolve, reject) => {
RCTAsyncStorage.multiMerge(keyValuePairs, function(errors) {
const error = convertErrors(errors);
callback && callback(error);
if (error) {
reject(error);
} else {
resolve(null);
}
});
});
},
};
// Not all native implementations support merge.
if (!RCTAsyncStorage.multiMerge) {
delete AsyncStorage.mergeItem;
delete AsyncStorage.multiMerge;
}
function convertErrors(errs): ?$ReadOnlyArray<?Error> {
if (!errs || (Array.isArray(errs) && errs.length === 0)) {
return null;
}
return (Array.isArray(errs) ? errs : [errs]).map(e => convertError(e));
}
function convertError(error): ?Error {
if (!error) {
return null;
}
const out = new Error(error.message);
// $FlowFixMe: adding custom properties to error.
out.key = error.key;
return out;
}
export default AsyncStorage;