Mik*_*yte 4 rxjs rxjs-pipeable-operators rxjs-observables
我有两个可观察流,它们执行非常独立的映射逻辑,但最终以以下 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));
Run Code Online (Sandbox Code Playgroud)
我想将最后一个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])
];
Run Code Online (Sandbox Code Playgroud)
但我不确定如何将这些运算符扩展到管道参数中,例如:#
this.selection
.pipe(
// Custom mapping operators
... this.resolveLocationsAndConvertToMarkers
)
.subscribe(markers => this.plotMarkers(markers));
Run Code Online (Sandbox Code Playgroud)
这个错误there are no overloads that expect 3 or 5 arguments...
您可以尝试使用本机 .apply()
this.selection
.pipe.apply(null,this.resolveLocationsAndConvertToMarkers)
Run Code Online (Sandbox Code Playgroud)
或将运算符列表包装在 pipe()
private resolveLocationsAndConvertToMarkers = (devices: String[]) => pipe(
tap(_ => this.devicesLoading = true),
switchMap((devices: string[]) => this.mapService.findLocationForDevices(devices)),
map(loc => marker([loc.latitude, loc.longitude])
);
Run Code Online (Sandbox Code Playgroud)
或返回高阶函数
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])
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
849 次 |
| 最近记录: |