UNPKG

react-native-storage

Version:

A local storage wrapper for both react-native(AsyncStorage) and browser(localStorage).

3 lines (2 loc) 10.4 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).storage={})}(this,function(e){"use strict";function t(e,t,n,r,i,a,s){try{var o=e[a](s),c=o.value}catch(e){return void n(e)}o.done?t(c):Promise.resolve(c).then(r,i)}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function a(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){i(e,t,n[t])})}return e}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&c(e,t)}function o(e){return(o=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function c(e,t){return(c=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function u(e,t,n){return(u=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(e){return!1}}()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var i=new(Function.bind.apply(e,r));return n&&c(i,n.prototype),i}).apply(null,arguments)}function h(e){var t="function"==typeof Map?new Map:void 0;return(h=function(e){if(null===e||(n=e,-1===Function.toString.call(n).indexOf("[native code]")))return e;var n;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return u(e,arguments,o(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),c(r,e)})(e)}function l(e,t){return!t||"object"!=typeof t&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}var f=function(e){function t(e){var r;return n(this,t),(r=l(this,o(t).call(this,"Not Found! Params: ".concat(e)))).name="NotFoundError",r.stack=(new Error).stack,r}return s(t,h(Error)),t}(),y=function(e){function t(e){var r;return n(this,t),(r=l(this,o(t).call(this,"Expired! Params: ".concat(e)))).name="ExpiredError",r.stack=(new Error).stack,r}return s(t,h(Error)),t}(),d=function(){function e(){var t=this,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(n(this,e),this._SIZE=r.size||1e3,this.sync=r.sync||{},this.defaultExpires=void 0!==r.defaultExpires?r.defaultExpires:864e5,this.enableCache=!1!==r.enableCache,this._s=r.storageBackend||null,this._innerVersion=11,this.cache={},this._s&&this._s.setItem)try{var i=this._s.setItem("__react_native_storage_test","test");this.isPromise=!(!i||!i.then)}catch(e){throw console.warn(e),delete this._s,e}else console.warn("Data would be lost after reload cause there is no storageBackend specified!\n \nEither use localStorage(for web) or AsyncStorage(for React Native) as a storageBackend.");this._mapPromise=this.getItem("map").then(function(e){t._m=t._checkMap(e&&JSON.parse(e)||{})})}var i,s,o,c,u;return i=e,(s=[{key:"getItem",value:function(e){return this._s?this.isPromise?this._s.getItem(e):Promise.resolve(this._s.getItem(e)):Promise.resolve()}},{key:"setItem",value:function(e,t){return this._s?this.isPromise?this._s.setItem(e,t):Promise.resolve(this._s.setItem(e,t)):Promise.resolve()}},{key:"removeItem",value:function(e){return this._s?this.isPromise?this._s.removeItem(e):Promise.resolve(this._s.removeItem(e)):Promise.resolve()}},{key:"_initMap",value:function(){return{innerVersion:this._innerVersion,index:0,__keys__:{}}}},{key:"_checkMap",value:function(e){return e&&e.innerVersion&&e.innerVersion===this._innerVersion?e:this._initMap()}},{key:"_getId",value:function(e,t){return e+"_"+t}},{key:"_saveToMap",value:function(e){var t=e.key,n=e.id,r=e.data,i=this._getId(t,n),a=this._m;if(void 0!==a[i])return this.enableCache&&(this.cache[i]=JSON.parse(r)),this.setItem("map_"+a[i],r);if(void 0!==a[a.index]){var s=a[a.index],o=s.split("_");delete a[s],this._removeIdInKey(o[0],o[1]),this.enableCache&&delete this.cache[s]}if(a[i]=a.index,a[a.index]=i,a.__keys__[t]=a.__keys__[t]||[],a.__keys__[t].push(n),this.enableCache){var c=JSON.parse(r);this.cache[i]=c}var u=a.index;++a.index===this._SIZE&&(a.index=0),this.setItem("map_"+u,r),this.setItem("map",JSON.stringify(a))}},{key:"save",value:function(e){var t=this,n=e.key,r=e.id,i=e.data,a=e.rawData,s=e.expires,o=void 0===s?this.defaultExpires:s;-1!==n.toString().indexOf("_")&&console.error('Please do not use "_" in key!');var c={rawData:i};if(void 0===i){if(void 0===a)return void console.error('"data" is required in save()!');console.warn('"rawData" is deprecated, please use "data" instead!'),c.rawData=a}var u=Date.now();if(null!==o&&(c.expires=u+o),c=JSON.stringify(c),void 0===r){if(this.enableCache){var h=JSON.parse(c);this.cache[n]=h}return this.setItem(n,c)}return-1!==r.toString().indexOf("_")&&console.error('Please do not use "_" in id!'),this._mapPromise.then(function(){return t._saveToMap({key:n,id:r,data:c})})}},{key:"getBatchData",value:function(e){var t=this;return Promise.all(e.map(function(e){return t.load(e)}))}},{key:"getBatchDataWithIds",value:(c=regeneratorRuntime.mark(function e(t){var n,r,i,a,s,o,c,u,h=this;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.key,r=t.ids,i=t.syncInBackground,a=t.syncParams,s=r.map(function(e){return h.load({key:n,id:e,syncInBackground:i,autoSync:!1,batched:!0})}),e.next=4,Promise.all(s);case 4:if(o=e.sent,c=[],o.forEach(function(e){void 0!==e.syncId&&c.push(e.syncId)}),!c.length){e.next=14;break}return e.next=10,this.sync[n]({id:c,syncParams:a});case 10:return u=e.sent,e.abrupt("return",o.map(function(e){return e.syncId?u.shift():e}));case 14:return e.abrupt("return",o);case 15:case"end":return e.stop()}},e,this)}),u=function(){var e=this,n=arguments;return new Promise(function(r,i){var a=c.apply(e,n);function s(e){t(a,r,i,s,o,"next",e)}function o(e){t(a,r,i,s,o,"throw",e)}s(void 0)})},function(e){return u.apply(this,arguments)})},{key:"_lookupGlobalItem",value:function(e){var t=this,n=e.key;return this.enableCache&&void 0!==this.cache[n]?this._loadGlobalItem(a({ret:this.cache[n]},e)):this.getItem(n).then(function(n){return t._loadGlobalItem(a({ret:n},e))})}},{key:"_loadGlobalItem",value:function(e){var t=e.key,n=e.ret,r=e.autoSync,i=e.syncInBackground,a=e.syncParams;if(null==n){if(r&&this.sync[t])return this.sync[t]({syncParams:a});throw new f(JSON.stringify(e))}"string"==typeof n&&(n=JSON.parse(n),this.enableCache&&(this.cache[t]=n));var s=Date.now();if(n.expires<s){if(r&&this.sync[t]){if(i){try{this.sync[t]({syncParams:a,syncInBackground:i})}catch(e){}return n.rawData}return this.sync[t]({syncParams:a,syncInBackground:i})}throw new y(JSON.stringify(e))}return n.rawData}},{key:"_noItemFound",value:function(e){var t=e.key,n=e.id,r=e.autoSync,i=e.syncParams;if(this.sync[t])return r?this.sync[t]({id:n,syncParams:i}):{syncId:n};throw new f(JSON.stringify(e))}},{key:"_loadMapItem",value:function(e){var t=e.ret,n=e.key,r=e.id,i=e.autoSync,a=e.batched,s=e.syncInBackground,o=e.syncParams;if(null==t)return this._noItemFound(e);if("string"==typeof t){t=JSON.parse(t);var c=e.key,u=e.id,h=this._getId(c,u);this.enableCache&&(this.cache[h]=t)}var l=Date.now();if(t.expires<l){if(i&&this.sync[n]){if(s){try{this.sync[n]({id:r,syncParams:o,syncInBackground:s})}catch(e){}return t.rawData}return this.sync[n]({id:r,syncParams:o,syncInBackground:s})}if(a)return{syncId:r};throw new y(JSON.stringify(e))}return t.rawData}},{key:"_lookUpInMap",value:function(e){var t,n=this,r=this._m,i=e.key,s=e.id,o=this._getId(i,s);return this.enableCache&&this.cache[o]?(t=this.cache[o],this._loadMapItem(a({ret:t},e))):void 0!==r[o]?this.getItem("map_"+r[o]).then(function(t){return n._loadMapItem(a({ret:t},e))}):this._noItemFound(a({ret:t},e))}},{key:"remove",value:function(e){var t=this;return this._mapPromise.then(function(){var n=t._m,r=e.key,i=e.id;if(void 0===i)return t.enableCache&&t.cache[r]&&delete t.cache[r],t.removeItem(r);var a=t._getId(r,i);if(void 0!==n[a]){t.enableCache&&t.cache[a]&&delete t.cache[a],t._removeIdInKey(r,i);var s=n[a];return delete n[a],t.setItem("map",JSON.stringify(n)),t.removeItem("map_"+s)}})}},{key:"_removeIdInKey",value:function(e,t){var n=(this._m.__keys__[e]||[]).indexOf(t);-1!==n&&this._m.__keys__[e].splice(n,1)}},{key:"load",value:function(e){var t=this,n=e.key,r=e.id,i=e.autoSync,a=void 0===i||i,s=e.syncInBackground,o=void 0===s||s,c=e.syncParams,u=e.batched;return this._mapPromise.then(function(){return void 0===r?t._lookupGlobalItem({key:n,autoSync:a,syncInBackground:o,syncParams:c}):t._lookUpInMap({key:n,id:r,autoSync:a,syncInBackground:o,batched:u,syncParams:c})})}},{key:"clearAll",value:function(){this._s.clear&&this._s.clear(),this._m=this._initMap()}},{key:"clearMap",value:function(){var e=this;return this.removeItem("map").then(function(){e.cache={},e._m=e._initMap()})}},{key:"clearMapForKey",value:function(e){var t=this;return this._mapPromise.then(function(){var n=(t._m.__keys__[e]||[]).map(function(n){return t.remove({key:e,id:n})});return Promise.all(n)})}},{key:"getIdsForKey",value:function(e){var t=this;return this._mapPromise.then(function(){return t._m.__keys__[e]||[]})}},{key:"getAllDataForKey",value:function(e,t){var n=this;return t=Object.assign({syncInBackground:!0},t),this.getIdsForKey(e).then(function(r){var i=r.map(function(n){return{key:e,id:n,syncInBackground:t.syncInBackground}});return n.getBatchData(i)})}}])&&r(i.prototype,s),o&&r(i,o),e}();e.default=d,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=storage.umd.js.map