我正在使用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个元素。我该如何实现这种行为?