UNPKG

@egjs/persist

Version:

Provide cache interface to handle persisted data among history navigation.

3 lines (2 loc) 6.89 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t.eg=t.eg||{},t.eg.Persist=e())}(this,function(){"use strict";var r="undefined"!=typeof window&&window||{},i=r.console,a=(r.document,r.history),o=r.location,t=r.navigator,e=r.parseFloat,n=r.performance,c=null,u=null;try{c=r.localStorage,u=r.sessionStorage}catch(t){}var s,f,l,p,h,d="___persist___",y="state"+d,v="depths",g="lastUrl",w=n&&n.navigation,E=w&&w.TYPE_NAVIGATE||0,_=(w&&w.TYPE_RELOAD,w&&w.TYPE_BACK_FORWARD||2),x=t?t.userAgent:"",m=(s=new RegExp("iPhone|iPad","i").test(x),f=new RegExp("Mac","i").test(x)&&!new RegExp("Chrome","i").test(x)&&new RegExp("Apple","i").test(x),l=new RegExp("Android ","i").test(x),p=new RegExp("wv; |inapp;","i").test(x),h=l?e(new RegExp("(Android)\\s([\\d_\\.]+|\\d_0)","i").exec(x)[2]):void 0,a&&"replaceState"in a&&"state"in a),O="None";function b(t){if(t){var e="__tmp__"+d;try{return t.setItem(e,d),t.getItem(e)===d}catch(t){return!1}}}var R,S=(b(u)?(R=u,O="SessionStorage"):b(c)?(R=c,O="LocalStorage"):a&&a.state&&(O="History"),R);function P(){console.warn("window.history or session/localStorage has no valid format data to be handled in persist.")}function j(t){var e,n;if(S?n=S.getItem(t):a.state?"object"==typeof a.state&&null!==a.state?n=a.state[t]:P():n=a.state,null===n)return{};var r="string"==typeof n&&0<n.length&&"null"!==n;try{var o=!("object"!=typeof(e=JSON.parse(n))||e instanceof Array);if(!r||!o)throw new Error}catch(t){P(),e={}}return e}function A(t,e){if(S)e?S.setItem(t,JSON.stringify(e)):S.removeItem(t);else try{var n=a&&null!=a.state?a.state:{};a&&"object"==typeof n?(n[t]=JSON.stringify(e),a.replaceState(n,document.title,o.href)):console.warn("To use a history object, it must be an object that is not a primitive type.")}catch(t){console.warn(t.message)}e?r[d]=!0:delete r[d]}function N(){return S}function k(t,e){if(m||S){var n=j(t)[e];return"null"!==n&&void 0!==n||(n=null),n}}function I(t,e,n){if(m||S){var r=j(t);r[e]=n,A(t,r)}}function T(t){A(t,null)}function D(t){return(D=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function z(t,e){return(z=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function J(t,e,n){return(J=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(t){return!1}}()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&z(o,n.prototype),o}).apply(null,arguments)}function M(t){var r="function"==typeof Map?new Map:void 0;return(M=function(t){if(null===t||(e=t,-1===Function.toString.call(e).indexOf("[native code]")))return t;var e;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(t))return r.get(t);r.set(t,n)}function n(){return J(t,arguments,D(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),z(n,t)})(t)}function F(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var Q,B,C,K,L,Y=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},V=function(p){var t,e;function h(t,e){var n,r=e.length,o=O,i=N(),a="",c=[];if(i){for(var u=i.length,s=0;s<u;++s){var f=i.key(s),l=i.getItem(f)||"";c.push({key:f,size:l.length})}(c=c.sort(function(t,e){return e.size-t.size}).slice(0,3)).length&&(a=" The highest values of "+o+" are "+c.map(function(t){var e;return JSON.stringify(((e={})[t.key]=t.size,e))}).join(", ")+".")}return n=p.call(this,"Setting the value (size: "+r+") of '"+t+"' exceeded the "+o+"'s quota."+a)||this,Y(F(F(n)),h.prototype),n.name="PersistQuotaExceededError",n.storageType=o,n.key=t,n.size=r,n.values=c,n}return e=p,(t=h).prototype=Object.create(e.prototype),(t.prototype.constructor=t).__proto__=e,h}(M(Error)),q=t?t.userAgent:"",G=(Q=new RegExp("iPhone|iPad","i").test(q),B=new RegExp("Mac","i").test(q)&&!new RegExp("Chrome","i").test(q)&&new RegExp("Apple","i").test(q),C=new RegExp("Android ","i").test(q),K=new RegExp("wv; |inapp;","i").test(q),L=C?e(new RegExp("(Android)\\s([\\d_\\.]+|\\d_0)","i").exec(q)[2]):void 0,!(Q||B||C&&(L<=4.3&&K||L<3)));function H(){return o?o.href.split("#")[0]:""}function U(t){return t+d}function W(t){return"QuotaExceededError"===t.name||"PersistQuotaExceededError"===t.name}var X="";function Z(t,e,n){var r=t;r||(r=isNaN(e[0])?{}:[]);var o=e.shift();return 0===e.length?(r instanceof Array&&isNaN(o)&&i.warn("Don't use key string on array"),n(r,o)):r[o]=Z(r[o],e,n),r}function $(e,n){try{I(y,e,n)}catch(t){nt(t,y,n)&&(e===g?$(e,n):e===v&&$(e,n&&n.slice(1)))}}function tt(t){return k(y,t)}function et(t){void 0===t&&(t=0);var e=H();if(X!==e){var n=X;try{X=e;var r=tt(v)||[];if(t===_){~r.indexOf(e)&&$(g,e)}else{var o=tt(g);if(T(U(e)),t===E&&e!==o){var i=r.indexOf(o);r.splice(i+1,r.length).forEach(function(t){T(U(t))});var a=r.indexOf(e);~a&&r.splice(a,1)}r.indexOf(e)<0&&r.push(e),$(v,r),$(g,e)}}catch(t){throw X=n,t}}}function nt(t,e,n){if(function(){var t=tt(v)||[],e=t.splice(0,1);if(!e.length)return!1;var n=e[0];if(T(U(n)),X===n&&($(g,X=""),!t.length))return!1;return $(v,t),!0}())return!0;throw W(t)?new V(e,n?JSON.stringify(n):""):t}var rt=function(){var t=function(){function t(t){this.key=t||""}t.clear=function(){(tt(v)||[]).forEach(function(t){T(U(t))}),T(y),X=""},t.isNeeded=function(){return G};var e=t.prototype;return e.get=function(t){et(E);var e=k(U(H()),this.key);if(!t||0===t.length)return e;for(var n=t.split("."),r=e,o=!0,i=0;i<n.length;i++){if(!r){o=!1;break}r=r[n[i]]}return o&&null!=r?r:null},e.set=function(e,n){et(E);var t=this.key,r=U(H()),o=k(r,t);try{if(0===e.length)I(r,t,n);else I(r,t,Z(o,e.split("."),function(t,e){t[e]=n}))}catch(t){nt(t,r,n)&&this.set(e,n)}return this},e.remove=function(e){et(E);var t=this.key,n=U(H()),r=k(n,t);try{if(0===e.length)I(n,t,null);else I(n,t,Z(r,e.split("."),function(t,e){"object"==typeof t&&delete t[e]}))}catch(t){nt(t)&&this.remove(e)}return this},t}();return t.VERSION="2.6.0",t.StorageManager={reset:T,setStateByKey:I,getStateByKey:k,getStorage:N},t}();"onpopstate"in r&&r.addEventListener("popstate",function(){try{et(_)}catch(t){if(!W(t))throw t}});try{et(n&&n.navigation&&n.navigation.type)}catch(t){if(!W(t))throw t}var ot={updateDepth:et,replaceDepth:function(){var t=H();if(X!==t){var e=X;try{X=t;var n=tt(v)||[],r=n.indexOf(e);0<=r&&(n.splice(r,1),T(U(e)));var o=n.indexOf(t);0<=o&&(n.splice(o,1),T(U(t))),n.push(t),$(v,n),$(g,t)}catch(t){throw X=e,t}}},PersistQuotaExceededError:V,default:rt};for(var it in ot)rt[it]=ot[it];return rt}); //# sourceMappingURL=persist.min.js.map