RxjS shareReplay : how to reset its value?

后端 未结 4 534
醉梦人生
醉梦人生 2021-01-05 02:30

I use shareReplay to call only once (like a cache) a webservice to retrieve some informations :

In my service :

getProf         


        
4条回答
  •  失恋的感觉
    2021-01-05 03:14

    Thanks to the answer of martin (which didn't work for me) I found a way to do it :

    protected profile: Observable;
    private refresh$ = new Subject();
    
    constructor() {
        this.profile = this.refresh$.pipe(shareReplay(1));
    
        this.resetProfile();
    }
    
    
    getProfile(): Observable {
        return this.profile;
    }
    
    resetProfile() {
        this.callWS().subscribe(user => {
            this.refresh$.next(user);
        });
    }
    

    I think there is maybe something better/cleaner to do (using a Behavior Subject ?), so if you know something better ..

提交回复
热议问题