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)?