2015-03-11 19:06:03 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-03-23 20:35:08 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-11 19:06:03 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-12-02 03:09:01 +00:00
|
|
|
* @noflow
|
2015-03-11 19:06:03 +00:00
|
|
|
* @flow-weak
|
2016-06-24 15:42:28 +00:00
|
|
|
* @jsdoc
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-03-11 19:06:03 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-02-18 00:39:50 +00:00
|
|
|
const NativeModules = require('NativeModules');
|
2015-03-11 19:06:03 +00:00
|
|
|
|
2015-10-27 16:59:30 +00:00
|
|
|
// Use RocksDB if available, then SQLite, then file storage.
|
2018-05-11 02:06:46 +00:00
|
|
|
const RCTAsyncStorage =
|
|
|
|
NativeModules.AsyncRocksDBStorage ||
|
2017-02-21 23:04:52 +00:00
|
|
|
NativeModules.AsyncSQLiteDBStorage ||
|
2017-02-18 00:39:50 +00:00
|
|
|
NativeModules.AsyncLocalStorage;
|
2015-03-11 19:06:03 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-20 21:07:10 +00:00
|
|
|
* `AsyncStorage` is a simple, unencrypted, asynchronous, persistent, key-value
|
2018-02-22 15:04:35 +00:00
|
|
|
* storage system that is global to the app. It should be used instead of
|
2017-11-20 21:07:10 +00:00
|
|
|
* LocalStorage.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
2018-05-10 22:44:52 +00:00
|
|
|
const AsyncStorage = {
|
2015-12-03 16:08:37 +00:00
|
|
|
_getRequests: ([]: Array<any>),
|
|
|
|
_getKeys: ([]: Array<string>),
|
|
|
|
_immediate: (null: ?number),
|
|
|
|
|
2015-03-11 19:06:03 +00:00
|
|
|
/**
|
2016-06-24 15:42:28 +00:00
|
|
|
* Fetches an item for a `key` and invokes a callback upon completion.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#getitem
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
getItem: function(
|
|
|
|
key: string,
|
2018-05-11 02:06:46 +00:00
|
|
|
callback?: ?(error: ?Error, result: ?string) => void,
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiGet([key], function(errors, result) {
|
|
|
|
// Unpack result to get value from [[key,value]]
|
2018-05-11 02:06:46 +00:00
|
|
|
const value = result && result[0] && result[0][1] ? result[0][1] : null;
|
2018-05-10 22:44:52 +00:00
|
|
|
const errs = convertErrors(errors);
|
2015-10-28 18:59:06 +00:00
|
|
|
callback && callback(errs && errs[0], value);
|
|
|
|
if (errs) {
|
|
|
|
reject(errs[0]);
|
2015-04-07 09:02:05 +00:00
|
|
|
} else {
|
|
|
|
resolve(value);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-06-24 15:42:28 +00:00
|
|
|
* Sets the value for a `key` and invokes a callback upon completion.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#setitem
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
setItem: function(
|
|
|
|
key: string,
|
|
|
|
value: string,
|
2018-05-11 02:06:46 +00:00
|
|
|
callback?: ?(error: ?Error) => void,
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-11 02:06:46 +00:00
|
|
|
RCTAsyncStorage.multiSet([[key, value]], function(errors) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const errs = convertErrors(errors);
|
2015-10-28 18:59:06 +00:00
|
|
|
callback && callback(errs && errs[0]);
|
|
|
|
if (errs) {
|
|
|
|
reject(errs[0]);
|
2015-04-07 09:02:05 +00:00
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
2015-10-27 16:59:30 +00:00
|
|
|
|
2015-04-07 09:02:05 +00:00
|
|
|
/**
|
2016-06-24 15:42:28 +00:00
|
|
|
* Removes an item for a `key` and invokes a callback upon completion.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#removeitem
|
2015-04-07 09:02:05 +00:00
|
|
|
*/
|
2015-03-11 19:06:03 +00:00
|
|
|
removeItem: function(
|
|
|
|
key: string,
|
2018-05-11 02:06:46 +00:00
|
|
|
callback?: ?(error: ?Error) => void,
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiRemove([key], function(errors) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const errs = convertErrors(errors);
|
2015-10-28 18:59:06 +00:00
|
|
|
callback && callback(errs && errs[0]);
|
|
|
|
if (errs) {
|
|
|
|
reject(errs[0]);
|
2015-04-07 09:02:05 +00:00
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-06-24 15:42:28 +00:00
|
|
|
* Merges an existing `key` value with an input value, assuming both values
|
2017-11-20 21:07:10 +00:00
|
|
|
* are stringified JSON.
|
2016-03-15 00:37:34 +00:00
|
|
|
*
|
2016-06-24 15:42:28 +00:00
|
|
|
* **NOTE:** This is not supported by all native implementations.
|
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#mergeitem
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
mergeItem: function(
|
|
|
|
key: string,
|
|
|
|
value: string,
|
2018-05-11 02:06:46 +00:00
|
|
|
callback?: ?(error: ?Error) => void,
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-11 02:06:46 +00:00
|
|
|
RCTAsyncStorage.multiMerge([[key, value]], function(errors) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const errs = convertErrors(errors);
|
2015-10-28 18:59:06 +00:00
|
|
|
callback && callback(errs && errs[0]);
|
|
|
|
if (errs) {
|
|
|
|
reject(errs[0]);
|
2015-04-07 09:02:05 +00:00
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-11-20 21:07:10 +00:00
|
|
|
* Erases *all* `AsyncStorage` for all clients, libraries, etc. You probably
|
2016-06-24 15:42:28 +00:00
|
|
|
* don't want to call this; use `removeItem` or `multiRemove` to clear only
|
2017-11-20 21:07:10 +00:00
|
|
|
* your app's keys.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#clear
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
2015-05-02 01:29:43 +00:00
|
|
|
clear: function(callback?: ?(error: ?Error) => void): Promise {
|
2015-04-07 09:02:05 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.clear(function(error) {
|
|
|
|
callback && callback(convertError(error));
|
2018-05-11 02:06:46 +00:00
|
|
|
if (error && convertError(error)) {
|
2015-04-07 09:02:05 +00:00
|
|
|
reject(convertError(error));
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-06-24 15:42:28 +00:00
|
|
|
* Gets *all* keys known to your app; for all callers, libraries, etc.
|
2016-03-15 00:37:34 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#getallkeys
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
getAllKeys: function(
|
|
|
|
callback?: ?(error: ?Error, keys: ?Array<string>) => void,
|
|
|
|
): Promise {
|
2015-04-07 09:02:05 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.getAllKeys(function(error, keys) {
|
|
|
|
callback && callback(convertError(error), keys);
|
|
|
|
if (error) {
|
|
|
|
reject(convertError(error));
|
|
|
|
} else {
|
|
|
|
resolve(keys);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-02-22 15:04:35 +00:00
|
|
|
/**
|
2017-11-20 21:07:10 +00:00
|
|
|
* Flushes any pending requests using a single batch call to get the data.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#flushgetrequests
|
|
|
|
* */
|
2015-12-03 16:08:37 +00:00
|
|
|
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.
|
2016-02-06 00:44:44 +00:00
|
|
|
//
|
|
|
|
// 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
|
2016-08-09 13:32:41 +00:00
|
|
|
const map = {};
|
2018-05-11 02:06:46 +00:00
|
|
|
result &&
|
|
|
|
result.forEach(([key, value]) => {
|
|
|
|
map[key] = value;
|
|
|
|
return value;
|
|
|
|
});
|
2015-12-03 16:08:37 +00:00
|
|
|
const reqLength = getRequests.length;
|
|
|
|
for (let i = 0; i < reqLength; i++) {
|
|
|
|
const request = getRequests[i];
|
|
|
|
const requestKeys = request.keys;
|
2016-08-09 13:32:41 +00:00
|
|
|
const requestResult = requestKeys.map(key => [key, map[key]]);
|
2015-12-03 16:08:37 +00:00
|
|
|
request.callback && request.callback(null, requestResult);
|
|
|
|
request.resolve && request.resolve(requestResult);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-03-11 19:06:03 +00:00
|
|
|
/**
|
2016-06-24 15:42:28 +00:00
|
|
|
* 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
|
2017-11-20 21:07:10 +00:00
|
|
|
* key-value pairs found.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#multiget
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
multiGet: function(
|
|
|
|
keys: Array<string>,
|
2018-05-11 02:06:46 +00:00
|
|
|
callback?: ?(errors: ?Array<Error>, result: ?Array<Array<string>>) => void,
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
2015-12-03 16:08:37 +00:00
|
|
|
if (!this._immediate) {
|
|
|
|
this._immediate = setImmediate(() => {
|
|
|
|
this._immediate = null;
|
|
|
|
this.flushGetRequests();
|
2015-04-07 09:02:05 +00:00
|
|
|
});
|
2015-12-03 16:08:37 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const getRequest = {
|
2015-12-03 16:08:37 +00:00
|
|
|
keys: keys,
|
|
|
|
callback: callback,
|
2016-02-06 00:44:44 +00:00
|
|
|
// do we need this?
|
2015-12-03 16:08:37 +00:00
|
|
|
keyIndex: this._getKeys.length,
|
|
|
|
resolve: null,
|
|
|
|
reject: null,
|
|
|
|
};
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const promiseResult = new Promise((resolve, reject) => {
|
2015-12-03 16:08:37 +00:00
|
|
|
getRequest.resolve = resolve;
|
|
|
|
getRequest.reject = reject;
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
2015-12-03 16:08:37 +00:00
|
|
|
|
|
|
|
this._getRequests.push(getRequest);
|
2016-02-06 00:44:44 +00:00
|
|
|
// avoid fetching duplicates
|
|
|
|
keys.forEach(key => {
|
|
|
|
if (this._getKeys.indexOf(key) === -1) {
|
|
|
|
this._getKeys.push(key);
|
|
|
|
}
|
|
|
|
});
|
2015-12-03 16:08:37 +00:00
|
|
|
|
|
|
|
return promiseResult;
|
2015-03-11 19:06:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-06-24 15:42:28 +00:00
|
|
|
* Use this as a batch operation for storing multiple key-value pairs. When
|
2017-11-20 21:07:10 +00:00
|
|
|
* the operation completes you'll get a single callback with any errors.
|
2015-03-11 19:06:03 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#multiset
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
multiSet: function(
|
|
|
|
keyValuePairs: Array<Array<string>>,
|
2018-05-11 02:06:46 +00:00
|
|
|
callback?: ?(errors: ?Array<Error>) => void,
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiSet(keyValuePairs, function(errors) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const error = convertErrors(errors);
|
2015-04-07 09:02:05 +00:00
|
|
|
callback && callback(error);
|
2015-10-28 18:59:06 +00:00
|
|
|
if (error) {
|
2015-04-07 09:02:05 +00:00
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2017-11-20 21:07:10 +00:00
|
|
|
* Call this to batch the deletion of all keys in the `keys` array.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#multiremove
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
multiRemove: function(
|
|
|
|
keys: Array<string>,
|
2018-05-11 02:06:46 +00:00
|
|
|
callback?: ?(errors: ?Array<Error>) => void,
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiRemove(keys, function(errors) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const error = convertErrors(errors);
|
2015-04-07 09:02:05 +00:00
|
|
|
callback && callback(error);
|
2015-10-28 18:59:06 +00:00
|
|
|
if (error) {
|
2015-04-07 09:02:05 +00:00
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-06-24 15:42:28 +00:00
|
|
|
* Batch operation to merge in existing and new values for a given set of
|
2017-11-20 21:07:10 +00:00
|
|
|
* keys. This assumes that the values are stringified JSON.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2016-06-24 15:42:28 +00:00
|
|
|
* **NOTE**: This is not supported by all native implementations.
|
2018-02-22 15:04:35 +00:00
|
|
|
*
|
2017-11-20 21:07:10 +00:00
|
|
|
* See http://facebook.github.io/react-native/docs/asyncstorage.html#multimerge
|
2015-03-11 19:06:03 +00:00
|
|
|
*/
|
|
|
|
multiMerge: function(
|
|
|
|
keyValuePairs: Array<Array<string>>,
|
2018-05-11 02:06:46 +00:00
|
|
|
callback?: ?(errors: ?Array<Error>) => void,
|
2015-04-07 09:02:05 +00:00
|
|
|
): Promise {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
RCTAsyncStorage.multiMerge(keyValuePairs, function(errors) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const error = convertErrors(errors);
|
2015-04-07 09:02:05 +00:00
|
|
|
callback && callback(error);
|
2015-10-28 18:59:06 +00:00
|
|
|
if (error) {
|
2015-04-07 09:02:05 +00:00
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
});
|
2015-03-11 19:06:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Not all native implementations support merge.
|
2015-03-17 10:08:46 +00:00
|
|
|
if (!RCTAsyncStorage.multiMerge) {
|
2015-03-11 19:06:03 +00:00
|
|
|
delete AsyncStorage.mergeItem;
|
|
|
|
delete AsyncStorage.multiMerge;
|
|
|
|
}
|
|
|
|
|
2015-10-28 18:59:06 +00:00
|
|
|
function convertErrors(errs) {
|
|
|
|
if (!errs) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-11 02:06:46 +00:00
|
|
|
return (Array.isArray(errs) ? errs : [errs]).map(e => convertError(e));
|
2015-10-28 18:59:06 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:06:03 +00:00
|
|
|
function convertError(error) {
|
|
|
|
if (!error) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const out = new Error(error.message);
|
2015-03-11 19:06:03 +00:00
|
|
|
out.key = error.key; // flow doesn't like this :(
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AsyncStorage;
|