local-store-pro
Version:
Better localStorage, subscribable localStorage, expirable localStorage
4 lines (3 loc) • 4.6 kB
JavaScript
/*! local-store-pro v3.0.3 | MIT */
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self,function(){var n=e.LocalStorePro,o=e.LocalStorePro=t();o.noConflict=function(){return e.LocalStorePro=n,o}}())}(this,(function(){"use strict";function e(e){var t=e.key,n=e.newValue,o=e.oldValue,r=e.type,i=new StorageEvent(r,{key:t,newValue:n,oldValue:o,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,o){var r,i,s,u,l=null,a=null;try{l=null!==(i=null===(r=t(n.newValue))||void 0===r?void 0:r.value)&&void 0!==i?i:l,a=null!==(u=null===(s=t(n.oldValue))||void 0===s?void 0:s.value)&&void 0!==u?u: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?o:n.key,newValue:l,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 o=function(){function o(){this.namespace="local-store-pro",this.store=localStorage,this.observers=new Map,this.hasBindWindow=!1}return o.getInstance=function(){return o.storage||(o.storage=new o),o.storage},o.prototype.setHasBindWindow=function(e){this.hasBindWindow=e},o.prototype.getHasBindWindow=function(){return this.hasBindWindow},o.prototype.setNamespace=function(e){this.namespace=e},o.prototype.getNamespace=function(){return this.namespace},o.prototype.getUsed=function(){return(JSON.stringify(this.getStore()).length/1024).toFixed(3)+" KB"},o.prototype.setStore=function(e){this.store=e},o.prototype.getStore=function(){return this.store},o.prototype.set=function(t,n,o){!function(e){"string"==typeof e||console.warn("store failed, entry a valid string key.")}(t);var r=JSON.stringify({value:n,expires:o?1e3*o+Date.now():o});try{e({key:t,newValue:r,oldValue:this.getItem(t),type:"storage"})}finally{this.getStore().setItem(t,r)}return this},o.prototype.get=function(e){var n,o=null!==(n=this.getStore().getItem(e))&&void 0!==n?n:null;if(null===o)return o;var r=t(o);return!r.expires||Date.now()<=r.expires?r.value:(this.remove(e,!0),null)},o.prototype.getItem=function(e){var n,o=null!==(n=this.getStore().getItem(e))&&void 0!==n?n:null;if(null===o)return o;var r=t(o);return!r.expires||Date.now()<=r.expires?o:null},o.prototype.has=function(e){return null!==this.getStore().getItem(e)},o.prototype.publish=function(e,t,o,r){(void 0===o&&(o=!1),void 0===r&&(r=null),e||o||this.has(e))&&(Array.isArray(e)?n(e,t,r):n(this.getObserver(e),t,r))},o.prototype.publishAll=function(e){var t=this;this.observers.forEach((function(n,o){t.publish(n,e,!0,o)}))},o.prototype.subscribe=function(e,t){var n=this.observers.get(e);return n?n.push(t):this.observers.set(e,[t]),this},o.prototype.getObserver=function(e){var t;return null!==(t=this.observers.get(e))&&void 0!==t?t:[]},o.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 o=this.observers.get(e);t.name&&o&&(o=o.filter((function(e){return e.name!==t.name})),this.observers.set(e,o))}else"string"==typeof e?this.observers.delete(e):this.observers.clear()},o.prototype.remove=function(t,n){var o;return e({key:t,newValue:null,oldValue:null!==(o=this.getStore().getItem(t))&&void 0!==o?o:null,type:"storage"}),!0!==n&&this.unsubscribe(t),this.getStore().removeItem(t),this},o.prototype.clear=function(){e({key:null,newValue:null,oldValue:null,type:"storage"}),this.getStore().clear()},o.storage=null,o}(),r=o.getInstance(),i=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];var n=e.length,o=e[0],i=e[1],s=e[2];return 1===n&&"string"==typeof o?r.get(o):n>=2&&"string"==typeof o?void 0===i?r.remove(o):"function"==typeof i?r.set(o,i(),s):r.set(o,i,s):null};return function(){if(Object.entries(Object.getPrototypeOf(r)).forEach((function(e){var t=e[0],n=e[1];"constructor"!==t&&"getStore"!==t&&(i[t]=n.bind(r))})),i.localStore=r,!r.getHasBindWindow()){r.setHasBindWindow(!0);window.addEventListener("storage",(function(e){var t,n,o,i,s;n=(t=e).key,o=t.newValue,i=t.oldValue,s=t.isTrusted,null===n||o===i?null===n&&null===o&&null===i&&(r.publishAll(t),!0!==s&&r.unsubscribe()):r.publish(n,t,!0)}))}return i}()}));
//# sourceMappingURL=local-store-pro.min.js.map