2017-09-05 18:56:33 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
if (typeof this._keyPrefix !== 'undefined') {
|
|
|
|
// Load all previously persisted data items from React Native's
|
|
|
|
// AsyncStorage.
|
|
|
|
AsyncStorage.getAllKeys().then((...getAllKeysCallbackArgs) => {
|
|
|
|
// XXX The keys argument of getAllKeys' callback may or may not
|
|
|
|
// be preceded by an error argument.
|
|
|
|
const keys
|
|
|
|
= getAllKeysCallbackArgs[getAllKeysCallbackArgs.length - 1]
|
|
|
|
.filter(key => key.startsWith(this._keyPrefix));
|
|
|
|
|
|
|
|
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.
|
2017-09-05 23:55:54 +00:00
|
|
|
if (!this.hasOwnProperty(key)) {
|
|
|
|
this[key] = value;
|
2017-09-05 18:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all keys from this storage.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
clear() {
|
2017-09-05 23:55:54 +00:00
|
|
|
for (const key of Object.keys(this)) {
|
2017-09-05 18:56:33 +00:00
|
|
|
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.
|
2017-10-01 06:35:19 +00:00
|
|
|
* @returns {string|null} The value associated with {@code key} or
|
|
|
|
* {@code null}.
|
2017-09-05 18:56:33 +00:00
|
|
|
*/
|
2017-09-05 23:55:54 +00:00
|
|
|
getItem(key) {
|
|
|
|
return this.hasOwnProperty(key) ? this[key] : null;
|
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);
|
|
|
|
}
|
|
|
|
}
|