[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use

in #utopian-io4 years ago

Project Information

Repository: https://github.com/nirvanaitsolutions/swapsteem
Project Name: SwapSteem

Describe the bug
Swapsteem code uses RxJS to subscribe to changes in data. RxJS in-turn use Observable which are Infinite Subscriptions. This means that the code will execute even without a subscribe being called unlike http.get where call will only be made when you subscribe to it. This causes a performance and memory hog as you exit and re-enter a controller because you are subscribing to same service multiple times. You can find more details in the stack overflow discussion here: https://stackoverflow.com/a/41177163/1141542

To Reproduce
This problem can be observed in dozens of places throughout the code. Below is a discussion on one such scenario and a fix for the same.

You can clone the code using following command to check on your local machine. Git required

git clone https://github.com/nirvanaitsolutions/swapsteem.git

terms-and-conditions.component.ts

  this.auth.getUserData().subscribe((scAuthService) => {
        this.ngxService.stop();
        if (scAuthService) {
          this.auth.userData = scAuthService;
        }
        this.dialogRef.close();
      })

Expected behavior
Above code can be re-written as.

  this.auth.getUserData().pipe(takeWhile(() => this.isAlive)).subscribe((scAuthService) => {
        this.ngxService.stop();
        if (scAuthService) {
          this.auth.userData = scAuthService;
        }
        this.dialogRef.close();
      })

When controller destroys set isAlive to false

  ngOnDestroy() {
    this.isAlive = false;
  }

GitHub Account : https://github.com/mightypanda-x
GitHub Issue : https://github.com/nirvanaitsolutions/swapsteem/issues/147