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

问题描述 投票:0回答:2

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

  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 ..

rxjs rxjs-pipeable-operators rxjs-observables
2个回答
0
投票

您可以尝试使用本机.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])
      );

0
投票

您可以尝试一种反应性方法(除非真正隔离,否则没有副作用):

© www.soinside.com 2019 - 2024. All rights reserved.