New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ngx-cacheable

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ngx-cacheable - npm Package Compare versions

Comparing version 1.1.7-beta.7 to 1.1.7

19

cacheable.decorator.js

@@ -13,5 +13,5 @@ "use strict";

if (propertyDescriptor && propertyDescriptor.value) {
if (!cacheConfig.storageStrategy) {
cacheConfig.storageStrategy = new common_1.GlobalCacheConfig.storageStrategy();
}
var storageStrategy_1 = !cacheConfig.storageStrategy
? new common_1.GlobalCacheConfig.storageStrategy()
: new cacheConfig.storageStrategy();
var pendingCachePairs_1 = [];

@@ -26,3 +26,3 @@ /**

: rxjs_1.empty()).subscribe(function (_) {
cacheConfig.storageStrategy.removeAll(cacheKey);
storageStrategy_1.removeAll(cacheKey);
pendingCachePairs_1.length = 0;

@@ -39,3 +39,3 @@ });

}
var cachePairs = cacheConfig.storageStrategy.getAll(cacheKey);
var cachePairs = storageStrategy_1.getAll(cacheKey);
var parameters = _parameters.map(function (param) { return param !== undefined ? JSON.parse(JSON.stringify(param)) : param; });

@@ -57,3 +57,3 @@ var _foundCachePair = cachePairs.find(function (cp) {

*/
cacheConfig.storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
storageStrategy_1.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
_foundCachePair = null;

@@ -66,3 +66,3 @@ }

_foundCachePair.created = new Date();
cacheConfig.storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
storageStrategy_1.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
}

@@ -88,3 +88,2 @@ }

/**
* if no maxCacheCount has been passed
* if maxCacheCount has not been passed, just shift the cachePair to make room for the new one

@@ -99,5 +98,5 @@ * if maxCacheCount has been passed, respect that and only shift the cachePairs if the new cachePair will make them exceed the count

cacheConfig.maxCacheCount < cachePairs.length + 1)) {
cacheConfig.storageStrategy.removeAtIndex(0, cacheKey);
storageStrategy_1.removeAtIndex(0, cacheKey);
}
cacheConfig.storageStrategy.add({
storageStrategy_1.add({
parameters: parameters,

@@ -104,0 +103,0 @@ response: response,

@@ -36,3 +36,3 @@ import { Observable } from 'rxjs';

*/
storageStrategy?: IStorageStrategy;
storageStrategy?: new () => IStorageStrategy;
/**

@@ -39,0 +39,0 @@ * property name under which to store the cached pairs for this method

@@ -7,3 +7,3 @@ import { IStorageStrategy } from './IStorageStrategy';

add(cachePair: ICachePair<Observable<any>>): void;
updateAtIndex(): void;
updateAtIndex(index: number, entity: ICachePair<any>): void;
getAll(): ICachePair<Observable<any>>[];

@@ -10,0 +10,0 @@ removeAtIndex(index: number): void;

@@ -25,3 +25,5 @@ "use strict";

;
InMemoryStorageStrategy.prototype.updateAtIndex = function () {
InMemoryStorageStrategy.prototype.updateAtIndex = function (index, entity) {
var updatee = this.cachePairs[index];
Object.assign(updatee, entity);
};

@@ -28,0 +30,0 @@ InMemoryStorageStrategy.prototype.getAll = function () {

{
"name": "ngx-cacheable",
"version": "1.1.7-beta.7",
"description": "Rx Observable cache decorator",
"version": "1.1.7",
"description": "Promise/Observable cache decorators",
"main": "./index.js",

@@ -6,0 +6,0 @@ "types": "./index.d.ts",

@@ -21,5 +21,5 @@ "use strict";

if (propertyDescriptor && propertyDescriptor.value) {
if (!cacheConfig.storageStrategy) {
cacheConfig.storageStrategy = new common_1.GlobalCacheConfig.storageStrategy();
}
var storageStrategy_1 = !cacheConfig.storageStrategy
? new common_1.GlobalCacheConfig.storageStrategy()
: new cacheConfig.storageStrategy();
var pendingCachePairs_1 = [];

@@ -34,3 +34,3 @@ /**

: rxjs_1.empty()).subscribe(function (_) {
cacheConfig.storageStrategy.removeAll(cacheKey);
storageStrategy_1.removeAll(cacheKey);
pendingCachePairs_1.length = 0;

@@ -47,3 +47,3 @@ });

}
var cachePairs = cacheConfig.storageStrategy.getAll(cacheKey);
var cachePairs = storageStrategy_1.getAll(cacheKey);
var parameters = _parameters.map(function (param) { return param !== undefined ? JSON.parse(JSON.stringify(param)) : param; });

@@ -65,3 +65,3 @@ var _foundCachePair = cachePairs.find(function (cp) {

*/
cacheConfig.storageStrategy.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
storageStrategy_1.removeAtIndex(cachePairs.indexOf(_foundCachePair), cacheKey);
_foundCachePair = null;

@@ -74,3 +74,3 @@ }

_foundCachePair.created = new Date();
cacheConfig.storageStrategy.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
storageStrategy_1.updateAtIndex(cachePairs.indexOf(_foundCachePair), _foundCachePair, cacheKey);
}

@@ -87,2 +87,3 @@ }

.then(function (response) {
removeCachePair(pendingCachePairs_1, parameters, cacheConfig);
/**

@@ -99,5 +100,5 @@ * if no maxCacheCount has been passed

cacheConfig.maxCacheCount < cachePairs.length + 1)) {
cacheConfig.storageStrategy.removeAtIndex(0, cacheKey);
storageStrategy_1.removeAtIndex(0, cacheKey);
}
cacheConfig.storageStrategy.add({
storageStrategy_1.add({
parameters: parameters,

@@ -108,3 +109,2 @@ response: response,

}
removeCachePair(pendingCachePairs_1, parameters, cacheConfig);
return response;

@@ -111,0 +111,0 @@ })

@@ -54,2 +54,3 @@ "use strict";

var DOMStorageStrategy_1 = require("../common/DOMStorageStrategy");
var InMemoryStorageStrategy_1 = require("../common/InMemoryStorageStrategy");
var strategies = [null, DOMStorageStrategy_1.DOMStorageStrategy];

@@ -130,2 +131,5 @@ strategies.forEach(function (s) {

};
Service.prototype.getDateWithCustomStorageStrategyProvided = function (parameter) {
return this.mockServiceCall(parameter);
};
__decorate([

@@ -213,2 +217,9 @@ cacheable_decorator_2.Cacheable()

], Service.prototype, "getData3", null);
__decorate([
cacheable_decorator_2.Cacheable({
maxAge: 7500,
slidingExpiration: true,
storageStrategy: InMemoryStorageStrategy_1.InMemoryStorageStrategy
})
], Service.prototype, "getDateWithCustomStorageStrategyProvided", null);
return Service;

@@ -690,2 +701,80 @@ }());

});
it('should work correctly with a custom storage strategy', function () {
var addSpy = spyOn(InMemoryStorageStrategy_1.InMemoryStorageStrategy.prototype, 'add').and.callThrough();
var updateAtIndexSpy = spyOn(InMemoryStorageStrategy_1.InMemoryStorageStrategy.prototype, 'updateAtIndex').and.callThrough();
var getAllSpy = spyOn(InMemoryStorageStrategy_1.InMemoryStorageStrategy.prototype, 'getAll').and.callThrough();
var removeAtIndexSpy = spyOn(InMemoryStorageStrategy_1.InMemoryStorageStrategy.prototype, 'removeAtIndex').and.callThrough();
var removeAllSpy = spyOn(InMemoryStorageStrategy_1.InMemoryStorageStrategy.prototype, 'removeAll').and.callThrough();
jasmine.clock().mockDate();
var asyncFreshData = _timedStreamAsyncAwait(service.getDateWithCustomStorageStrategyProvided('test'), 1000);
// called removeAtIndex once, because of how the cache works, it always removes the last cached pair with this method
expect(removeAtIndexSpy).toHaveBeenCalledTimes(1);
expect(asyncFreshData).toEqual({ payload: 'test' });
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
// one add call, one getAll call
expect(getAllSpy).toHaveBeenCalledTimes(1);
expect(addSpy).toHaveBeenCalledTimes(1);
var cachedResponse = _timedStreamAsyncAwait(service.getDateWithCustomStorageStrategyProvided('test'));
// this call will renew the updateAtIndex call count since it's used to renew the cache
expect(updateAtIndexSpy).toHaveBeenCalledTimes(1);
expect(cachedResponse).toEqual({ payload: 'test' });
/**
* call count should still be one, since we rerouted to cache, instead of service call
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
// two getAll calls because we checked for the cache twice, and only one add call, because cache existed
expect(getAllSpy).toHaveBeenCalledTimes(2);
expect(addSpy).toHaveBeenCalledTimes(1);
/**
* travel through 3000ms of time
*/
jasmine.clock().tick(3000);
/**
* calling the method again should renew expiration for 7500 more milliseconds
*/
service.getDateWithCustomStorageStrategyProvided('test').subscribe();
// this call will renew the updateAtIndex call count since it's used to renew the cache
expect(updateAtIndexSpy).toHaveBeenCalledTimes(2);
// one more getAll cache and it is renewed
expect(getAllSpy).toHaveBeenCalledTimes(3);
expect(addSpy).toHaveBeenCalledTimes(1);
jasmine.clock().tick(4501);
/**
* this should have returned null, if the cache didnt renew
*/
var cachedResponse2 = _timedStreamAsyncAwait(service.getDateWithCustomStorageStrategyProvided('test'));
// this call will renew the updateAtIndex call count since it's used to renew the cache
expect(updateAtIndexSpy).toHaveBeenCalledTimes(3);
// one more getAll call, and still just one add call, since the cache was renewed due to sliding expiration
expect(getAllSpy).toHaveBeenCalledTimes(4);
expect(addSpy).toHaveBeenCalledTimes(1);
expect(cachedResponse2).toEqual({ payload: 'test' });
/**
* call count is still one, because we renewed the cache 4501ms ago
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
/**
* expire cache, shouldn't renew since 7501 ms have ellapsed
*/
jasmine.clock().tick(7501);
var cachedResponse3 = _timedStreamAsyncAwait(service.getDateWithCustomStorageStrategyProvided('test'));
// cache has expired so the currently cached pair should have been swapped by now by calling the removeAtIndex method first
expect(removeAtIndexSpy).toHaveBeenCalledTimes(2);
expect(getAllSpy).toHaveBeenCalledTimes(5);
expect(addSpy).toHaveBeenCalledTimes(1);
/**
* cached has expired, request hasn't returned yet but still - the service was called
*/
expect(cachedResponse3).toEqual(null);
expect(mockServiceCallSpy).toHaveBeenCalledTimes(2);
/**
* call the global cache buster just so we can test if removeAll was called
*/
cacheable_decorator_1.globalCacheBusterNotifier.next();
expect(addSpy).toHaveBeenCalledTimes(1);
expect(updateAtIndexSpy).toHaveBeenCalledTimes(3);
expect(getAllSpy).toHaveBeenCalledTimes(5);
expect(removeAtIndexSpy).toHaveBeenCalledTimes(2);
expect(removeAllSpy).toHaveBeenCalled();
});
});

@@ -692,0 +781,0 @@ function _timedStreamAsyncAwait(stream$, skipTime) {

@@ -51,2 +51,3 @@ "use strict";

var DOMStorageStrategy_1 = require("../common/DOMStorageStrategy");
var InMemoryStorageStrategy_1 = require("../common/InMemoryStorageStrategy");
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;

@@ -137,2 +138,5 @@ var strategies = [null, DOMStorageStrategy_1.DOMStorageStrategy];

};
Service.prototype.getDateWithCustomStorageStrategyProvided = function (parameter) {
return this.mockServiceCall(parameter);
};
__decorate([

@@ -215,2 +219,9 @@ promise_cacheable_decorator_1.PCacheable()

], Service.prototype, "getData3", null);
__decorate([
promise_cacheable_decorator_1.PCacheable({
maxAge: 400,
slidingExpiration: true,
storageStrategy: InMemoryStorageStrategy_1.InMemoryStorageStrategy
})
], Service.prototype, "getDateWithCustomStorageStrategyProvided", null);
return Service;

@@ -701,4 +712,41 @@ }());

}); });
it('should work correctly with a custom storage strategy', function (done) { return __awaiter(_this, void 0, void 0, function () {
var getAllSpy, asyncFreshData, cachedResponse;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
getAllSpy = spyOn(InMemoryStorageStrategy_1.InMemoryStorageStrategy.prototype, 'getAll').and.callThrough();
return [4 /*yield*/, service.getDateWithCustomStorageStrategyProvided('test')];
case 1:
asyncFreshData = _a.sent();
expect(asyncFreshData).toEqual({ payload: 'test' });
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
// one add call, one getAll call
expect(getAllSpy).toHaveBeenCalledTimes(1);
return [4 /*yield*/, service.getDateWithCustomStorageStrategyProvided('test')];
case 2:
cachedResponse = _a.sent();
expect(cachedResponse).toEqual({ payload: 'test' });
expect(getAllSpy).toHaveBeenCalledTimes(2);
/**
* call count should still be one, since we rerouted to cache, instead of service call
*/
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
setTimeout(function () {
service.getDateWithCustomStorageStrategyProvided('test');
expect(mockServiceCallSpy).toHaveBeenCalledTimes(1);
// three getAll calls since every time we call the decorated method, we check the cache first
expect(getAllSpy).toHaveBeenCalledTimes(3);
setTimeout(function () {
service.getDateWithCustomStorageStrategyProvided('test');
expect(mockServiceCallSpy).toHaveBeenCalledTimes(2);
done();
}, 500);
}, 200);
return [2 /*return*/];
}
});
}); });
});
});
//# sourceMappingURL=promise-cacheable.decorator.spec.js.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc