0

I need some help with an rxjs subscription, can you help me understand this little thing called "rewrite" subscription?

For instance, I have a calendar slider.

On the lifecycle hook OnInit I'm getting the default value like this:

 let today = new Date();
let monday = this.datePipe.transform(this.getMonday(today), 'yyyy-MM-dd');
let changedMonday = this.getMonday(today);
let weekDay = this.datePipe.transform(new Date(changedMonday.setDate(changedMonday.getDate() + 6)), 'yyyy-MM-dd');
this.getCalendarPeriodSubscr = this.projectsService.getCalendarPeriod(monday, weekDay)
  .subscribe((dates: DateType[])=> {
    this.datesFullPeriod = dates;
    this.datesPeriod = this.checkWeekDays(dates)
  })

Then I need to get a new value from the server with another http request, like this:

  goNextDate(){
console.log('go next date')
let sunday = new Date(this.datesFullPeriod[this.datesFullPeriod.length - 1].date);
let from = this.datePipe.transform(new Date(sunday.setDate(sunday.getDate() + 1)), 'yyyy-MM-dd');
let endDate = new Date(from)
let to = this.datePipe.transform(new Date(endDate.setDate(endDate.getDate() + 6)), 'yyyy-MM-dd');
this.getCalendarPeriodSubscr = this.projectsService.getCalendarPeriod(from, to)
  .subscribe((dates: DateType[])=> {
    this.datesFullPeriod = dates;
    this.datesPeriod = this.checkWeekDays(dates)
  })

}

So, as you can see, I rewrite getCalendarPeriodSubscr for another subscription (type of getCalendarPeriodSubscr is Subscription).

I know something about .share() or push to global Subscription[] or using a Subject for the next unsubscribe. But I don't understand as I really need something like these.

All of this are helping me to unsubscribe from it once.

So please, does anyone know, will "rewrite" give me what I want like 1 subscription for all of these http requests (I'm not sure if that 1st and next subscriptions, except the last, are destroyed when rewriting the previous)?

ProgrammerPer
  • 1,125
  • 1
  • 11
  • 26
Den Kerny
  • 562
  • 10
  • 18
  • 1
    Since the observables you are using are what they call cold observables you don't need to unsubscribe. https://stackoverflow.com/questions/42000535/should-i-unsubscribe-from-cold-observable. Regarding the question asked, no they are not destroyed but in your case it also doesnt matter since they clean themselves up – Jelle Jan 21 '19 at 16:57
  • thx for the answer, now ive deleted subscription cause of http in my case will complete observable. These prev answers help me undestanding much better cold\hot observables and gave me the reason to search it deeper: https://stackoverflow.com/a/48516454/9928564 https://stackoverflow.com/a/42000843/9928564 https://stackoverflow.com/a/51850733/9928564 – Den Kerny Jan 22 '19 at 10:57

1 Answers1

0

Removing your local reference will not destroy the notifier's reference. (Maybe)

(Edit) I missed the detail @jelle pointed out where cold observables complete themselves- in your case with http, that answer is correct.

So the way subscriptions are supposed to be destroyed is to unsubscribe them so the notifier does not attempt to notify them something has changed. A misunderstanding with Subscriptions is that when the local reference is used up they disappear, however, the notifier that was subscribed to is still carrying a reference to that subscription as well, hence the need for unsubscribe to remove the reference from the notifier.

https://brianflove.com/2018/03/04/rxjs-the-basics/

  • so everything i need is to unsubsribe from it in goNextDate(), then subscribe right here , or (what is better approach) do not write it in variable for subscription and just subscribe to it, like it desctibed here https://stackoverflow.com/a/42000843/9928564 ? – Den Kerny Jan 22 '19 at 08:58
  • I upvoted the comment from @jelle for the specific reference to observables from http- I missed that detail. If this wasn't that case, you would check to see if the subscription exists, then unsubscribe before resigning. Also going to update my answer. –  Jan 22 '19 at 11:54