import 'regenerator-runtime/runtime' ; import Storage , { NotFoundError } from 'react-native-storage' ; import _isNil from 'lodash/isNil' ; import _isUndefined from 'lodash/isUndefined' ; /**
 * 构建一个存储对象
 */ export function buildStorage ( backend ) { const storage = new Storage ( { size : 1000 , storageBackend : backend , defaultExpires : 1000 * 3600 * 24 , enableCache : true } ) ; const rnStorage = { set : async ( key , data ) => { try { if ( ! ! key && typeof key === 'string' && ! _isUndefined ( data ) ) { await storage . save ( { key , data } ) ; } } catch ( e ) { console . error ( e ) ; } return data ; } , /**
         * 自定义过期时间的存储
         * set默认为1天，该方法自定义过期时间
         */ setWithExpires : async ( key , data , expires ) => { try { if ( ! ! key && typeof key === 'string' && ! _isUndefined ( data ) ) { await storage . save ( { key , data , expires } ) ; } } catch ( e ) { console . error ( e ) ; } return data ; } , get : async ( key , defaultVal ? ) => { let res ; try { res = await storage . load ( { key , autoSync : true , syncInBackground : false } ) ; } catch ( e ) { if ( ! ( e instanceof NotFoundError ) ) { console . log ( `get key ${ key } error:` , String ( e ) ) ; } res = _isNil ( defaultVal ) ? null : defaultVal ; } return res ; } , remove : async ( key ) => { await storage . remove ( { key } ) ; } , /**
         * 持久化存储, 永不过期
         */ save : async ( key , data ) => { try { if ( ! ! key && typeof key === 'string' && ! _isUndefined ( data ) ) { await storage . save ( { key , data , expires : null } ) ; } } catch ( e ) { console . error ( e ) ; } return data ! ; } } ; return rnStorage ; }