2019-05-08 12:51:14 +00:00
|
|
|
import AsyncStorage from '@react-native-community/async-storage';
|
2017-09-05 18:56:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A Web Sorage API implementation used for polyfilling
|
2017-10-01 06:35:19 +00:00
|
|
|
* {@code window.localStorage} and/or {@code window.sessionStorage}.
|
2017-09-05 18:56:33 +00:00
|
|
|
* <p>
|
|
|
|
* The Web Storage API is synchronous whereas React Native's builtin generic
|
2017-10-01 06:35:19 +00:00
|
|
|
* storage API {@code AsyncStorage} is asynchronous so the implementation with
|
2017-09-05 18:56:33 +00:00
|
|
|
* persistence is optimistic: it will first store the value locally in memory so
|
|
|
|
* that results can be served synchronously and then persist the value
|
|
|
|
* asynchronously. If an asynchronous operation produces an error, it's ignored.
|
|
|
|
*/
|
|
|
|
export default class Storage {
|
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* Initializes a new {@code Storage} instance. Loads all previously
|
|
|
|
* persisted data items from React Native's {@code AsyncStorage} if
|
2017-09-05 18:56:33 +00:00
|
|
|
* necessary.
|
|
|
|
*
|
|
|
|
* @param {string|undefined} keyPrefix - The prefix of the
|
2017-10-01 06:35:19 +00:00
|
|
|
* {@code AsyncStorage} keys to be persisted by this storage.
|
2017-09-05 18:56:33 +00:00
|
|
|
*/
|
2017-09-05 23:55:54 +00:00
|
|
|
constructor(keyPrefix) {
|
2017-09-05 18:56:33 +00:00
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* The prefix of the {@code AsyncStorage} keys persisted by this
|
|
|
|
* storage. If {@code undefined}, then the data items stored in this
|
2017-09-05 18:56:33 +00:00
|
|
|
* storage will not be persisted.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this._keyPrefix = keyPrefix;
|
|
|
|
|
2018-02-05 21:26:01 +00:00
|
|
|
// Perform optional asynchronous initialization.
|
|
|
|
const initializing = this._initializeAsync();
|
|
|
|
|
|
|
|
if (initializing) {
|
|
|
|
// Indicate that asynchronous initialization is under way.
|
|
|
|
this._initializing = initializing;
|
|
|
|
|
|
|
|
// When the asynchronous initialization completes, indicate its
|
|
|
|
// completion.
|
|
|
|
initializing.finally(() => {
|
|
|
|
if (this._initializing === initializing) {
|
|
|
|
this._initializing = undefined;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all keys from this storage.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
clear() {
|
|
|
|
for (const key of Object.keys(this)) {
|
|
|
|
this.removeItem(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value associated with a specific key in this storage.
|
|
|
|
*
|
|
|
|
* @param {string} key - The name of the key to retrieve the value of.
|
|
|
|
* @returns {string|null} The value associated with {@code key} or
|
|
|
|
* {@code null}.
|
|
|
|
*/
|
|
|
|
getItem(key) {
|
|
|
|
return this.hasOwnProperty(key) ? this[key] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the value associated with a specific key in this {@code Storage}
|
|
|
|
* in an async manner. The method is required for the cases where we need
|
|
|
|
* the stored data but we're not sure yet whether this {@code Storage} is
|
2018-11-08 12:25:02 +00:00
|
|
|
* already initialized (e.g. On app start).
|
2018-02-05 21:26:01 +00:00
|
|
|
*
|
|
|
|
* @param {string} key - The name of the key to retrieve the value of.
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
_getItemAsync(key) {
|
|
|
|
return (
|
|
|
|
(this._initializing || Promise.resolve())
|
|
|
|
.catch(() => { /* _getItemAsync should always resolve! */ })
|
|
|
|
.then(() => this.getItem(key)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs asynchronous initialization of this {@code Storage} instance
|
|
|
|
* such as loading all keys from {@link AsyncStorage}.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
_initializeAsync() {
|
2017-09-05 18:56:33 +00:00
|
|
|
if (typeof this._keyPrefix !== 'undefined') {
|
|
|
|
// Load all previously persisted data items from React Native's
|
|
|
|
// AsyncStorage.
|
|
|
|
|
2018-02-05 21:26:01 +00:00
|
|
|
return new Promise(resolve => {
|
2017-12-14 17:02:32 +00:00
|
|
|
AsyncStorage.getAllKeys().then((...getAllKeysCallbackArgs) => {
|
2018-02-05 21:26:01 +00:00
|
|
|
// XXX The keys argument of getAllKeys' callback may or may
|
|
|
|
// not be preceded by an error argument.
|
2017-12-14 17:02:32 +00:00
|
|
|
const keys
|
|
|
|
= getAllKeysCallbackArgs[
|
|
|
|
getAllKeysCallbackArgs.length - 1
|
|
|
|
].filter(key => key.startsWith(this._keyPrefix));
|
2017-09-05 18:56:33 +00:00
|
|
|
|
2017-12-14 17:02:32 +00:00
|
|
|
AsyncStorage.multiGet(keys)
|
|
|
|
.then((...multiGetCallbackArgs) => {
|
|
|
|
// XXX The result argument of multiGet may or may not be
|
|
|
|
// preceded by an errors argument.
|
|
|
|
const result
|
|
|
|
= multiGetCallbackArgs[
|
|
|
|
multiGetCallbackArgs.length - 1
|
|
|
|
];
|
|
|
|
const keyPrefixLength
|
|
|
|
= this._keyPrefix && this._keyPrefix.length;
|
|
|
|
|
|
|
|
// eslint-disable-next-line prefer-const
|
|
|
|
for (let [ key, value ] of result) {
|
|
|
|
key = key.substring(keyPrefixLength);
|
|
|
|
|
|
|
|
// XXX The loading of the previously persisted data
|
|
|
|
// items from AsyncStorage is asynchronous which
|
|
|
|
// means that it is technically possible to invoke
|
|
|
|
// setItem with a key before the key is loaded from
|
|
|
|
// AsyncStorage.
|
|
|
|
if (!this.hasOwnProperty(key)) {
|
|
|
|
this[key] = value;
|
|
|
|
}
|
2017-09-05 18:56:33 +00:00
|
|
|
}
|
2017-12-14 17:02:32 +00:00
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
2017-09-05 18:56:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-05 21:26:01 +00:00
|
|
|
return undefined;
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 18:56:33 +00:00
|
|
|
/**
|
|
|
|
* Returns the name of the nth key in this storage.
|
|
|
|
*
|
|
|
|
* @param {number} n - The zero-based integer index of the key to get the
|
|
|
|
* name of.
|
|
|
|
* @returns {string} The name of the nth key in this storage.
|
|
|
|
*/
|
2017-09-05 23:55:54 +00:00
|
|
|
key(n) {
|
|
|
|
const keys = Object.keys(this);
|
2017-09-05 18:56:33 +00:00
|
|
|
|
2017-09-05 23:55:54 +00:00
|
|
|
return n < keys.length ? keys[n] : null;
|
2017-09-05 18:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an integer representing the number of data items stored in this
|
|
|
|
* storage.
|
|
|
|
*
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2017-09-05 23:55:54 +00:00
|
|
|
get length() {
|
|
|
|
return Object.keys(this).length;
|
2017-09-05 18:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a specific key from this storage.
|
|
|
|
*
|
|
|
|
* @param {string} key - The name of the key to remove.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-09-05 23:55:54 +00:00
|
|
|
removeItem(key) {
|
|
|
|
delete this[key];
|
2017-09-05 18:56:33 +00:00
|
|
|
typeof this._keyPrefix === 'undefined'
|
|
|
|
|| AsyncStorage.removeItem(`${String(this._keyPrefix)}${key}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a specific key to this storage and associates it with a specific
|
|
|
|
* value. If the key exists already, updates its value.
|
|
|
|
*
|
|
|
|
* @param {string} key - The name of the key to add/update.
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {string} value - The value to associate with {@code key}.
|
2017-09-05 18:56:33 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-09-05 23:55:54 +00:00
|
|
|
setItem(key, value) {
|
2017-09-05 18:56:33 +00:00
|
|
|
value = String(value); // eslint-disable-line no-param-reassign
|
2017-09-05 23:55:54 +00:00
|
|
|
this[key] = value;
|
2017-09-05 18:56:33 +00:00
|
|
|
typeof this._keyPrefix === 'undefined'
|
|
|
|
|| AsyncStorage.setItem(`${String(this._keyPrefix)}${key}`, value);
|
|
|
|
}
|
|
|
|
}
|