Socket
Socket
Sign inDemoInstall

@react-native-async-storage/async-storage

Package Overview
Dependencies
Maintainers
1
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-native-async-storage/async-storage - npm Package Compare versions

Comparing version 1.15.17 to 1.16.0

lib/commonjs/helpers.js

2

jest/async-storage-mock.js

@@ -31,3 +31,3 @@ /**

mergeItem: jest.fn((key, value, callback) =>
asMock.multiMerge([[key, value]], callback),
asMock.multiMerge([[key, value]], callback)
),

@@ -34,0 +34,0 @@

@@ -18,5 +18,4 @@ "use strict";

* LICENSE file in the root directory of this source tree.
*
*
*/
// @ts-ignore Cannot find module 'merge-options' or its corresponding type declarations
const merge = _mergeOptions.default.bind({

@@ -27,96 +26,81 @@ concatArrays: true,

const mergeLocalStorageItem = (key, value) => {
function mergeLocalStorageItem(key, value) {
const oldValue = window.localStorage.getItem(key);
const oldObject = JSON.parse(oldValue);
const newObject = JSON.parse(value);
const nextValue = JSON.stringify(merge(oldObject, newObject));
window.localStorage.setItem(key, nextValue);
};
const createPromise = (getValue, callback) => {
if (oldValue) {
const oldObject = JSON.parse(oldValue);
const newObject = JSON.parse(value);
const nextValue = JSON.stringify(merge(oldObject, newObject));
window.localStorage.setItem(key, nextValue);
} else {
window.localStorage.setItem(key, value);
}
}
function createPromise(getValue, callback) {
return new Promise((resolve, reject) => {
try {
const value = getValue();
if (callback) {
callback(null, value);
}
callback === null || callback === void 0 ? void 0 : callback(null, value);
resolve(value);
} catch (err) {
if (callback) {
callback(err);
}
callback === null || callback === void 0 ? void 0 : callback(err);
reject(err);
}
});
};
}
const createPromiseAll = (promises, callback, processResult) => {
function createPromiseAll(promises, callback, processResult) {
return Promise.all(promises).then(result => {
const value = processResult ? processResult(result) : null;
callback && callback(null, value);
var _processResult;
const value = (_processResult = processResult === null || processResult === void 0 ? void 0 : processResult(result)) !== null && _processResult !== void 0 ? _processResult : null;
callback === null || callback === void 0 ? void 0 : callback(null, value);
return Promise.resolve(value);
}, errors => {
callback && callback(errors);
callback === null || callback === void 0 ? void 0 : callback(errors);
return Promise.reject(errors);
});
};
}
class AsyncStorage {
const AsyncStorage = {
/**
* Fetches `key` value.
*/
static getItem(key, callback) {
return createPromise(() => {
return window.localStorage.getItem(key);
}, callback);
}
getItem: (key, callback) => {
return createPromise(() => window.localStorage.getItem(key), callback);
},
/**
* Sets `value` for `key`.
*/
setItem: (key, value, callback) => {
return createPromise(() => window.localStorage.setItem(key, value), callback);
},
static setItem(key, value, callback) {
return createPromise(() => {
window.localStorage.setItem(key, value);
}, callback);
}
/**
* Removes a `key`
*/
removeItem: (key, callback) => {
return createPromise(() => window.localStorage.removeItem(key), callback);
},
static removeItem(key, callback) {
return createPromise(() => {
return window.localStorage.removeItem(key);
}, callback);
}
/**
* Merges existing value with input value, assuming they are stringified JSON.
*/
mergeItem: (key, value, callback) => {
return createPromise(() => mergeLocalStorageItem(key, value), callback);
},
static mergeItem(key, value, callback) {
return createPromise(() => {
mergeLocalStorageItem(key, value);
}, callback);
}
/**
* Erases *all* AsyncStorage for the domain.
*/
clear: callback => {
return createPromise(() => window.localStorage.clear(), callback);
},
static clear(callback) {
return createPromise(() => {
window.localStorage.clear();
}, callback);
}
/**
* Gets *all* keys known to the app, for all callers, libraries, etc.
*/
static getAllKeys(callback) {
getAllKeys: callback => {
return createPromise(() => {

@@ -127,3 +111,3 @@ const numberOfKeys = window.localStorage.length;

for (let i = 0; i < numberOfKeys; i += 1) {
const key = window.localStorage.key(i);
const key = window.localStorage.key(i) || '';
keys.push(key);

@@ -134,9 +118,9 @@ }

}, callback);
}
},
/**
* (stub) Flushes any pending requests using a single batch call to get the data.
*/
flushGetRequests: () => undefined,
static flushGetRequests() {}
/**

@@ -148,5 +132,3 @@ * multiGet resolves to an array of key-value pair arrays that matches the

*/
static multiGet(keys, callback) {
multiGet: (keys, callback) => {
const promises = keys.map(key => AsyncStorage.getItem(key));

@@ -157,3 +139,4 @@

return createPromiseAll(promises, callback, processResult);
}
},
/**

@@ -163,17 +146,15 @@ * Takes an array of key-value array pairs.

*/
static multiSet(keyValuePairs, callback) {
multiSet: (keyValuePairs, callback) => {
const promises = keyValuePairs.map(item => AsyncStorage.setItem(item[0], item[1]));
return createPromiseAll(promises, callback);
}
},
/**
* Delete all the keys in the `keys` array.
*/
static multiRemove(keys, callback) {
multiRemove: (keys, callback) => {
const promises = keys.map(key => AsyncStorage.removeItem(key));
return createPromiseAll(promises, callback);
}
},
/**

@@ -185,12 +166,13 @@ * Takes an array of key-value array pairs and merges them with existing

*/
multiMerge: (keyValuePairs, callback) => {
const promises = keyValuePairs.map(item => {
var _AsyncStorage$mergeIt, _AsyncStorage$mergeIt2;
static multiMerge(keyValuePairs, callback) {
const promises = keyValuePairs.map(item => AsyncStorage.mergeItem(item[0], item[1]));
return (_AsyncStorage$mergeIt = (_AsyncStorage$mergeIt2 = AsyncStorage.mergeItem) === null || _AsyncStorage$mergeIt2 === void 0 ? void 0 : _AsyncStorage$mergeIt2.call(AsyncStorage, item[0], item[1])) !== null && _AsyncStorage$mergeIt !== void 0 ? _AsyncStorage$mergeIt : Promise.reject('Not implemented');
});
return createPromiseAll(promises, callback);
}
}
exports.default = AsyncStorage;
};
var _default = AsyncStorage;
exports.default = _default;
//# sourceMappingURL=AsyncStorage.js.map

@@ -1,12 +0,2 @@

/**
* 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
*
* @jsdoc
*/
'use strict';
"use strict";

@@ -18,2 +8,4 @@ Object.defineProperty(exports, "__esModule", {

var _helpers = require("./helpers");
var _RCTAsyncStorage = _interopRequireDefault(require("./RCTAsyncStorage"));

@@ -23,2 +15,8 @@

/**
* 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.
*/
if (!_RCTAsyncStorage.default) {

@@ -29,4 +27,2 @@ throw new Error(`[@RNC/AsyncStorage]: NativeModule: AsyncStorage is null.

• Run \`react-native link @react-native-async-storage/async-storage\` in the project root.
• Rebuild and restart the app.

@@ -40,388 +36,345 @@

If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-async-storage/react-native-async-storage/issues
If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-async-storage/async-storage/issues
`);
}
function checkValidInput(usedKey, value) {
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 .removeItem 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`);
}
}
}
function checkValidArgs(keyValuePairs, callback) {
if (!Array.isArray(keyValuePairs) || keyValuePairs.length === 0 || !Array.isArray(keyValuePairs[0])) {
throw new Error('[AsyncStorage] Expected array of key-value pairs as first argument to multiSet');
}
if (callback && typeof callback !== 'function') {
if (Array.isArray(callback)) {
throw new Error('[AsyncStorage] Expected function as second argument to multiSet. Did you forget to wrap key-value pairs in an array for the first argument?');
}
throw new Error('[AsyncStorage] Expected function as second argument to multiSet');
}
}
/**
* `AsyncStorage` is a simple, unencrypted, asynchronous, persistent, key-value
* storage system that is global to the app. It should be used instead of
* storage system that is global to the app. It should be used instead of
* LocalStorage.
*
* See http://reactnative.dev/docs/asyncstorage.html
* See https://react-native-async-storage.github.io/async-storage/docs/api
*/
const AsyncStorage = {
_getRequests: [],
_getKeys: [],
_immediate: null,
const AsyncStorage = (() => {
let _getRequests = [];
let _getKeys = [];
let _immediate = null;
return {
/**
* Fetches an item for a `key` and invokes a callback upon completion.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#getitem
*/
getItem: (key, callback) => {
return new Promise((resolve, reject) => {
(0, _helpers.checkValidInput)(key);
/**
* Fetches an item for a `key` and invokes a callback upon completion.
*
* See http://reactnative.dev/docs/asyncstorage.html#getitem
*/
getItem: function (key, callback) {
return new Promise((resolve, reject) => {
checkValidInput(key);
_RCTAsyncStorage.default.multiGet([key], (errors, result) => {
var _result$;
_RCTAsyncStorage.default.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);
// Unpack result to get value from [[key,value]]
const value = result !== null && result !== void 0 && (_result$ = result[0]) !== null && _result$ !== void 0 && _result$[1] ? result[0][1] : null;
const errs = (0, _helpers.convertErrors)(errors);
callback === null || callback === void 0 ? void 0 : callback(errs === null || errs === void 0 ? void 0 : errs[0], value);
if (errs) {
reject(errs[0]);
} else {
resolve(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, value, callback) {
return new Promise((resolve, reject) => {
checkValidInput(key, value);
/**
* Sets the value for a `key` and invokes a callback upon completion.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#setitem
*/
setItem: (key, value, callback) => {
return new Promise((resolve, reject) => {
(0, _helpers.checkValidInput)(key, value);
_RCTAsyncStorage.default.multiSet([[key, value]], function (errors) {
const errs = convertErrors(errors);
callback && callback(errs && errs[0]);
_RCTAsyncStorage.default.multiSet([[key, value]], errors => {
const errs = (0, _helpers.convertErrors)(errors);
callback === null || callback === void 0 ? void 0 : callback(errs === null || errs === void 0 ? void 0 : errs[0]);
if (errs) {
reject(errs[0]);
} else {
resolve(null);
}
if (errs) {
reject(errs[0]);
} else {
resolve();
}
});
});
});
},
},
/**
* Removes an item for a `key` and invokes a callback upon completion.
*
* See http://reactnative.dev/docs/asyncstorage.html#removeitem
*/
removeItem: function (key, callback) {
return new Promise((resolve, reject) => {
checkValidInput(key);
/**
* Removes an item for a `key` and invokes a callback upon completion.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#removeitem
*/
removeItem: (key, callback) => {
return new Promise((resolve, reject) => {
(0, _helpers.checkValidInput)(key);
_RCTAsyncStorage.default.multiRemove([key], function (errors) {
const errs = convertErrors(errors);
callback && callback(errs && errs[0]);
_RCTAsyncStorage.default.multiRemove([key], errors => {
const errs = (0, _helpers.convertErrors)(errors);
callback === null || callback === void 0 ? void 0 : callback(errs === null || errs === void 0 ? void 0 : errs[0]);
if (errs) {
reject(errs[0]);
} else {
resolve(null);
}
if (errs) {
reject(errs[0]);
} else {
resolve();
}
});
});
});
},
},
/**
* 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, value, callback) {
return new Promise((resolve, reject) => {
checkValidInput(key, value);
/**
* 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 https://react-native-async-storage.github.io/async-storage/docs/api#mergeitem
*/
mergeItem: (key, value, callback) => {
return new Promise((resolve, reject) => {
(0, _helpers.checkValidInput)(key, value);
_RCTAsyncStorage.default.multiMerge([[key, value]], function (errors) {
const errs = convertErrors(errors);
callback && callback(errs && errs[0]);
_RCTAsyncStorage.default.multiMerge([[key, value]], errors => {
const errs = (0, _helpers.convertErrors)(errors);
callback === null || callback === void 0 ? void 0 : callback(errs === null || errs === void 0 ? void 0 : errs[0]);
if (errs) {
reject(errs[0]);
} else {
resolve(null);
}
if (errs) {
reject(errs[0]);
} else {
resolve();
}
});
});
});
},
},
/**
* 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) {
return new Promise((resolve, reject) => {
_RCTAsyncStorage.default.clear(function (error) {
const err = convertError(error);
callback && callback(err);
/**
* 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 https://react-native-async-storage.github.io/async-storage/docs/api#clear
*/
clear: callback => {
return new Promise((resolve, reject) => {
_RCTAsyncStorage.default.clear(error => {
const err = (0, _helpers.convertError)(error);
callback === null || callback === void 0 ? void 0 : callback(err);
if (err) {
reject(err);
} else {
resolve(null);
}
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
},
},
/**
* Gets *all* keys known to your app; for all callers, libraries, etc.
*
* See http://reactnative.dev/docs/asyncstorage.html#getallkeys
*/
getAllKeys: function (callback) {
return new Promise((resolve, reject) => {
_RCTAsyncStorage.default.getAllKeys(function (error, keys) {
const err = convertError(error);
callback && callback(err, keys);
/**
* Gets *all* keys known to your app; for all callers, libraries, etc.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#getallkeys
*/
getAllKeys: callback => {
return new Promise((resolve, reject) => {
_RCTAsyncStorage.default.getAllKeys((error, keys) => {
const err = (0, _helpers.convertError)(error);
callback === null || callback === void 0 ? void 0 : callback(err, keys);
if (err) {
reject(err);
} else {
resolve(keys);
}
if (keys) {
resolve(keys);
} else {
reject(err);
}
});
});
});
},
},
/**
* 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.
*/
/**
* 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 () {
const getRequests = this._getRequests;
const getKeys = this._getKeys;
this._getRequests = [];
this._getKeys = [];
/**
* Flushes any pending requests using a single batch call to get the data.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#flushgetrequests
* */
flushGetRequests: () => {
const getRequests = _getRequests;
const getKeys = _getKeys;
_getRequests = [];
_getKeys = [];
_RCTAsyncStorage.default.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(_ref => {
let [key, value] = _ref;
map[key] = value;
return value;
});
const reqLength = getRequests.length;
/**
* As mentioned few lines above, this method could be called with the array of potential error,
* in case of anything goes wrong. The problem is, if any of the batched calls fails
* the rest of them would fail too, but the error would be consumed by just one. The rest
* would simply return `undefined` as their result, rendering false negatives.
*
* In order to avoid this situation, in case of any call failing,
* the rest of them will be rejected as well (with the same error).
*/
_RCTAsyncStorage.default.multiGet(getKeys, (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 === null || result === void 0 ? void 0 : result.forEach(_ref => {
let [key, value] = _ref;
map[key] = value;
return value;
});
const reqLength = getRequests.length;
/**
* As mentioned few lines above, this method could be called with the array of potential error,
* in case of anything goes wrong. The problem is, if any of the batched calls fails
* the rest of them would fail too, but the error would be consumed by just one. The rest
* would simply return `undefined` as their result, rendering false negatives.
*
* In order to avoid this situation, in case of any call failing,
* the rest of them will be rejected as well (with the same error).
*/
const errorList = convertErrors(errors);
const error = errorList && errorList.length ? errorList[0] : null;
const errorList = (0, _helpers.convertErrors)(errors);
const error = errorList !== null && errorList !== void 0 && errorList.length ? errorList[0] : null;
for (let i = 0; i < reqLength; i++) {
const request = getRequests[i];
for (let i = 0; i < reqLength; i++) {
var _request$callback2, _request$resolve;
if (error) {
request.callback && request.callback(error);
request.reject && request.reject(error);
continue;
const request = getRequests[i];
if (error) {
var _request$callback, _request$reject;
(_request$callback = request.callback) === null || _request$callback === void 0 ? void 0 : _request$callback.call(request, errorList);
(_request$reject = request.reject) === null || _request$reject === void 0 ? void 0 : _request$reject.call(request, error);
continue;
}
const requestResult = request.keys.map(key => [key, map[key]]);
(_request$callback2 = request.callback) === null || _request$callback2 === void 0 ? void 0 : _request$callback2.call(request, null, requestResult);
(_request$resolve = request.resolve) === null || _request$resolve === void 0 ? void 0 : _request$resolve.call(request, requestResult);
}
});
},
const requestResult = request.keys.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 https://react-native-async-storage.github.io/async-storage/docs/api#multiget
*/
multiGet: (keys, callback) => {
if (!_immediate) {
_immediate = setImmediate(() => {
_immediate = null;
AsyncStorage.flushGetRequests();
});
}
});
},
/**
* 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, callback) {
if (!this._immediate) {
this._immediate = setImmediate(() => {
this._immediate = null;
this.flushGetRequests();
const getRequest = {
keys: keys,
callback: callback,
// do we need this?
keyIndex: _getKeys.length,
resolve: null,
reject: null
};
const promiseResult = new Promise((resolve, reject) => {
getRequest.resolve = resolve;
getRequest.reject = reject;
});
}
const getRequest = {
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;
});
_getRequests.push(getRequest); // avoid fetching duplicates
this._getRequests.push(getRequest); // avoid fetching duplicates
keys.forEach(key => {
if (_getKeys.indexOf(key) === -1) {
_getKeys.push(key);
}
});
return promiseResult;
},
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 https://react-native-async-storage.github.io/async-storage/docs/api#multiset
*/
multiSet: (keyValuePairs, callback) => {
(0, _helpers.checkValidArgs)(keyValuePairs, callback);
return new Promise((resolve, reject) => {
keyValuePairs.forEach(_ref2 => {
let [key, value] = _ref2;
(0, _helpers.checkValidInput)(key, value);
});
/**
* 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, callback) {
checkValidArgs(keyValuePairs, callback);
return new Promise((resolve, reject) => {
keyValuePairs.forEach(_ref2 => {
let [key, value] = _ref2;
checkValidInput(key, value);
});
_RCTAsyncStorage.default.multiSet(keyValuePairs, errors => {
const error = (0, _helpers.convertErrors)(errors);
callback === null || callback === void 0 ? void 0 : callback(error);
_RCTAsyncStorage.default.multiSet(keyValuePairs, function (errors) {
const error = convertErrors(errors);
callback && callback(error);
if (error) {
reject(error);
} else {
resolve(null);
}
if (error) {
reject(error);
} else {
resolve();
}
});
});
});
},
},
/**
* Call this to batch the deletion of all keys in the `keys` array.
*
* See http://reactnative.dev/docs/asyncstorage.html#multiremove
*/
multiRemove: function (keys, callback) {
return new Promise((resolve, reject) => {
keys.forEach(key => checkValidInput(key));
/**
* Call this to batch the deletion of all keys in the `keys` array.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#multiremove
*/
multiRemove: (keys, callback) => {
return new Promise((resolve, reject) => {
keys.forEach(key => (0, _helpers.checkValidInput)(key));
_RCTAsyncStorage.default.multiRemove(keys, function (errors) {
const error = convertErrors(errors);
callback && callback(error);
_RCTAsyncStorage.default.multiRemove(keys, errors => {
const error = (0, _helpers.convertErrors)(errors);
callback === null || callback === void 0 ? void 0 : callback(error);
if (error) {
reject(error);
} else {
resolve(null);
}
if (error) {
reject(error);
} else {
resolve();
}
});
});
});
},
},
/**
* 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, callback) {
return new Promise((resolve, reject) => {
_RCTAsyncStorage.default.multiMerge(keyValuePairs, function (errors) {
const error = convertErrors(errors);
callback && callback(error);
/**
* 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 https://react-native-async-storage.github.io/async-storage/docs/api#multimerge
*/
multiMerge: (keyValuePairs, callback) => {
return new Promise((resolve, reject) => {
_RCTAsyncStorage.default.multiMerge(keyValuePairs, errors => {
const error = (0, _helpers.convertErrors)(errors);
callback === null || callback === void 0 ? void 0 : callback(error);
if (error) {
reject(error);
} else {
resolve(null);
}
if (error) {
reject(error);
} else {
resolve();
}
});
});
});
}
}; // Not all native implementations support merge.
}
};
})(); // Not all native implementations support merge.
if (!_RCTAsyncStorage.default.multiMerge) {
// $FlowFixMe
delete AsyncStorage.mergeItem; // $FlowFixMe
delete AsyncStorage.mergeItem;
delete AsyncStorage.multiMerge;
}
function convertErrors(errs) {
if (!errs || Array.isArray(errs) && errs.length === 0) {
return null;
}
return (Array.isArray(errs) ? errs : [errs]).map(e => convertError(e));
}
function convertError(error) {
if (!error) {
return null;
}
const out = new Error(error.message); // $FlowFixMe: adding custom properties to error.
out.key = error.key;
return out;
}
var _default = AsyncStorage;
exports.default = _default;
//# sourceMappingURL=AsyncStorage.native.js.map

@@ -12,6 +12,2 @@ "use strict";

/**
* @format
*
*/
function useAsyncStorage(key) {

@@ -34,2 +30,4 @@ return {

mergeItem: function () {
var _AsyncStorage$mergeIt, _AsyncStorage$mergeIt2;
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {

@@ -39,3 +37,3 @@ args[_key3] = arguments[_key3];

return _AsyncStorage.default.mergeItem(key, ...args);
return (_AsyncStorage$mergeIt = (_AsyncStorage$mergeIt2 = _AsyncStorage.default.mergeItem) === null || _AsyncStorage$mergeIt2 === void 0 ? void 0 : _AsyncStorage$mergeIt2.call(_AsyncStorage.default, key, ...args)) !== null && _AsyncStorage$mergeIt !== void 0 ? _AsyncStorage$mergeIt : Promise.reject('Not implemented');
},

@@ -42,0 +40,0 @@ removeItem: function () {

@@ -20,8 +20,5 @@ "use strict";

/**
* @format
*
*/
var _default = _AsyncStorage.default;
var _default = _AsyncStorage.default; // @ts-ignore AsyncStorage mock module
exports.default = _default;
//# sourceMappingURL=index.js.map

@@ -8,20 +8,18 @@ "use strict";

const {
NativeModules,
TurboModuleRegistry
} = require('react-native');
var _reactNative = require("react-native");
const shouldFallbackToLegacyNativeModule = require('./shouldFallbackToLegacyNativeModule');
var _shouldFallbackToLegacyNativeModule = require("./shouldFallbackToLegacyNativeModule");
let RCTAsyncStorage = NativeModules.PlatformLocalStorage || // Support for external modules, like react-native-windows
NativeModules.RNC_AsyncSQLiteDBStorage || NativeModules.RNCAsyncStorage;
// @ts-ignore Module '"react-native"' has no exported member 'TurboModuleRegistry'.
let RCTAsyncStorage = _reactNative.NativeModules['PlatformLocalStorage'] || // Support for external modules, like react-native-windows
_reactNative.NativeModules['RNC_AsyncSQLiteDBStorage'] || _reactNative.NativeModules['RNCAsyncStorage'];
if (!RCTAsyncStorage && shouldFallbackToLegacyNativeModule()) {
if (!RCTAsyncStorage && (0, _shouldFallbackToLegacyNativeModule.shouldFallbackToLegacyNativeModule)()) {
// TurboModuleRegistry falls back to NativeModules so we don't have to try go
// assign NativeModules' counterparts if TurboModuleRegistry would resolve
// with undefined.
if (TurboModuleRegistry) {
RCTAsyncStorage = TurboModuleRegistry.get('AsyncSQLiteDBStorage') || TurboModuleRegistry.get('AsyncLocalStorage');
if (_reactNative.TurboModuleRegistry) {
RCTAsyncStorage = _reactNative.TurboModuleRegistry.get('AsyncSQLiteDBStorage') || _reactNative.TurboModuleRegistry.get('AsyncLocalStorage');
} else {
RCTAsyncStorage = NativeModules.AsyncSQLiteDBStorage || NativeModules.AsyncLocalStorage;
RCTAsyncStorage = _reactNative.NativeModules['AsyncSQLiteDBStorage'] || _reactNative.NativeModules['AsyncLocalStorage'];
}

@@ -28,0 +26,0 @@ }

"use strict";
const {
NativeModules
} = require('react-native');
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.shouldFallbackToLegacyNativeModule = shouldFallbackToLegacyNativeModule;
module.exports = function shouldFallbackToLegacyNativeModule() {
var _reactNative = require("react-native");
function shouldFallbackToLegacyNativeModule() {
var _NativeModules$Native, _NativeModules$Native2;
const expoConstants = (_NativeModules$Native = NativeModules.NativeUnimoduleProxy) === null || _NativeModules$Native === void 0 ? void 0 : (_NativeModules$Native2 = _NativeModules$Native.modulesConstants) === null || _NativeModules$Native2 === void 0 ? void 0 : _NativeModules$Native2.ExponentConstants;
const expoConstants = (_NativeModules$Native = _reactNative.NativeModules['NativeUnimoduleProxy']) === null || _NativeModules$Native === void 0 ? void 0 : (_NativeModules$Native2 = _NativeModules$Native.modulesConstants) === null || _NativeModules$Native2 === void 0 ? void 0 : _NativeModules$Native2.ExponentConstants;

@@ -35,3 +38,3 @@ if (expoConstants) {

return false;
};
}
//# sourceMappingURL=shouldFallbackToLegacyNativeModule.js.map

@@ -7,5 +7,4 @@ /**

* LICENSE file in the root directory of this source tree.
*
*
*/
// @ts-ignore Cannot find module 'merge-options' or its corresponding type declarations
import mergeOptions from 'merge-options';

@@ -17,96 +16,81 @@ const merge = mergeOptions.bind({

const mergeLocalStorageItem = (key, value) => {
function mergeLocalStorageItem(key, value) {
const oldValue = window.localStorage.getItem(key);
const oldObject = JSON.parse(oldValue);
const newObject = JSON.parse(value);
const nextValue = JSON.stringify(merge(oldObject, newObject));
window.localStorage.setItem(key, nextValue);
};
const createPromise = (getValue, callback) => {
if (oldValue) {
const oldObject = JSON.parse(oldValue);
const newObject = JSON.parse(value);
const nextValue = JSON.stringify(merge(oldObject, newObject));
window.localStorage.setItem(key, nextValue);
} else {
window.localStorage.setItem(key, value);
}
}
function createPromise(getValue, callback) {
return new Promise((resolve, reject) => {
try {
const value = getValue();
if (callback) {
callback(null, value);
}
callback === null || callback === void 0 ? void 0 : callback(null, value);
resolve(value);
} catch (err) {
if (callback) {
callback(err);
}
callback === null || callback === void 0 ? void 0 : callback(err);
reject(err);
}
});
};
}
const createPromiseAll = (promises, callback, processResult) => {
function createPromiseAll(promises, callback, processResult) {
return Promise.all(promises).then(result => {
const value = processResult ? processResult(result) : null;
callback && callback(null, value);
var _processResult;
const value = (_processResult = processResult === null || processResult === void 0 ? void 0 : processResult(result)) !== null && _processResult !== void 0 ? _processResult : null;
callback === null || callback === void 0 ? void 0 : callback(null, value);
return Promise.resolve(value);
}, errors => {
callback && callback(errors);
callback === null || callback === void 0 ? void 0 : callback(errors);
return Promise.reject(errors);
});
};
}
export default class AsyncStorage {
const AsyncStorage = {
/**
* Fetches `key` value.
*/
static getItem(key, callback) {
return createPromise(() => {
return window.localStorage.getItem(key);
}, callback);
}
getItem: (key, callback) => {
return createPromise(() => window.localStorage.getItem(key), callback);
},
/**
* Sets `value` for `key`.
*/
setItem: (key, value, callback) => {
return createPromise(() => window.localStorage.setItem(key, value), callback);
},
static setItem(key, value, callback) {
return createPromise(() => {
window.localStorage.setItem(key, value);
}, callback);
}
/**
* Removes a `key`
*/
removeItem: (key, callback) => {
return createPromise(() => window.localStorage.removeItem(key), callback);
},
static removeItem(key, callback) {
return createPromise(() => {
return window.localStorage.removeItem(key);
}, callback);
}
/**
* Merges existing value with input value, assuming they are stringified JSON.
*/
mergeItem: (key, value, callback) => {
return createPromise(() => mergeLocalStorageItem(key, value), callback);
},
static mergeItem(key, value, callback) {
return createPromise(() => {
mergeLocalStorageItem(key, value);
}, callback);
}
/**
* Erases *all* AsyncStorage for the domain.
*/
clear: callback => {
return createPromise(() => window.localStorage.clear(), callback);
},
static clear(callback) {
return createPromise(() => {
window.localStorage.clear();
}, callback);
}
/**
* Gets *all* keys known to the app, for all callers, libraries, etc.
*/
static getAllKeys(callback) {
getAllKeys: callback => {
return createPromise(() => {

@@ -117,3 +101,3 @@ const numberOfKeys = window.localStorage.length;

for (let i = 0; i < numberOfKeys; i += 1) {
const key = window.localStorage.key(i);
const key = window.localStorage.key(i) || '';
keys.push(key);

@@ -124,9 +108,9 @@ }

}, callback);
}
},
/**
* (stub) Flushes any pending requests using a single batch call to get the data.
*/
flushGetRequests: () => undefined,
static flushGetRequests() {}
/**

@@ -138,5 +122,3 @@ * multiGet resolves to an array of key-value pair arrays that matches the

*/
static multiGet(keys, callback) {
multiGet: (keys, callback) => {
const promises = keys.map(key => AsyncStorage.getItem(key));

@@ -147,3 +129,4 @@

return createPromiseAll(promises, callback, processResult);
}
},
/**

@@ -153,17 +136,15 @@ * Takes an array of key-value array pairs.

*/
static multiSet(keyValuePairs, callback) {
multiSet: (keyValuePairs, callback) => {
const promises = keyValuePairs.map(item => AsyncStorage.setItem(item[0], item[1]));
return createPromiseAll(promises, callback);
}
},
/**
* Delete all the keys in the `keys` array.
*/
static multiRemove(keys, callback) {
multiRemove: (keys, callback) => {
const promises = keys.map(key => AsyncStorage.removeItem(key));
return createPromiseAll(promises, callback);
}
},
/**

@@ -175,10 +156,12 @@ * Takes an array of key-value array pairs and merges them with existing

*/
multiMerge: (keyValuePairs, callback) => {
const promises = keyValuePairs.map(item => {
var _AsyncStorage$mergeIt, _AsyncStorage$mergeIt2;
static multiMerge(keyValuePairs, callback) {
const promises = keyValuePairs.map(item => AsyncStorage.mergeItem(item[0], item[1]));
return (_AsyncStorage$mergeIt = (_AsyncStorage$mergeIt2 = AsyncStorage.mergeItem) === null || _AsyncStorage$mergeIt2 === void 0 ? void 0 : _AsyncStorage$mergeIt2.call(AsyncStorage, item[0], item[1])) !== null && _AsyncStorage$mergeIt !== void 0 ? _AsyncStorage$mergeIt : Promise.reject('Not implemented');
});
return createPromiseAll(promises, callback);
}
}
};
export default AsyncStorage;
//# sourceMappingURL=AsyncStorage.js.map

@@ -6,9 +6,4 @@ /**

* LICENSE file in the root directory of this source tree.
*
* @format
*
* @jsdoc
*/
'use strict';
import { checkValidArgs, checkValidInput, convertError, convertErrors } from './helpers';
import RCTAsyncStorage from './RCTAsyncStorage';

@@ -21,4 +16,2 @@

• Run \`react-native link @react-native-async-storage/async-storage\` in the project root.
• Rebuild and restart the app.

@@ -32,377 +25,334 @@

If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-async-storage/react-native-async-storage/issues
If none of these fix the issue, please open an issue on the Github repository: https://github.com/react-native-async-storage/async-storage/issues
`);
}
function checkValidInput(usedKey, value) {
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 .removeItem 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`);
}
}
}
function checkValidArgs(keyValuePairs, callback) {
if (!Array.isArray(keyValuePairs) || keyValuePairs.length === 0 || !Array.isArray(keyValuePairs[0])) {
throw new Error('[AsyncStorage] Expected array of key-value pairs as first argument to multiSet');
}
if (callback && typeof callback !== 'function') {
if (Array.isArray(callback)) {
throw new Error('[AsyncStorage] Expected function as second argument to multiSet. Did you forget to wrap key-value pairs in an array for the first argument?');
}
throw new Error('[AsyncStorage] Expected function as second argument to multiSet');
}
}
/**
* `AsyncStorage` is a simple, unencrypted, asynchronous, persistent, key-value
* storage system that is global to the app. It should be used instead of
* storage system that is global to the app. It should be used instead of
* LocalStorage.
*
* See http://reactnative.dev/docs/asyncstorage.html
* See https://react-native-async-storage.github.io/async-storage/docs/api
*/
const AsyncStorage = {
_getRequests: [],
_getKeys: [],
_immediate: null,
const AsyncStorage = (() => {
let _getRequests = [];
let _getKeys = [];
let _immediate = null;
return {
/**
* Fetches an item for a `key` and invokes a callback upon completion.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#getitem
*/
getItem: (key, callback) => {
return new Promise((resolve, reject) => {
checkValidInput(key);
RCTAsyncStorage.multiGet([key], (errors, result) => {
var _result$;
/**
* Fetches an item for a `key` and invokes a callback upon completion.
*
* See http://reactnative.dev/docs/asyncstorage.html#getitem
*/
getItem: function (key, callback) {
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);
// Unpack result to get value from [[key,value]]
const value = result !== null && result !== void 0 && (_result$ = result[0]) !== null && _result$ !== void 0 && _result$[1] ? result[0][1] : null;
const errs = convertErrors(errors);
callback === null || callback === void 0 ? void 0 : callback(errs === null || errs === void 0 ? void 0 : errs[0], value);
if (errs) {
reject(errs[0]);
} else {
resolve(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, value, callback) {
return new Promise((resolve, reject) => {
checkValidInput(key, value);
RCTAsyncStorage.multiSet([[key, value]], function (errors) {
const errs = convertErrors(errors);
callback && callback(errs && errs[0]);
/**
* Sets the value for a `key` and invokes a callback upon completion.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#setitem
*/
setItem: (key, value, callback) => {
return new Promise((resolve, reject) => {
checkValidInput(key, value);
RCTAsyncStorage.multiSet([[key, value]], errors => {
const errs = convertErrors(errors);
callback === null || callback === void 0 ? void 0 : callback(errs === null || errs === void 0 ? void 0 : errs[0]);
if (errs) {
reject(errs[0]);
} else {
resolve(null);
}
if (errs) {
reject(errs[0]);
} else {
resolve();
}
});
});
});
},
},
/**
* Removes an item for a `key` and invokes a callback upon completion.
*
* See http://reactnative.dev/docs/asyncstorage.html#removeitem
*/
removeItem: function (key, callback) {
return new Promise((resolve, reject) => {
checkValidInput(key);
RCTAsyncStorage.multiRemove([key], function (errors) {
const errs = convertErrors(errors);
callback && callback(errs && errs[0]);
/**
* Removes an item for a `key` and invokes a callback upon completion.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#removeitem
*/
removeItem: (key, callback) => {
return new Promise((resolve, reject) => {
checkValidInput(key);
RCTAsyncStorage.multiRemove([key], errors => {
const errs = convertErrors(errors);
callback === null || callback === void 0 ? void 0 : callback(errs === null || errs === void 0 ? void 0 : errs[0]);
if (errs) {
reject(errs[0]);
} else {
resolve(null);
}
if (errs) {
reject(errs[0]);
} else {
resolve();
}
});
});
});
},
},
/**
* 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, value, callback) {
return new Promise((resolve, reject) => {
checkValidInput(key, value);
RCTAsyncStorage.multiMerge([[key, value]], function (errors) {
const errs = convertErrors(errors);
callback && callback(errs && errs[0]);
/**
* 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 https://react-native-async-storage.github.io/async-storage/docs/api#mergeitem
*/
mergeItem: (key, value, callback) => {
return new Promise((resolve, reject) => {
checkValidInput(key, value);
RCTAsyncStorage.multiMerge([[key, value]], errors => {
const errs = convertErrors(errors);
callback === null || callback === void 0 ? void 0 : callback(errs === null || errs === void 0 ? void 0 : errs[0]);
if (errs) {
reject(errs[0]);
} else {
resolve(null);
}
if (errs) {
reject(errs[0]);
} else {
resolve();
}
});
});
});
},
},
/**
* 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) {
return new Promise((resolve, reject) => {
RCTAsyncStorage.clear(function (error) {
const err = convertError(error);
callback && callback(err);
/**
* 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 https://react-native-async-storage.github.io/async-storage/docs/api#clear
*/
clear: callback => {
return new Promise((resolve, reject) => {
RCTAsyncStorage.clear(error => {
const err = convertError(error);
callback === null || callback === void 0 ? void 0 : callback(err);
if (err) {
reject(err);
} else {
resolve(null);
}
if (err) {
reject(err);
} else {
resolve();
}
});
});
});
},
},
/**
* Gets *all* keys known to your app; for all callers, libraries, etc.
*
* See http://reactnative.dev/docs/asyncstorage.html#getallkeys
*/
getAllKeys: function (callback) {
return new Promise((resolve, reject) => {
RCTAsyncStorage.getAllKeys(function (error, keys) {
const err = convertError(error);
callback && callback(err, keys);
/**
* Gets *all* keys known to your app; for all callers, libraries, etc.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#getallkeys
*/
getAllKeys: callback => {
return new Promise((resolve, reject) => {
RCTAsyncStorage.getAllKeys((error, keys) => {
const err = convertError(error);
callback === null || callback === void 0 ? void 0 : callback(err, keys);
if (err) {
reject(err);
} else {
resolve(keys);
}
if (keys) {
resolve(keys);
} else {
reject(err);
}
});
});
});
},
},
/**
* 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.
*/
/**
* 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 () {
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(_ref => {
let [key, value] = _ref;
map[key] = value;
return value;
});
const reqLength = getRequests.length;
/**
* As mentioned few lines above, this method could be called with the array of potential error,
* in case of anything goes wrong. The problem is, if any of the batched calls fails
* the rest of them would fail too, but the error would be consumed by just one. The rest
* would simply return `undefined` as their result, rendering false negatives.
*
* In order to avoid this situation, in case of any call failing,
* the rest of them will be rejected as well (with the same error).
*/
/**
* Flushes any pending requests using a single batch call to get the data.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#flushgetrequests
* */
flushGetRequests: () => {
const getRequests = _getRequests;
const getKeys = _getKeys;
_getRequests = [];
_getKeys = [];
RCTAsyncStorage.multiGet(getKeys, (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 === null || result === void 0 ? void 0 : result.forEach(_ref => {
let [key, value] = _ref;
map[key] = value;
return value;
});
const reqLength = getRequests.length;
/**
* As mentioned few lines above, this method could be called with the array of potential error,
* in case of anything goes wrong. The problem is, if any of the batched calls fails
* the rest of them would fail too, but the error would be consumed by just one. The rest
* would simply return `undefined` as their result, rendering false negatives.
*
* In order to avoid this situation, in case of any call failing,
* the rest of them will be rejected as well (with the same error).
*/
const errorList = convertErrors(errors);
const error = errorList && errorList.length ? errorList[0] : null;
const errorList = convertErrors(errors);
const error = errorList !== null && errorList !== void 0 && errorList.length ? errorList[0] : null;
for (let i = 0; i < reqLength; i++) {
const request = getRequests[i];
for (let i = 0; i < reqLength; i++) {
var _request$callback2, _request$resolve;
if (error) {
request.callback && request.callback(error);
request.reject && request.reject(error);
continue;
}
const request = getRequests[i];
const requestResult = request.keys.map(key => [key, map[key]]);
request.callback && request.callback(null, requestResult);
request.resolve && request.resolve(requestResult);
}
});
},
if (error) {
var _request$callback, _request$reject;
/**
* 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, callback) {
if (!this._immediate) {
this._immediate = setImmediate(() => {
this._immediate = null;
this.flushGetRequests();
(_request$callback = request.callback) === null || _request$callback === void 0 ? void 0 : _request$callback.call(request, errorList);
(_request$reject = request.reject) === null || _request$reject === void 0 ? void 0 : _request$reject.call(request, error);
continue;
}
const requestResult = request.keys.map(key => [key, map[key]]);
(_request$callback2 = request.callback) === null || _request$callback2 === void 0 ? void 0 : _request$callback2.call(request, null, requestResult);
(_request$resolve = request.resolve) === null || _request$resolve === void 0 ? void 0 : _request$resolve.call(request, requestResult);
}
});
}
},
const getRequest = {
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);
/**
* 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 https://react-native-async-storage.github.io/async-storage/docs/api#multiget
*/
multiGet: (keys, callback) => {
if (!_immediate) {
_immediate = setImmediate(() => {
_immediate = null;
AsyncStorage.flushGetRequests();
});
}
});
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, callback) {
checkValidArgs(keyValuePairs, callback);
return new Promise((resolve, reject) => {
keyValuePairs.forEach(_ref2 => {
let [key, value] = _ref2;
checkValidInput(key, value);
const getRequest = {
keys: keys,
callback: callback,
// do we need this?
keyIndex: _getKeys.length,
resolve: null,
reject: null
};
const promiseResult = new Promise((resolve, reject) => {
getRequest.resolve = resolve;
getRequest.reject = reject;
});
RCTAsyncStorage.multiSet(keyValuePairs, function (errors) {
const error = convertErrors(errors);
callback && callback(error);
if (error) {
reject(error);
} else {
resolve(null);
}
});
});
},
_getRequests.push(getRequest); // avoid fetching duplicates
/**
* Call this to batch the deletion of all keys in the `keys` array.
*
* See http://reactnative.dev/docs/asyncstorage.html#multiremove
*/
multiRemove: function (keys, callback) {
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);
keys.forEach(key => {
if (_getKeys.indexOf(key) === -1) {
_getKeys.push(key);
}
});
});
},
return promiseResult;
},
/**
* 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, callback) {
return new Promise((resolve, reject) => {
RCTAsyncStorage.multiMerge(keyValuePairs, function (errors) {
const error = convertErrors(errors);
callback && callback(error);
/**
* 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 https://react-native-async-storage.github.io/async-storage/docs/api#multiset
*/
multiSet: (keyValuePairs, callback) => {
checkValidArgs(keyValuePairs, callback);
return new Promise((resolve, reject) => {
keyValuePairs.forEach(_ref2 => {
let [key, value] = _ref2;
checkValidInput(key, value);
});
RCTAsyncStorage.multiSet(keyValuePairs, errors => {
const error = convertErrors(errors);
callback === null || callback === void 0 ? void 0 : callback(error);
if (error) {
reject(error);
} else {
resolve(null);
}
if (error) {
reject(error);
} else {
resolve();
}
});
});
});
}
}; // Not all native implementations support merge.
},
if (!RCTAsyncStorage.multiMerge) {
// $FlowFixMe
delete AsyncStorage.mergeItem; // $FlowFixMe
/**
* Call this to batch the deletion of all keys in the `keys` array.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#multiremove
*/
multiRemove: (keys, callback) => {
return new Promise((resolve, reject) => {
keys.forEach(key => checkValidInput(key));
RCTAsyncStorage.multiRemove(keys, errors => {
const error = convertErrors(errors);
callback === null || callback === void 0 ? void 0 : callback(error);
delete AsyncStorage.multiMerge;
}
if (error) {
reject(error);
} else {
resolve();
}
});
});
},
function convertErrors(errs) {
if (!errs || Array.isArray(errs) && errs.length === 0) {
return 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 https://react-native-async-storage.github.io/async-storage/docs/api#multimerge
*/
multiMerge: (keyValuePairs, callback) => {
return new Promise((resolve, reject) => {
RCTAsyncStorage.multiMerge(keyValuePairs, errors => {
const error = convertErrors(errors);
callback === null || callback === void 0 ? void 0 : callback(error);
return (Array.isArray(errs) ? errs : [errs]).map(e => convertError(e));
}
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
};
})(); // Not all native implementations support merge.
function convertError(error) {
if (!error) {
return null;
}
const out = new Error(error.message); // $FlowFixMe: adding custom properties to error.
out.key = error.key;
return out;
if (!RCTAsyncStorage.multiMerge) {
delete AsyncStorage.mergeItem;
delete AsyncStorage.multiMerge;
}

@@ -409,0 +359,0 @@

@@ -1,5 +0,1 @@

/**
* @format
*
*/
import AsyncStorage from './AsyncStorage';

@@ -23,2 +19,4 @@ export function useAsyncStorage(key) {

mergeItem: function () {
var _AsyncStorage$mergeIt, _AsyncStorage$mergeIt2;
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {

@@ -28,3 +26,3 @@ args[_key3] = arguments[_key3];

return AsyncStorage.mergeItem(key, ...args);
return (_AsyncStorage$mergeIt = (_AsyncStorage$mergeIt2 = AsyncStorage.mergeItem) === null || _AsyncStorage$mergeIt2 === void 0 ? void 0 : _AsyncStorage$mergeIt2.call(AsyncStorage, key, ...args)) !== null && _AsyncStorage$mergeIt !== void 0 ? _AsyncStorage$mergeIt : Promise.reject('Not implemented');
},

@@ -31,0 +29,0 @@ removeItem: function () {

@@ -1,8 +0,4 @@

/**
* @format
*
*/
import AsyncStorage from './AsyncStorage';
export default AsyncStorage;
export { useAsyncStorage } from './hooks';
export default AsyncStorage; // @ts-ignore AsyncStorage mock module
//# sourceMappingURL=index.js.map

@@ -1,11 +0,7 @@

const {
NativeModules,
TurboModuleRegistry
} = require('react-native');
// @ts-ignore Module '"react-native"' has no exported member 'TurboModuleRegistry'.
import { NativeModules, TurboModuleRegistry } from 'react-native';
import { shouldFallbackToLegacyNativeModule } from './shouldFallbackToLegacyNativeModule';
let RCTAsyncStorage = NativeModules['PlatformLocalStorage'] || // Support for external modules, like react-native-windows
NativeModules['RNC_AsyncSQLiteDBStorage'] || NativeModules['RNCAsyncStorage'];
const shouldFallbackToLegacyNativeModule = require('./shouldFallbackToLegacyNativeModule');
let RCTAsyncStorage = NativeModules.PlatformLocalStorage || // Support for external modules, like react-native-windows
NativeModules.RNC_AsyncSQLiteDBStorage || NativeModules.RNCAsyncStorage;
if (!RCTAsyncStorage && shouldFallbackToLegacyNativeModule()) {

@@ -18,3 +14,3 @@ // TurboModuleRegistry falls back to NativeModules so we don't have to try go

} else {
RCTAsyncStorage = NativeModules.AsyncSQLiteDBStorage || NativeModules.AsyncLocalStorage;
RCTAsyncStorage = NativeModules['AsyncSQLiteDBStorage'] || NativeModules['AsyncLocalStorage'];
}

@@ -21,0 +17,0 @@ }

@@ -1,9 +0,6 @@

const {
NativeModules
} = require('react-native');
module.exports = function shouldFallbackToLegacyNativeModule() {
import { NativeModules } from 'react-native';
export function shouldFallbackToLegacyNativeModule() {
var _NativeModules$Native, _NativeModules$Native2;
const expoConstants = (_NativeModules$Native = NativeModules.NativeUnimoduleProxy) === null || _NativeModules$Native === void 0 ? void 0 : (_NativeModules$Native2 = _NativeModules$Native.modulesConstants) === null || _NativeModules$Native2 === void 0 ? void 0 : _NativeModules$Native2.ExponentConstants;
const expoConstants = (_NativeModules$Native = NativeModules['NativeUnimoduleProxy']) === null || _NativeModules$Native === void 0 ? void 0 : (_NativeModules$Native2 = _NativeModules$Native.modulesConstants) === null || _NativeModules$Native2 === void 0 ? void 0 : _NativeModules$Native2.ExponentConstants;

@@ -33,3 +30,3 @@ if (expoConstants) {

return false;
};
}
//# sourceMappingURL=shouldFallbackToLegacyNativeModule.js.map
{
"name": "@react-native-async-storage/async-storage",
"version": "1.15.17",
"version": "1.16.0",
"description": "Asynchronous, persistent, key-value storage system for React Native.",
"main": "lib/commonjs/index.js",
"module": "lib/module/index.js",
"react-native": "src/index.js",
"types": "./types/index.d.ts",
"react-native": "src/index.ts",
"types": "lib/typescript/index.d.ts",
"files": [
"lib/",
"src/",
"jest/",
"types/",
"RNCAsyncStorage.podspec",
"android/build.gradle",
"android/src",
"android/build.gradle",
"android/testresults.gradle",
"ios/",
"jest/",
"lib/commonjs/**/*.js",
"lib/module/**/*.js",
"lib/typescript/",
"macos/",
"windows/",
"RNCAsyncStorage.podspec"
"src/",
"windows/"
],
"author": "Krzysztof Borowy <hello@krizzu.dev>",
"contributors": [
"Evan Bacon <bacon@expo.io> (https://github.com/evanbacon)"
"Evan Bacon <bacon@expo.io> (https://github.com/evanbacon)",
"Tommy Nguyen <4123478+tido64@users.noreply.github.com> (https://github.com/tido64)"
],

@@ -43,3 +45,3 @@ "homepage": "https://github.com/react-native-async-storage/async-storage#readme",

"format:c": "clang-format -i $(git ls-files '*.cpp' '*.h' '*.m' '*.mm')",
"format:js": "prettier --write $(git ls-files '*.js' '*.json' '*.yml')",
"format:js": "prettier --write $(git ls-files '*.js' '*.json' '*.md' '*.ts' '*.tsx' '*.yml')",
"prepare": "bob build",

@@ -58,6 +60,5 @@ "start": "react-native start",

"bundle:macos": "react-native bundle --entry-file index.ts --platform macos --bundle-output example/index.macos.jsbundle",
"test": "concurrently -n flow,lint,ts yarn:test:flow yarn:test:lint yarn:test:ts",
"test:flow": "flow check",
"test:lint": "eslint src/**/*.js example/**/*.js jest/*.js",
"test:ts": "tsc",
"test": "concurrently -n lint,ts yarn:test:lint yarn:test:ts",
"test:lint": "eslint src/**/*.ts example/**/*.ts jest/*.js",
"test:ts": "tsc --project tsconfig.all.json",
"test:e2e:android": "detox test -c android.emu.release --maxConcurrency 1",

@@ -86,3 +87,2 @@ "test:e2e:ios": "detox test -c ios.sim.release --maxConcurrency 1",

"expo": "^43.0.0",
"flow-bin": "^0.137.0",
"jest": "^26.5.3",

@@ -137,2 +137,23 @@ "jest-circus": "^26.6.1",

},
"eslintConfig": {
"extends": [
"@react-native-community",
"plugin:jest/recommended"
],
"rules": {
"dot-notation": "off"
}
},
"prettier": {
"endOfLine": "auto",
"singleQuote": true,
"overrides": [
{
"files": "*.md",
"options": {
"proseWrap": "always"
}
}
]
},
"react-native-builder-bob": {

@@ -142,11 +163,29 @@ "source": "src",

"targets": [
"commonjs",
"module",
"typescript"
]
},
"release": {
"branches": [
"master"
],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
[
"commonjs",
"@semantic-release/git",
{
"copyFlow": true
"assets": [
"CHANGELOG.md",
"package.json"
],
"message": "chore(release): ${nextRelease.version} [skip ci]"
}
],
"module"
]
]
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc