After this.route.params
is destroyed and this.sub
is unsubscribed, "interval run"
keeps logging. This was against my intuition that with one subscription, after an unsubscribe, everything should stop happening. What's happening here & what's the best way to deal with it? Should I add a takeUntil, or should I use something other than concatMap? (i'm polling for new photos)
constructor(private authService:AuthService,
private route:ActivatedRoute,
private router:Router,
private photosApi:PhotosApi) {
}
ngOnInit() {
this.sub = this.route.params
.switchMap(()=>{
console.log('switchmap Ruun')
return Observable.interval(2000).startWith(0).concatMap(()=>{
console.log('interval run')
return this.photosApi.apiPhotosGet(this.authService.getAuth(), 0, 40)
})
})
.subscribe((data:PagedResultPatientPhotoModel) => {
this.processPhotos(data)
}).add(()=>console.log('unsubscribe happened'))
edit: I tried rewriting to make it cleaner, the following with concatMap only does one call, i'm not sure why it stops working after the first call:
this.intervalSub = Observable.interval(3000).startWith(0).concatMap(()=>{
console.log('getting photos interval', this.route.params)
return this.route.params.switchMap(()=>{
console.log('getting photos')
return this.photosApi.apiPhotosGet(this.authService.getAuth(), 0, 40)
})
}).subscribe((data:PagedResultPatientPhotoModel) => {
this.processPhotos(data)
}).add(()=>console.log('unsubscribe happened'))
and the following with switchMap works as long as the api calls can run faster than 3000, otherwise they cancel each other:
this.intervalSub = Observable.interval(3000).startWith(0).switchMap(()=>{
console.log('getting photos interval', this.route.params)
return this.route.params.switchMap(()=>{
console.log('getting photos')
return this.photosApi.apiPhotosGet(this.authService.getAuth(), 0, 40)
})
}).subscribe((data:PagedResultPatientPhotoModel) => {
this.processPhotos(data)
}).add(()=>console.log('unsubscribe happened'))
i'm curious as to why switchMap (and mergemap) worked each time but concatMap only worked once, seems like a bug in RXJS
I've decided to go with mergeMap since i feel like it's unlikely for the order the requests come back in to be wrong very often, and if so, it will correct itself after a few seconds.