-
Notifications
You must be signed in to change notification settings - Fork 42
/
cache-buster.decorator.ts
49 lines (43 loc) · 1.75 KB
/
cache-buster.decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {
DecoratorFactoryTypeOfAnyOrKReturnType,
ICacheBusterConfigOrConfigWithInstant
} from './common/ICacheBusterConfig';
import {bustCache, ICacheable, isInstant} from './common';
import {Observable} from 'rxjs';
import {tap} from 'rxjs/operators';
export function CacheBuster<T extends ICacheBusterConfigOrConfigWithInstant>(cacheBusterConfig?: T)
: DecoratorFactoryTypeOfAnyOrKReturnType<T, ICacheable<Observable<any>>> {
return function (
_target: Object,
_propertyKey: string,
propertyDescriptor: TypedPropertyDescriptor<ICacheable<Observable<any>> | any>
) {
const decoratedMethod = propertyDescriptor.value;
if (propertyDescriptor && propertyDescriptor.value) {
/* use function instead of an arrow function to keep context of invocation */
propertyDescriptor.value = function (...parameters: Array<any>) {
if(isInstant(cacheBusterConfig)) {
bustCache(cacheBusterConfig);
return decoratedMethod.call(this, ...parameters);
}
const decoratedMethodResult = decoratedMethod.call(this, ...parameters);
throwErrorIfResultIsNotObservable(decoratedMethodResult);
return decoratedMethodResult.pipe(
tap(() => {
bustCache(cacheBusterConfig);
})
);
};
};
return propertyDescriptor;
};
};
export const NO_OBSERVABLE_ERROR_MESSAGE = `
Method decorated with @CacheBuster should return observable.
If you don't want to change the method signature, set isInstant flag to true.
`;
export function throwErrorIfResultIsNotObservable(decoratedMethodResult: any): asserts decoratedMethodResult is Observable<any> {
if (decoratedMethodResult instanceof Observable === false) {
throw new Error(NO_OBSERVABLE_ERROR_MESSAGE);
}
}