gpt4 book ai didi

RxJs:您可以将运算符作为参数传播到管道运算符中吗

转载 作者:行者123 更新时间:2023-12-03 22:00:12 25 4
gpt4 key购买 nike

我有两个可观察的流,它们执行非常独立的映射逻辑,但最终以以下 3 个运算符结束:

  this.selection
.pipe(
..Custom mapping operators
tap(_ => this.devicesLoading = true),
switchMap(d => this.mapService.findLocationForDevices(d)),
map(loc => marker([loc.latitude, loc.longitude])
)
.subscribe(markers => this.plotMarkers(markers));

我要移动最后一个 tap, switchMap, map运算符到一个通用函数,所以我可以在我的两个可观察流中应用它们。

我想过这样做:
  private resolveLocationsAndConvertToMarkers = (devices: String[]) => [
tap(_ => this.devicesLoading = true),
switchMap((devices: string[]) => this.mapService.findLocationForDevices(devices)),
map(loc => marker([loc.latitude, loc.longitude])
];

但我不确定如何将这些运算符传播到管道参数中,例如:#
      this.selection
.pipe(
// Custom mapping operators
... this.resolveLocationsAndConvertToMarkers
)
.subscribe(markers => this.plotMarkers(markers));

这个错误 there are no overloads that expect 3 or 5 arguments ..

最佳答案

您可以尝试使用原生 .apply()

this.selection
.pipe.apply(null,this.resolveLocationsAndConvertToMarkers)

或将运算符列表包装在 pipe()
  private resolveLocationsAndConvertToMarkers = (devices: String[]) => pipe(
tap(_ => this.devicesLoading = true),
switchMap((devices: string[]) => this.mapService.findLocationForDevices(devices)),
map(loc => marker([loc.latitude, loc.longitude])
);

或返回高阶函数
private resolveLocationsAndConvertToMarkers = (devices: String[]) => source=>source.pipe(
tap(_ => this.devicesLoading = true),
switchMap((devices: string[]) => this.mapService.findLocationForDevices(devices)),
map(loc => marker([loc.latitude, loc.longitude])
);

关于RxJs:您可以将运算符作为参数传播到管道运算符中吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60149866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com