UNPKG

ng2-cache

Version:
1 lines 7.68 kB
!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?factory(exports,require("@angular/core")):"function"==typeof define&&define.amd?define("ng2-cache",["exports","@angular/core"],factory):factory((global.ng=global.ng||{},global.ng.angularLibraryStarter={}),global.ng.core)}(this,function(exports,core){"use strict";function __extends(d,b){function __(){this.constructor=d}extendStatics(d,b),d.prototype=null===b?Object.create(b):(__.prototype=b.prototype,new __)}var extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)b.hasOwnProperty(p)&&(d[p]=b[p])},CacheStorageAbstract=function(){return function(){}}(),CacheSessionStorage=function(_super){function CacheSessionStorage(){return null!==_super&&_super.apply(this,arguments)||this}return __extends(CacheSessionStorage,_super),CacheSessionStorage.prototype.getItem=function(key){var value=sessionStorage.getItem(key);return value?JSON.parse(value):null},CacheSessionStorage.prototype.setItem=function(key,value){try{return sessionStorage.setItem(key,JSON.stringify(value)),!0}catch(e){return!1}},CacheSessionStorage.prototype.removeItem=function(key){sessionStorage.removeItem(key)},CacheSessionStorage.prototype.clear=function(){sessionStorage.clear()},CacheSessionStorage.prototype.type=function(){return 1},CacheSessionStorage.prototype.isEnabled=function(){try{return sessionStorage.setItem("test","test"),sessionStorage.removeItem("test"),!0}catch(e){return!1}},CacheSessionStorage.decorators=[{type:core.Injectable}],CacheSessionStorage.ctorParameters=function(){return[]},CacheSessionStorage}(CacheStorageAbstract),CacheLocalStorage=function(_super){function CacheLocalStorage(){return null!==_super&&_super.apply(this,arguments)||this}return __extends(CacheLocalStorage,_super),CacheLocalStorage.prototype.getItem=function(key){var value=localStorage.getItem(key);return value?JSON.parse(value):null},CacheLocalStorage.prototype.setItem=function(key,value){try{return localStorage.setItem(key,JSON.stringify(value)),!0}catch(e){return!1}},CacheLocalStorage.prototype.removeItem=function(key){localStorage.removeItem(key)},CacheLocalStorage.prototype.clear=function(){localStorage.clear()},CacheLocalStorage.prototype.type=function(){return 0},CacheLocalStorage.prototype.isEnabled=function(){try{return localStorage.setItem("test","test"),localStorage.removeItem("test"),!0}catch(e){return!1}},CacheLocalStorage.decorators=[{type:core.Injectable}],CacheLocalStorage.ctorParameters=function(){return[]},CacheLocalStorage}(CacheStorageAbstract),CacheMemoryStorage=function(_super){function CacheMemoryStorage(){var _this=null!==_super&&_super.apply(this,arguments)||this;return _this._data={},_this}return __extends(CacheMemoryStorage,_super),CacheMemoryStorage.prototype.getItem=function(key){return this._data[key]?this._data[key]:null},CacheMemoryStorage.prototype.setItem=function(key,value){return this._data[key]=value,!0},CacheMemoryStorage.prototype.removeItem=function(key){delete this._data[key]},CacheMemoryStorage.prototype.clear=function(){this._data=[]},CacheMemoryStorage.prototype.type=function(){return 2},CacheMemoryStorage.prototype.isEnabled=function(){return!0},CacheMemoryStorage.decorators=[{type:core.Injectable}],CacheMemoryStorage.ctorParameters=function(){return[]},CacheMemoryStorage}(CacheStorageAbstract),CACHE_PREFIX="CacheService",CacheService=function(){function CacheService(_storage){this._storage=_storage,this._defaultOptions={expires:Number.MAX_VALUE,maxAge:Number.MAX_VALUE},this._prefix=CACHE_PREFIX,this._validateStorage()}return CacheService.prototype.set=function(key,value,options){var storageKey=this._toStorageKey(key);return options=options||this._defaultOptions,!!this._storage.setItem(storageKey,this._toStorageValue(value,options))&&(!this._isSystemKey(key)&&options.tag&&this._saveTag(options.tag,storageKey),!0)},CacheService.prototype.get=function(key){var storageValue=this._storage.getItem(this._toStorageKey(key)),value=null;return storageValue&&(this._validateStorageValue(storageValue)?value=storageValue.value:this.remove(key)),value},CacheService.prototype.exists=function(key){return!!this.get(key)},CacheService.prototype.remove=function(key){this._storage.removeItem(this._toStorageKey(key)),this._removeFromTag(this._toStorageKey(key))},CacheService.prototype.removeAll=function(){this._storage.clear()},CacheService.prototype.getTagData=function(tag){var _this=this,tags=this.get(this._tagsStorageKey())||{},result={};return tags[tag]&&tags[tag].forEach(function(key){var data=_this.get(_this._fromStorageKey(key));data&&(result[_this._fromStorageKey(key)]=data)}),result},CacheService.prototype.useStorage=function(type){var service=new CacheService(this._initStorage(type));return service.setGlobalPrefix(this._getCachePrefix()),service},CacheService.prototype.removeTag=function(tag){var _this=this,tags=this.get(this._tagsStorageKey())||{};tags[tag]&&(tags[tag].forEach(function(key){_this._storage.removeItem(key)}),delete tags[tag],this.set(this._tagsStorageKey(),tags))},CacheService.prototype.setGlobalPrefix=function(prefix){this._prefix=prefix},CacheService.prototype._validateStorage=function(){this._storage||(this._storage=this._initStorage(1)),this._storage.isEnabled()||(this._storage=this._initStorage(2))},CacheService.prototype._removeFromTag=function(key){var index,tags=this.get(this._tagsStorageKey())||{};for(var tag in tags)if(-1!==(index=tags[tag].indexOf(key))){tags[tag].splice(index,1),this.set(this._tagsStorageKey(),tags);break}},CacheService.prototype._initStorage=function(type){var storage;switch(type){case 1:storage=new CacheSessionStorage;break;case 0:storage=new CacheLocalStorage;break;default:storage=new CacheMemoryStorage}return storage},CacheService.prototype._toStorageKey=function(key){return this._getCachePrefix()+key},CacheService.prototype._fromStorageKey=function(key){return key.replace(this._getCachePrefix(),"")},CacheService.prototype._toStorageValue=function(value,options){return{value:value,options:this._toStorageOptions(options)}},CacheService.prototype._toStorageOptions=function(options){var storageOptions={};return storageOptions.expires=options.expires?options.expires:options.maxAge?Date.now()+1e3*options.maxAge:this._defaultOptions.expires,storageOptions.maxAge=options.maxAge?options.maxAge:this._defaultOptions.maxAge,storageOptions},CacheService.prototype._validateStorageValue=function(value){return!!value.options.expires&&value.options.expires>Date.now()},CacheService.prototype._isSystemKey=function(key){return-1!==[this._tagsStorageKey()].indexOf(key)},CacheService.prototype._saveTag=function(tag,key){var tags=this.get(this._tagsStorageKey())||{};tags[tag]?tags[tag].push(key):tags[tag]=[key],this.set(this._tagsStorageKey(),tags)},CacheService.prototype._getCachePrefix=function(){return this._prefix},CacheService.prototype._tagsStorageKey=function(){return"CacheService_tags"},CacheService.decorators=[{type:core.Injectable}],CacheService.ctorParameters=function(){return[{type:CacheStorageAbstract,decorators:[{type:core.Optional}]}]},CacheService}(),Ng2CacheModule=function(){function Ng2CacheModule(){}return Ng2CacheModule.decorators=[{type:core.NgModule,args:[{providers:[CacheService]}]}],Ng2CacheModule.ctorParameters=function(){return[]},Ng2CacheModule}();exports.CacheService=CacheService,exports.CacheStorageAbstract=CacheStorageAbstract,exports.CacheLocalStorage=CacheLocalStorage,exports.CacheMemoryStorage=CacheMemoryStorage,exports.CacheSessionStorage=CacheSessionStorage,exports.Ng2CacheModule=Ng2CacheModule,Object.defineProperty(exports,"__esModule",{value:!0})});