forked from angelnikolov/ts-cacheable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache-buster.decorator.ts
35 lines (33 loc) · 1.21 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
import { Observable, Subject } from 'rxjs';
import { tap } from 'rxjs/operators';
type ICacheable = (...args) => Observable<any>;
export interface ICacheBusterConfig {
/**
* pass a Subject which will emit whenever the inner source stream has emitted
* this can later be subscribed to from Cacheables so they can get rid of their caches
*/
cacheBusterNotifier?: Subject<any>;
}
export function CacheBuster(_cacheBusterConfig?: ICacheBusterConfig) {
return function(
_target: Object,
_propertyKey: string,
propertyDescriptor: TypedPropertyDescriptor<ICacheable>
) {
const _oldMethod = propertyDescriptor.value;
if (propertyDescriptor && propertyDescriptor.value) {
const cacheBusterConfig = _cacheBusterConfig ? _cacheBusterConfig : {};
/* use function instead of an arrow function to keep context of invocation */
(propertyDescriptor.value as any) = function(...parameters) {
return (_oldMethod.call(this, ...parameters) as Observable<any>).pipe(
tap(() => {
if (cacheBusterConfig.cacheBusterNotifier) {
cacheBusterConfig.cacheBusterNotifier.next();
}
})
);
};
}
return propertyDescriptor;
};
}