RxJs Observable duplicate values

SeaBiscuit

So i have pretty straight forward scenario. One subject and observable. When client logs in i publish success, when user logs out i publish false.

Problem is in subscribe method in LoginComponent First time everything works great. User logs in i get one event, but after that when user logs out second time and logs in again i get 2 same events, again if user logs out and then logs in i get 3 duplicate events and so on.

AuthService.ts

public _loggedIn: Subject<LoggedInOrResetPassword> = new Subject();
public loggedId: Observable<LoggedInOrResetPassword> = this._loggedIn.asObservable();


obtainAccessToken(){
 // ommitted
 this.httpClient.post(environment.baseUrl + url, null, requestOptions)
                .subscribe(data => {
                  this.saveToken(data);
                  this._loggedIn.next(LoggedInOrResetPassword.createTrue());
                });
  // ommitted
}


private logout(navigateTo?: string){
    this._loggedIn.next(LoggedInOrResetPassword.createFalse());
}

LoginComponent.ts

ngOnInit() {
    this.authservice.loggedId.subscribe( ( loggedInOrResetPassword: LoggedInOrResetPassword ) => {
    // HERE I GET DUPLICATE VALUES
});
Picci

The reason is that you are NOT unsubscribing when LoginComponent is destroyed.

Your code should be changed as follows

First add an instance property to LoginComponent to store the subscription, such as

export class LoginComponent implements OnInit, OnDestroy {
  .....
  loginSubscription: Subscription;
  .....
}

Then change ngOnInit so that you store the subscription in the newly added property

ngOnInit() {
    this.loginSubscription = this.authservice.loggedId.subscribe( ( loggedInOrResetPassword: LoggedInOrResetPassword ) => {
    // HERE I GET DUPLICATE VALUES
});

Eventually add ngOnDestroy to make sure you unsubscribe when the component gets destroyed

ngOnDestroy {
  if (this.loginSubscription) {
    this.loginSubscription.unsubscribe();
  }
}

Take a look at the async pipe of Angular as an alternative method to subscribe to Observables and automatically unsubscribe.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

rxjs pairwise is emitting duplicate values

RxJava : observable emitting duplicate values

Delay time between Observable values - RxJS

Separate observable values by specific amount of time in RxJS

RXJS Observable Transform Array To Multiple Values

RxJS get before/after emission values of Observable

RxJs - Join result of observable with other asynchronous values

RxJs: distinctUntilChanged still emits duplicate values

RXJS: Skipping values from observable triggered by other observable

rxjs join and filter an observable based on values from a different observable

How to filter duplicate values emitted by observable in RXJava?

RXJS observable in observable

RxJs Observable created from Iterable of promises not updating values each iteration

rxjs - buffer emitted values until other observable emits, then emit as normal

Rxjs Observable array select between given start and end values

Get difference between last two values of an RxJS Observable

How get two or more values as an array from observable rxjs?

How To Update values in HttpParams in Angular Post Request with rxjs Observable

Difference in importing Observable from 'rxjs/Observable' and 'rxjs'

RxJs Observable in Observable from Array

Angular / RxJS Multicasting Observable of Observable

Import of Observable rxjs/Observable is not working

Loop back observable in RxJS?

Is observable and promise compatible in rxjs?

RXJS Create Observable for sockets

RXJS control observable invocation

Angular/RxJS: synchronous observable

rxjs observable reusing logic

RxJS Observable: Subscription lost?

TOP Ranking

HotTag

Archive