与switchMap一起使用时,concatMap仅被调用一次

时间:2020-02-03 11:08:20

标签: angular typescript rxjs

先决条件:

dishService.query :返回Observable(菜肴清单);每道菜都具有latlng属性

geoLocationService.getDistanceFromLatLng$ :返回Observable,传递时的距离:latlng坐标;

代码:

this.pageSubject.pipe(
  switchMap((filter: DishFilter)=>this.dishService.query(filter)),//gets a list of dishes
  tap(_=>console.log("after switchmap", _)
  concatMap(page=>combineLatest(page.results.map(dish=>
        this.geoLocationService.getDistanceFromLatLng$({lat:  user.current_location.lat, lng:  user.current_location.lng}, {lat: dish.restaurant_lat, lng: dish.restaurant_lng}).pipe(
          map(distance=>dish.distance = distance),
          tap(_=>console.log("after distance", _))
        )
      )
    ), (page, results) => {
        return page
    }
  ),//for each dish attach a  distance property using geoLocationService.getDistanceFromLatLng$;Observable
  tap(=>console.log("after concatmap")),
).subscribe((page: Page)=>{
  //use page somewhere
  console.log("completed")
  this.cdRef.detectChanges();
});

我做到了:

this.pageSubject.next(filter);//logs to console: after switchmap, after distance..., after concatmap, completed
this.pageSubject.next(filter);//logs to console: after switchmap

在第二次调用pageSubject.next(filter)时,concatMap不执行。为什么?

0 个答案:

没有答案