Skip to content

Commit 667349a

Browse files
committed
#3 make cache decorators work on getters as well
1 parent 3550e90 commit 667349a

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-method-cache",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"license": "Apache-2.0",
55
"author": {
66
"name": "Poul Kruijt",

src/core/decorator/base-cache.decorator.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ class TestCache {
77

88
public called: number = 0;
99

10+
public getterCalled: number = 0;
11+
12+
@baseCacheDecorator(CacheType.Memory)
13+
get testGetter(): string {
14+
this.getterCalled++;
15+
return 'testGetter';
16+
}
17+
1018
@baseCacheDecorator(CacheType.Memory)
1119
public testMethod(test: number = 0, decrement: number = 0): number {
1220
this.called++;
@@ -31,6 +39,13 @@ describe('Cache decorator is properly set', () => {
3139
expect(testCache.called).toEqual(3);
3240
});
3341

42+
it("should work for a getter", () => {
43+
testCache.testGetter;
44+
testCache.testGetter;
45+
46+
expect(testCache.getterCalled).toEqual(1);
47+
});
48+
3449
it("should return the right value for the right argument(s)", () => {
3550
const value1: number = 5;
3651
const value2: number = 10;

src/core/decorator/base-cache.decorator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ export function baseCacheDecorator<T extends BaseCacheOptions>(cacheType: CacheT
88

99
return (target: object, method: string | symbol, descriptor: PropertyDescriptor): PropertyDescriptor => {
1010

11-
descriptor.value = createCacheDecorator(cacheType, target, descriptor.value!, options as T);
11+
if (descriptor.hasOwnProperty('get')) {
12+
descriptor.get = createCacheDecorator(cacheType, target, descriptor.get!, options as T);
13+
} else if (!descriptor.hasOwnProperty('set')) {
14+
descriptor.value = createCacheDecorator(cacheType, target, descriptor.value!, options as T);
15+
}
1216

1317
return descriptor;
1418

src/core/util/decorator.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {getCacheContainer, getMethodCacheProvider, setCacheContainer} from '../r
77

88
import {createGUID} from './string.util';
99

10-
export function createCacheDecorator(type: CacheType, target: Object, method: Function, options: BaseCacheOptions): Function {
10+
export function createCacheDecorator(type: CacheType, target: Object, method: Function, options: BaseCacheOptions): () => any {
1111

1212
const provider: BaseCacheProvider<BaseCacheObject<BaseCacheOptions>, BaseCacheOptions> = getMethodCacheProvider(type);
1313
const cacheObject: BaseCacheObject<BaseCacheOptions> = provider.getCacheObject(options.key!) || provider.createCacheObject(options);

0 commit comments

Comments
 (0)