angular-persistence
Version:
A library to handle persistence for Angular 2 applications.
1 lines • 13.6 kB
JavaScript
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports,require("@angular/core"),require("rxjs")):typeof define==="function"&&define.amd?define(["exports","@angular/core","rxjs"],factory):factory((global.ng=global.ng||{},global.ng.AngularPersistence=global.ng.AngularPersistence||{}),global.ng.core,global.rxjs)})(this,function(exports,_angular_core,rxjs){"use strict";var StorageType={};StorageType.MEMORY=0;StorageType.IMMUTABLE_MEMORY=1;StorageType.SESSION=2;StorageType.LOCAL=3;StorageType[StorageType.MEMORY]="MEMORY";StorageType[StorageType.IMMUTABLE_MEMORY]="IMMUTABLE_MEMORY";StorageType[StorageType.SESSION]="SESSION";StorageType[StorageType.LOCAL]="LOCAL";var CacheImpl=function(){function CacheImpl(key,_loader,service,config){var _this=this;if(config===void 0){config={}}this._loader=_loader;var type=config.type||StorageType.MEMORY;service.defineProperty(this,"_value",key,config);this._changes=service.changes({key:key,type:type}).map(function(def){return _this._value}).publishBehavior(this._value).refCount()}CacheImpl.prototype.get=function(){var _this=this;var result=this._value;if(result===undefined){var observable=this._cachedObservable;if(observable===undefined){var loaded=this._loader();if(loaded&&loaded instanceof rxjs.Observable){var newObservable=loaded.publishLast().refCount().do(function(value){return _this._value=value}).do(function(value){return _this._cachedObservable=undefined});this._cachedObservable=newObservable;return newObservable}else{result=loaded;this._value=result}}else{return observable}}return rxjs.Observable.of(result)};CacheImpl.prototype.changes=function(){return this._changes};CacheImpl.prototype.clear=function(){this._value=undefined};return CacheImpl}();var INFO_KEY="__INFO";var ContainerInfo=function(){function ContainerInfo(_namespace,_container){this._namespace=_namespace;this._container=_container;var infoObj=_container.get(this._namespace);if(infoObj){if(typeof infoObj!=="object"||!infoObj[INFO_KEY]){throw new Error("Potential attribute conflict detected")}}}ContainerInfo.prototype.addAttribute=function(key){var item=this._getInfo();item[key]=true;this._setInfo(item)};ContainerInfo.prototype.removeAttribute=function(key){var info=this._getInfo();delete info[key];this._setInfo(info)};ContainerInfo.prototype.getAttributes=function(){return Object.keys(this._getInfo()).filter(function(key){return key!==INFO_KEY})};ContainerInfo.prototype.available=function(){var infoObj=this._container.get(this._namespace);return!infoObj||typeof infoObj==="object"&&infoObj[INFO_KEY]};ContainerInfo.prototype._getInfo=function(){var obj=this._container.get(this._namespace);if(!obj){obj={};obj[INFO_KEY]=true}return obj};ContainerInfo.prototype._setInfo=function(info){if(Object.keys(info).length<=1){this._container.remove(this._namespace)}else{this._container.set(this._namespace,info)}};return ContainerInfo}();var SubStorage=function(){function SubStorage(_namespace,_root,_available){if(_available===void 0){_available=true}this._namespace=_namespace;this._root=_root;this._available=_available;this._info=new ContainerInfo(_namespace,_root)}SubStorage.prototype.set=function(key,value){if(!this._available){return false}var val=this._root.set(this._getNamespacedKey(key),value);this._info.addAttribute(key);return val};SubStorage.prototype.get=function(key){if(!this._available){return undefined}var val=this._root.get(this._getNamespacedKey(key));if(val===undefined){this._info.removeAttribute(key)}return val};SubStorage.prototype.remove=function(key){if(!this._available){return undefined}this._info.removeAttribute(key);return this._root.remove(this._getNamespacedKey(key))};SubStorage.prototype.removeAll=function(){var _this=this;this._info.getAttributes().forEach(function(element){_this.remove(element)})};SubStorage.prototype.available=function(){return this._available&&this._info.available()};SubStorage.prototype.exists=function(key){return this.get(key)!==undefined};SubStorage.prototype.keys=function(){var _this=this;return this._info.getAttributes().filter(function(key){return _this.exists(key)})};SubStorage.prototype._getNamespacedKey=function(key){return this._namespace+"::"+key};return SubStorage}();var NULL_VALUE="_____NULL_VALUE_____";var BrowserContainer=function(){function BrowserContainer(_storage){this._storage=_storage}BrowserContainer.prototype.set=function(key,value){try{if(value===null){value=NULL_VALUE}if(value===undefined){this._storage.removeItem(key)}else{this._storage.setItem(key,JSON.stringify(value))}}catch(error){return false}return true};BrowserContainer.prototype.get=function(key){var strval=this._storage.getItem(key);if(strval===null){return undefined}var value=JSON.parse(strval);if(value===NULL_VALUE){return null}return value};BrowserContainer.prototype.remove=function(key){var curVal=this.get(key);if(curVal!==undefined){this._storage.removeItem(key)}return curVal};BrowserContainer.prototype.removeAll=function(){this._storage.clear()};return BrowserContainer}();var __extends$1=undefined&&undefined.__extends||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p];function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)};var PREFIX="ANGULAR_PERSISTENCE_STORAGE";var AbstractBrowserStorage=function(_super){__extends$1(AbstractBrowserStorage,_super);function AbstractBrowserStorage(storage){_super.call(this,PREFIX,new BrowserContainer(storage),storage?true:false)}return AbstractBrowserStorage}(SubStorage);var __extends=undefined&&undefined.__extends||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p];function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)};var SessionStorage=function(_super){__extends(SessionStorage,_super);function SessionStorage(){_super.call(this,sessionStorage)}return SessionStorage}(AbstractBrowserStorage);var __extends$2=undefined&&undefined.__extends||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p];function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)};var LocalStorage=function(_super){__extends$2(LocalStorage,_super);function LocalStorage(){_super.call(this,localStorage)}return LocalStorage}(AbstractBrowserStorage);var MemoryStorage=function(){function MemoryStorage(){this._data={}}MemoryStorage.prototype.available=function(){return true};MemoryStorage.prototype.set=function(key,value){if(value===undefined){delete this._data[key]}else{this._data[key]=value}return true};MemoryStorage.prototype.get=function(key){return this._data[key]};MemoryStorage.prototype.exists=function(key){return this._data[key]!==undefined};MemoryStorage.prototype.remove=function(key){delete this._data[key]};MemoryStorage.prototype.removeAll=function(){var keys=Object.keys(this._data);this._data={};return keys};MemoryStorage.prototype.keys=function(){return Object.keys(this._data)};return MemoryStorage}();var __extends$3=undefined&&undefined.__extends||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p];function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)};var ImmutableMemoryStorage=function(_super){__extends$3(ImmutableMemoryStorage,_super);function ImmutableMemoryStorage(){_super.apply(this,arguments)}ImmutableMemoryStorage.prototype.set=function(key,value){if(value!==undefined){value=JSON.stringify(value)}return _super.prototype.set.call(this,key,value)};ImmutableMemoryStorage.prototype.get=function(key){var value=_super.prototype.get.call(this,key);if(value!==undefined){return JSON.parse(value)}return undefined};return ImmutableMemoryStorage}(MemoryStorage);var StorageFactory=function(){function StorageFactory(){this._storages=[]}StorageFactory.getStorage=function(){return new StorageFactory};StorageFactory.prototype.of=function(type){var storage=this._storages[type];if(!storage){switch(type){case StorageType.MEMORY:storage=new MemoryStorage;this._storages[type]=storage;break;case StorageType.IMMUTABLE_MEMORY:storage=new ImmutableMemoryStorage;break;case StorageType.LOCAL:storage=new LocalStorage;break;case StorageType.SESSION:storage=new SessionStorage;break;default:}if(!storage||!storage.available()){throw new Error("Storage type not available")}this._storages[type]=storage}return storage};return StorageFactory}();var PersistenceService=function(){function PersistenceService(){this._emitter=new _angular_core.EventEmitter;this._storage=StorageFactory.getStorage()}PersistenceService.prototype.changes=function(config){if(config===void 0){config={}}var observable=this._emitter.asObservable();if(config.key){observable=observable.filter(function(val){return val.key===config.key})}if(config.type){observable=observable.filter(function(val){return val.type===config.type})}return observable};PersistenceService.prototype.get=function(key,type){if(type===void 0){type=StorageType.MEMORY}var storage=this._getStorage(type);var value=storage.get(key);if(value){var currDate=Date.now();if(value.expireAfter&&value.created+value.expireAfter<currDate){storage.remove(key);this._emitter.emit({key:key,type:type});return undefined}if(value.oneUse){storage.remove(key);this._emitter.emit({key:key,type:type});return value.data}if(value.timeout){if(value.lastAccessed+value.timeout<currDate){storage.remove(key);this._emitter.emit({key:key,type:type});return undefined}else{value.lastAccessed=currDate;storage.set(key,value)}}return value.data}return undefined};PersistenceService.prototype.set=function(key,value,config){if(config===void 0){config={}}if(!config.type){config.type=StorageType.MEMORY}if(!value===undefined){this.remove(key);this._emitter.emit({key:key,type:config.type});return true}var storage=this._getStorage(config.type);var currDate=Date.now();var success=storage.set(key,{data:value,expireAfter:config.expireAfter,timeout:config.timeout,oneUse:config.oneUse?true:false,created:currDate,lastAccessed:currDate});if(success){this._emitter.emit({key:key,type:config.type})}else{storage.remove(key)}return success};PersistenceService.prototype.remove=function(key,type){if(type===void 0){type=StorageType.MEMORY}var storage=this._getStorage(type);var currentItem=this.get(key,type);if(currentItem!==undefined){storage.remove(key);this._emitter.emit({key:key,type:type})}return currentItem};PersistenceService.prototype.removeAll=function(type){var _this=this;if(type===void 0){type=StorageType.MEMORY}var keys=this._getStorage(type).keys();this._getStorage(type).removeAll();keys.forEach(function(key){return _this._emitter.emit({key:key,type:type})})};PersistenceService.prototype.clean=function(type){if(type===void 0){type=StorageType.MEMORY}var storage=this._getStorage(type);var keys=storage.keys();var currDate=Date.now();for(var _i=0,keys_1=keys;_i<keys_1.length;_i++){var key=keys_1[_i];var item=storage.get(key);if(item&&(item.expireAfter&&item.created+item.expireAfter<currDate||item.timeout&&item.lastAccessed+item.timeout<currDate)){this.remove(key)}}};PersistenceService.prototype.defineProperty=function(obj,propName,key,config){var _this=this;if(config===void 0){config={}}var type=config.type||StorageType.MEMORY;Object.defineProperty(obj,propName,{enumerable:true,configurable:true,get:function(){return _this.get(key,type)},set:function(val){_this.set(key,val,config)}})};PersistenceService.prototype.createContainer=function(namespace,config){if(config===void 0){config={}}var thisService=this;var myConfig={oneUse:config.oneUse,expireAfter:config.expireAfter,timeout:config.timeout,type:config.type||StorageType.MEMORY};return new SubStorage(namespace,{get:function(key){return thisService.get(key,myConfig.type)},set:function(key,value){return thisService.set(key,value,myConfig)},remove:function(key){return thisService.remove(key,myConfig.type)},removeAll:function(){return thisService.removeAll()}},true)};PersistenceService.prototype.createCache=function(key,loader,config){if(config===void 0){config={}}var myConfig={type:config.type||StorageType.MEMORY,expireAfter:config.expireAfter,timeout:config.timeout};return new CacheImpl(key,loader,this,myConfig)};PersistenceService.prototype._getStorage=function(type){return this._storage.of(type)};PersistenceService.prototype._calculateExpires=function(maxAge){return maxAge?Date.now()+maxAge:undefined};PersistenceService.decorators=[{type:_angular_core.Injectable}];PersistenceService.ctorParameters=function(){return[]};return PersistenceService}();var AbstractCachedService=function(){function AbstractCachedService(){}AbstractCachedService.prototype.changes=function(){return this.getCache().changes()};AbstractCachedService.prototype.get=function(){return this.getCache().get()};AbstractCachedService.prototype.clear=function(){return this.getCache().clear()};AbstractCachedService.prototype.resolve=function(route,state){return this.get()};AbstractCachedService.prototype.canActivate=function(route,state){return this.get().map(function(val){return val?true:false})};AbstractCachedService.prototype.getCache=function(){};return AbstractCachedService}();var PersistenceModule=function(){function PersistenceModule(){}PersistenceModule.decorators=[{type:_angular_core.NgModule,args:[{providers:[PersistenceService]}]}];PersistenceModule.ctorParameters=function(){return[]};return PersistenceModule}();exports.PersistenceService=PersistenceService;exports.AbstractCachedService=AbstractCachedService;exports.StorageType=StorageType;exports.PersistenceModule=PersistenceModule;Object.defineProperty(exports,"__esModule",{value:true})});