[RN] Simplify initialization of AsyncStorage

This commit is contained in:
Saúl Ibarra Corretgé 2018-01-16 16:21:20 +01:00 committed by Paweł Domas
parent b04661b40b
commit 98ff20a026
2 changed files with 10 additions and 25 deletions

View File

@ -105,14 +105,11 @@ export class AbstractApp extends Component {
* properly initializes. On web it does actually nothing, see * properly initializes. On web it does actually nothing, see
* {@link #_initStorage}. * {@link #_initStorage}.
*/ */
this.init = new Promise(resolve => { this.init = this._initStorage().then(() => {
this._initStorage().then(() => {
this.setState({ this.setState({
route: undefined, route: undefined,
store: this._maybeCreateStore(props) store: this._maybeCreateStore(props)
}); });
resolve();
});
}); });
} }
@ -249,13 +246,11 @@ export class AbstractApp extends Component {
* @returns {ReactElement} * @returns {ReactElement}
*/ */
_initStorage() { _initStorage() {
return new Promise(resolve => { if (typeof window.localStorage._initialized !== 'undefined') {
if (window.localStorage._initializing) { return window.localStorage._initialized;
window.localStorage._inited.then(resolve);
} else {
resolve();
} }
});
return Promise.resolve();
} }
/** /**

View File

@ -34,16 +34,7 @@ export default class Storage {
// Load all previously persisted data items from React Native's // Load all previously persisted data items from React Native's
// AsyncStorage. // AsyncStorage.
/** this._initialized = new Promise(resolve => {
* A flag to indicate that the async {@code AsyncStorage} is not
* initialized yet. This is native specific but it will work
* fine on web as well, as it will have no value (== false) there.
* This is required to be available as we need a sync way to check
* if the storage is inited or not.
*/
this._initializing = true;
this._inited = new Promise(resolve => {
AsyncStorage.getAllKeys().then((...getAllKeysCallbackArgs) => { AsyncStorage.getAllKeys().then((...getAllKeysCallbackArgs) => {
// XXX The keys argument of getAllKeys' callback may // XXX The keys argument of getAllKeys' callback may
// or may not be preceded by an error argument. // or may not be preceded by an error argument.
@ -77,7 +68,6 @@ export default class Storage {
} }
} }
this._initializing = false;
resolve(); resolve();
}); });
}); });