CW1
CW1

Reputation: 331

Observable unsubscription not working angular

I am making interval calls with Observables in this way:

makeIntervalCall(){
  this.subscription = Observable
  .interval(3000)
  .do( dispatch to service* )
  .subscribe()

  if (status.something coming from ngrx store) {

    this.subscription.unsubscribe();
    console.log('interval killed')
  }

}

This works and my polling stops but I now have a requirement to reuse the same Observable sequence for other calls. Again, this works fine but I see that the Observable keeps duplicating itself. I have placed a console log on the unsubscribe and instead of calling itself once its duplicated everytime. To be clear... by the 10th use of calling the sequence it is logged out 10 times where as i would expect it to destroy itself when i call unsubscribe and create a single new stream when i call it again. Why would unsubscribe not work? is there an alternative to destroying the observable and starting fresh everytime i call it.

Upvotes: 1

Views: 46

Answers (1)

CW1
CW1

Reputation: 331

Ok I just figured out what I was doing wrong... I am using ngrx/store.. within the subscriptions I have an outer ring that is my this.store.select statement which provides the mechanism to unsubscribe. The problem was that i was not storing that subscription itself and unsubscribing internally :(

Upvotes: 1

Related Questions