从以前的结果中清除可观察到的递归

我正在使用expand运算符来递归地调用提供的函数。

this.getPolyline().pipe(
  expand(({ polyline,same }) => same ? this.getPolyline(polyline) : empty()),tap(e => console.log(e))
).subscribe();

并不需要解释发生在组件内部的所有事物,因此为简单起见,我们假设在每个click上发出mapPmVertexAdded$mapPmsnap$的可观察对象。

private getPolyline(line?: L.Polyline): Observable<{polyline: L.Polyline,same: boolean}> {
  return this.leafletService.mapPmVertexAdded$.pipe(
  withLatestFrom(this.leafletService.mapPmsnap$),filter(([{ latlng },{ snapLatLng }]) => latlng.lng === snapLatLng.lng && latlng.lat === snapLatLng.lat),map(([vertex,{ layerInteractedWith }]) => {
    const coords = layerInteractedWith._latlngs.map((latLngs: Array<L.LatLng>) => latLngs.map((latLng: L.LatLng) => [latLng.lat,latLng.lng]));
    coords[0].push(coords[0][0]);
    return L.polyline(coords);
  }),mergeMap((polyline: L.Polyline) => this.comparePolylines(line,polyline))
)}

这就是让我完全不高兴的事情。 首先点击后输出:

从以前的结果中清除可观察到的递归

后的输出,点击:

从以前的结果中清除可观察到的递归

点击第三次后的输出:

从以前的结果中清除可观察到的递归

似乎所有以前的结果都保持不变。我希望流中只有1个元素。我该如何实现这种行为?

ayufly 回答:从以前的结果中清除可观察到的递归

不确定我是否正确理解,添加first()运算符可能会阻止循环中的先前流发出

private getPolyline(line?: L.Polyline): Observable<{polyline: L.Polyline,same: boolean}> {
  return this.leafletService.mapPmVertexAdded$.pipe(
  withLatestFrom(this.leafletService.mapPmSnap$),filter(([{ latlng },{ snapLatLng }]) => latlng.lng === snapLatLng.lng && latlng.lat === snapLatLng.lat),map(([vertex,{ layerInteractedWith }]) => {
    const coords = layerInteractedWith._latlngs.map((latLngs: Array<L.LatLng>) => latLngs.map((latLng: L.LatLng) => [latLng.lat,latLng.lng]));
    coords[0].push(coords[0][0]);
    return L.polyline(coords);
  }),mergeMap((polyline: L.Polyline) => this.comparePolylines(line,polyline)),first()
)}
,

我删除了主管中的条件

this.getPolyline().pipe(
  expand((polyline) => this.getPolyline(polyline)),tap(e => console.log(e))
).subscribe();

并使用take(1)代替first()

本文链接:https://www.f2er.com/3151481.html

大家都在问