UNPKG

local-store-pro

Version:

Better localStorage, subscribable localStorage, expirable localStorage

4 lines (3 loc) 4.29 kB
/*! local-store-pro v3.0.3 | MIT */ function e(e){var t=e.key,n=e.newValue,r=e.oldValue,o=e.type,i=new StorageEvent(o,{key:t,newValue:n,oldValue:r,url:location.href,storageArea:localStorage});null===window||void 0===window||window.dispatchEvent(i)}function t(e){var t="undefined"===e||void 0===e?null:e;return JSON.parse(t)}function n(e,n,r){var o,i,s,l,u=null,a=null;try{u=null!==(i=null===(o=t(n.newValue))||void 0===o?void 0:o.value)&&void 0!==i?i:u,a=null!==(l=null===(s=t(n.oldValue))||void 0===s?void 0:s.value)&&void 0!==l?l:a}catch(e){console.warn("has an exception in your json parse, from ".concat(n.url,", .eg ").concat(e))}var c={type:n.type,key:null===n.key?r:n.key,newValue:u,oldValue:a,url:n.url,isTrusted:n.isTrusted,native:n};e.forEach((function(e){if("function"==typeof e)try{e(c)}catch(t){console.warn("has an exception in your ".concat(e.name||"anonymous function","( ev ){\n ").concat(t,"\n }"))}}))}var r=function(){function r(){this.namespace="local-store-pro",this.store=localStorage,this.observers=new Map,this.hasBindWindow=!1}return r.getInstance=function(){return r.storage||(r.storage=new r),r.storage},r.prototype.setHasBindWindow=function(e){this.hasBindWindow=e},r.prototype.getHasBindWindow=function(){return this.hasBindWindow},r.prototype.setNamespace=function(e){this.namespace=e},r.prototype.getNamespace=function(){return this.namespace},r.prototype.getUsed=function(){return(JSON.stringify(this.getStore()).length/1024).toFixed(3)+" KB"},r.prototype.setStore=function(e){this.store=e},r.prototype.getStore=function(){return this.store},r.prototype.set=function(t,n,r){!function(e){"string"==typeof e||console.warn("store failed, entry a valid string key.")}(t);var o=JSON.stringify({value:n,expires:r?1e3*r+Date.now():r});try{e({key:t,newValue:o,oldValue:this.getItem(t),type:"storage"})}finally{this.getStore().setItem(t,o)}return this},r.prototype.get=function(e){var n,r=null!==(n=this.getStore().getItem(e))&&void 0!==n?n:null;if(null===r)return r;var o=t(r);return!o.expires||Date.now()<=o.expires?o.value:(this.remove(e,!0),null)},r.prototype.getItem=function(e){var n,r=null!==(n=this.getStore().getItem(e))&&void 0!==n?n:null;if(null===r)return r;var o=t(r);return!o.expires||Date.now()<=o.expires?r:null},r.prototype.has=function(e){return null!==this.getStore().getItem(e)},r.prototype.publish=function(e,t,r,o){(void 0===r&&(r=!1),void 0===o&&(o=null),e||r||this.has(e))&&(Array.isArray(e)?n(e,t,o):n(this.getObserver(e),t,o))},r.prototype.publishAll=function(e){var t=this;this.observers.forEach((function(n,r){t.publish(n,e,!0,r)}))},r.prototype.subscribe=function(e,t){var n=this.observers.get(e);return n?n.push(t):this.observers.set(e,[t]),this},r.prototype.getObserver=function(e){var t;return null!==(t=this.observers.get(e))&&void 0!==t?t:[]},r.prototype.unsubscribe=function(e,t){var n=this;if(Array.isArray(e))e.forEach((function(e){n.observers.delete(e)}));else if("string"==typeof e&&t){var r=this.observers.get(e);t.name&&r&&(r=r.filter((function(e){return e.name!==t.name})),this.observers.set(e,r))}else"string"==typeof e?this.observers.delete(e):this.observers.clear()},r.prototype.remove=function(t,n){var r;return e({key:t,newValue:null,oldValue:null!==(r=this.getStore().getItem(t))&&void 0!==r?r:null,type:"storage"}),!0!==n&&this.unsubscribe(t),this.getStore().removeItem(t),this},r.prototype.clear=function(){e({key:null,newValue:null,oldValue:null,type:"storage"}),this.getStore().clear()},r.storage=null,r}(),o=r.getInstance(),i=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=e.length,r=e[0],i=e[1],s=e[2];return 1===n&&"string"==typeof r?o.get(r):n>=2&&"string"==typeof r?void 0===i?o.remove(r):"function"==typeof i?o.set(r,i(),s):o.set(r,i,s):null};var s=function(){if(Object.entries(Object.getPrototypeOf(o)).forEach((function(e){var t=e[0],n=e[1];"constructor"!==t&&"getStore"!==t&&(i[t]=n.bind(o))})),i.localStore=o,!o.getHasBindWindow()){o.setHasBindWindow(!0);window.addEventListener("storage",(function(e){var t,n,r,i,s;n=(t=e).key,r=t.newValue,i=t.oldValue,s=t.isTrusted,null===n||r===i?null===n&&null===r&&null===i&&(o.publishAll(t),!0!==s&&o.unsubscribe()):o.publish(n,t,!0)}))}return i}();export{s as default}; //# sourceMappingURL=local-store-pro.esm.js.map