ngx-cacheable
Advanced tools
Comparing version 1.0.8 to 1.0.9
@@ -1,2 +0,3 @@ | ||
import { Observable } from 'rxjs'; | ||
import { Observable, Subject } from 'rxjs'; | ||
export declare const globalCacheBusterNotifier: Subject<void>; | ||
export declare type ICacheRequestResolver = (oldParameters: Array<any>, newParameters: Array<any>) => boolean; | ||
@@ -3,0 +4,0 @@ export declare type IShouldCacheDecider = (response: any) => boolean; |
@@ -8,2 +8,3 @@ "use strict"; | ||
}; | ||
exports.globalCacheBusterNotifier = new rxjs_1.Subject(); | ||
function Cacheable(_cacheConfig) { | ||
@@ -16,11 +17,11 @@ return function (_target, _propertyKey, propertyDescriptor) { | ||
var cacheConfig_1 = _cacheConfig ? _cacheConfig : {}; | ||
if (cacheConfig_1.cacheBusterObserver) { | ||
/** | ||
* subscribe to the cacheBusterObserver and upon emission, clear all caches | ||
*/ | ||
cacheConfig_1.cacheBusterObserver.subscribe(function (_) { | ||
_cachePairs_1.length = 0; | ||
_observableCachePairs_1.length = 0; | ||
}); | ||
} | ||
/** | ||
* subscribe to the globalCacheBuster | ||
* if a custom cacheBusterObserver is passed, subscribe to it as well | ||
* subscribe to the cacheBusterObserver and upon emission, clear all caches | ||
*/ | ||
rxjs_1.merge(exports.globalCacheBusterNotifier.asObservable(), cacheConfig_1.cacheBusterObserver ? cacheConfig_1.cacheBusterObserver : rxjs_1.empty()).subscribe(function (_) { | ||
_cachePairs_1.length = 0; | ||
_observableCachePairs_1.length = 0; | ||
}); | ||
cacheConfig_1.cacheResolver = cacheConfig_1.cacheResolver | ||
@@ -27,0 +28,0 @@ ? cacheConfig_1.cacheResolver |
@@ -586,2 +586,71 @@ "use strict"; | ||
}); | ||
it('should clear all caches when the global cache buster is called', function () { | ||
/** | ||
* set up a service with multiple cached methods | ||
*/ | ||
var Service = /** @class */ (function () { | ||
function Service() { | ||
} | ||
Service.prototype.mockServiceCall = function (parameter) { | ||
return rxjs_1.timer(1000).pipe(operators_1.mapTo({ payload: parameter })); | ||
}; | ||
Service.prototype.getData1 = function (parameter) { | ||
return this.mockServiceCall(parameter); | ||
}; | ||
Service.prototype.getData2 = function (parameter) { | ||
return this.mockServiceCall(parameter); | ||
}; | ||
Service.prototype.getData3 = function (parameter) { | ||
return this.mockServiceCall(parameter); | ||
}; | ||
__decorate([ | ||
cacheable_decorator_1.Cacheable() | ||
], Service.prototype, "getData1", null); | ||
__decorate([ | ||
cacheable_decorator_1.Cacheable() | ||
], Service.prototype, "getData2", null); | ||
__decorate([ | ||
cacheable_decorator_1.Cacheable() | ||
], Service.prototype, "getData3", null); | ||
return Service; | ||
}()); | ||
var service = new Service(); | ||
mockServiceCallSpy = spyOn(service, 'mockServiceCall').and.callThrough(); | ||
/** | ||
* call the first method and cache it | ||
*/ | ||
service.getData1('test1'); | ||
var asyncFreshData1 = _timedStreamAsyncAwait(service.getData1('test1'), 1000); | ||
expect(asyncFreshData1).toEqual({ payload: 'test1' }); | ||
var cachedResponse1 = _timedStreamAsyncAwait(service.getData1('test1')); | ||
expect(cachedResponse1).toEqual({ payload: 'test1' }); | ||
/** | ||
* even though we called getData1 twice, this should only be called once | ||
* since the second call went straight to the cache | ||
*/ | ||
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1); | ||
service.getData2('test2'); | ||
var asyncFreshData2 = _timedStreamAsyncAwait(service.getData2('test2'), 1000); | ||
expect(asyncFreshData2).toEqual({ payload: 'test2' }); | ||
var cachedResponse2 = _timedStreamAsyncAwait(service.getData2('test2')); | ||
expect(cachedResponse2).toEqual({ payload: 'test2' }); | ||
expect(mockServiceCallSpy).toHaveBeenCalledTimes(2); | ||
service.getData3('test3'); | ||
var asyncFreshData3 = _timedStreamAsyncAwait(service.getData3('test3'), 1000); | ||
expect(asyncFreshData3).toEqual({ payload: 'test3' }); | ||
var cachedResponse3 = _timedStreamAsyncAwait(service.getData3('test3')); | ||
expect(cachedResponse3).toEqual({ payload: 'test3' }); | ||
expect(mockServiceCallSpy).toHaveBeenCalledTimes(3); | ||
/** | ||
* bust all caches | ||
*/ | ||
cacheable_decorator_1.globalCacheBusterNotifier.next(); | ||
_timedStreamAsyncAwait(service.getData1('test1'), 1000); | ||
_timedStreamAsyncAwait(service.getData2('test2'), 1000); | ||
_timedStreamAsyncAwait(service.getData3('test3'), 1000); | ||
/** | ||
* if we didn't bust the cache, this would've been 3 | ||
*/ | ||
expect(mockServiceCallSpy).toHaveBeenCalledTimes(6); | ||
}); | ||
}); | ||
@@ -588,0 +657,0 @@ function _timedStreamAsyncAwait(stream$, skipTime) { |
{ | ||
"name": "ngx-cacheable", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "Rx Observable cache decorator", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102320
1812