import IStorage from 'common/abstractions/services/storage' ; import { AsyncStorage } from 'react-native' ; const StorageAsync = { getValue ( key ) { return AsyncStorage . getItem ( key ) ; } , setValue ( key , value ) { if ( ! key ) { throw new Error ( '[StorageAsync] key must be not empty! got:' + key ) ; } if ( ! value ) { throw new Error ( '[StorageAsync] value must be not null! use \'remove\' instead. Got: ' + `${ key } | ${ value }` ) ; } return AsyncStorage . setItem ( key , value ) ; } , async hasValue ( key ) { try { const value = await StorageAsync . getValue ( key ) ; return ! ! value ; } catch ( err ) { return false ; } } , remove ( key ) { return AsyncStorage . removeItem ( key ) ; } , } ; export class StorageEntryAsync { private readonly _key : string ; private _cachedValue : string ; private _disableCache : boolean ; constructor ( key , disableCache ? ) { this . _key = key ; this . _disableCache = disableCache ; } getAsync ( ) { if ( ! this . _disableCache && this . _cachedValue ) { return Promise . resolve ( this . _cachedValue ) ; } return StorageAsync . getValue ( this . _key ) ; } async setAsync ( val ) { try { this . _cachedValue = val ; await StorageAsync . setValue ( this . _key , val ) ; return true ; } catch ( err ) { return false ; } } } export default StorageAsync ;