this.route.params
が破棄され、this.sub
が登録解除された後、はログを記録し続けます。これは私の直感に反して、1つのサブスクリプションでは、契約解除後にすべてが停止するはずです。ここで何が起きているのですか&それに対処する最も良い方法は何ですか? takeUntilを追加するか、concatMap以外のものを使用すべきですか? (私は新しい写真のポーリングよ)RXJSで観測可能な内部観測オブザーバブルの受信可
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'))
編集:私はそれをきれいにするために書き換えてみました、それは最初の呼び出しの後に動作を停止した理由を、 concatMapと、次は、唯一の呼び出しが、私はわからないん
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'))
とswitchMapで次がそうでなければ、彼らはお互いをキャンセルする限り、API呼び出しが速く3000よりも実行することができますように動作します
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'))
私はなぜSWIのように好奇心が強いですtchMap(とmergemap)は毎回動作しましたが、concatMapは一度しか動作しませんでしたが、RXJSのバグのようです
私はmergeMapを使うことに決めました。要求が間違っているしばしば、そしてもしそうなら、それは数秒後にそれ自身を修正するでしょう。
https://stackoverflow.com/questions/35765322/angular2-switchmap-not-canceling-previous-http-calls - switchmapは、同じ基本ストリーム内のリクエストのみをキャンセルできるように見えます。 - 内側に伝播しません。 –