angelnikolov / ts-cacheable

Observable/Promise Cache Decorator
https://npmjs.com/package/ts-cacheable
ISC License
340 stars 42 forks source link

reload data when cacheBusterNotifier is called #58

Closed williamxsp closed 4 years ago

williamxsp commented 4 years ago

When I save some data Id like that my informations get updated automatically. Im doing the following:

get(pessoaId): Observable<Array<VideoModel>> {
        return merge(of([]), cacheBuster$).pipe(
            switchMap(() => {
                return this.http.get(`${this.baseUrl}/${pessoaId}`);
            })
        );
    }

 @CacheBuster({
        cacheBusterNotifier: cacheBuster$
    })
    add(video: VideoModel): Observable<VideoModel> {}

But im not sure if this is the best way to accomplish this. Do you have any idea?

angelnikolov commented 4 years ago

Hey @williamxsp. Thanks for the question. You are on the right path, with the cache buster. What you need to do so the cache is evicted successfully, is to just decorate your get method like:

@Cacheable({
    cacheBusterObserver: cacheBuster$.asObservable()
})
get(pessoaId): Observable<Array<VideoModel>> {
        return of([]).pipe(
            switchMap(() => {
                return this.http.get(`${this.baseUrl}/${pessoaId}`);
            })
        );
    }

Also, is there a reason that you need this of([]) and a switchMap?

angelnikolov commented 4 years ago

@williamxsp 🏓

williamxsp commented 4 years ago

Thanks @angelnikolov . Sorry for the late reply :)