angular-persistence
Version:
A library to handle persistence for Angular 2 applications.
97 lines • 3.43 kB
JavaScript
/**
* This is a cache that also implements the <code>CanActivate</code> and <code>Resolve<T></code>
* interfaces in angular so that it can be used as both a provider and a set of guards for Angular
* routing. By implementing the abstract <code>getCache<T></code> method using a cache object,
* this abstract class can form the foundation for a service based off of the persistence framework.
*
* @export
* @abstract
* \@class AbstraceCachedService
* @template T - the type of value returned by this service.
*
* @author Scott O'Bryan
* \@since 1.0
* @abstract
*/
export var AbstractCachedService = (function () {
function AbstractCachedService() {
}
/**
* Returns an {Observable<T>} which will monitor changes to the
* cache over a period of time. This is a hot, multi-value
* observable which will emit the cached value, if one exists,
* when the Observable is first subscribed to. The observer will
* then emit a new event each time the cache changes.
*
* As this is a multi-value observer which is not expected to
* complete, it is the responsiblity of the subscriber to
* unsubscribe in order to prevent potential memory leaks.
*
* the value in the cache changes
* @return {?}
*/
AbstractCachedService.prototype.changes = function () {
return this.getCache().changes();
};
/**
* Returns an {Observable<T>} to a cached value if one is loaded
* or to the value specified by the loader that was supplied when
* this cache was created if it is not.
*
* This Observable is guarenteed to be a single observable which
* means it returns a single value before it completes. As such
* you do not have to unsubscribe from this Observable.
*
* value.
* @return {?}
*/
AbstractCachedService.prototype.get = function () {
return this.getCache().get();
};
/**
* Manually clears the value in the cache forcing a reload.
*
* @abstract
* @return {?}
*/
AbstractCachedService.prototype.clear = function () {
return this.getCache().clear();
};
/**
* Returns the observable to the current cached service for use in the angular Router.
* This is equivalent to the get method but implements the resolve interface for the
* Angular Router.
*
* @param {?} route
* @param {?} state
* @return {?}
*/
AbstractCachedService.prototype.resolve = function (route, state) {
return this.get();
};
/**
* Returns true if the value of the cached observable is "truthy" and false if it is not.
*
*
* \@memberOf AbstraceCachedService
* @param {?} route
* @param {?} state
* @return {?}
*/
AbstractCachedService.prototype.canActivate = function (route, state) {
return this.get().map(function (val) { return val ? true : false; });
};
/**
* Returns a cache that this service will use to return values. The Cache may be obtained
* from the PersistenceService or it may be a custom implementation should one be needed.
*
* @protected
* @abstract
* @template T
* @abstract
* @return {?}
*/
AbstractCachedService.prototype.getCache = function () { };
return AbstractCachedService;
}());
//# sourceMappingURL=persistence.cached_service.js.map