-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathpromise.cache-buster.decorator.ts
47 lines (41 loc) · 1.68 KB
/
promise.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
import {
DecoratorFactoryTypeOfAnyOrKReturnType, ICacheBusterConfigOrConfigWithInstant
} from './common/ICacheBusterConfig';
import {bustCache, ICacheable, isInstant} from './common';
export function PCacheBuster<T extends ICacheBusterConfigOrConfigWithInstant>(cacheBusterConfig?: T)
: DecoratorFactoryTypeOfAnyOrKReturnType<T, ICacheable<Promise<any>>> {
return function (
_target: Object,
_propertyKey: string,
propertyDescriptor: TypedPropertyDescriptor<ICacheable<Promise<any>>>
) {
const decoratedMethod = propertyDescriptor.value;
if (propertyDescriptor && propertyDescriptor.value) {
/* use function instead of an arrow function to keep context of invocation */
(propertyDescriptor.value as any) = function (...parameters: Array<any>) {
if(isInstant(cacheBusterConfig)){
bustCache(cacheBusterConfig);
return decoratedMethod.call(this, ...parameters);
}
const decoratedMethodResult = decoratedMethod.call(this, ...parameters)
throwErrorIfResultIsNotPromise(decoratedMethodResult);
return decoratedMethodResult.then(
response => {
bustCache(cacheBusterConfig)
return response;
}
);
};
};
return propertyDescriptor;
};
};
export const NO_PROMISE_ERROR_MESSAGE = `
Method decorated with @CacheBuster should return Promise.
If you don't want to change the method signature, set isInstant flag to true.
`;
function throwErrorIfResultIsNotPromise(decoratedMethodResult: any): asserts decoratedMethodResult is Promise<any> {
if (decoratedMethodResult instanceof Promise === false) {
throw new Error(NO_PROMISE_ERROR_MESSAGE);
}
}