简体   繁体   English

在特定条件下跳过/忽略来自事件订阅的 Observable

[英]Skip/Ignore Observable fromEvent subscription on a specific condition

I would like to skip/ignore subscription on fromEvent() observable when a condition is true: when this.currentPosition === this.vc.getScrollPosition()[1] I don't want to subscribe the observable because it's scrolling to next items directly and my goal is to wait for user interaction to scroll to the next item on my interface..当条件为真时,我想跳过/忽略fromEvent() 可观察对象订阅:当this.currentPosition === this.vc.getScrollPosition()[1]我不想订阅可观察对象,因为它正在滚动到下一个直接项目,我的目标是等待用户交互滚动到我界面上的下一个项目..

fromEvent(window, 'scroll').pipe(
    distinctUntilChanged(),
    debounceTime(1000)
)
    .subscribe(
        (event) => {

            const current = this.positions.find(e => e.name === this.currentSectionName);
            const indexOfCurrent = this.positions.indexOf(current);

            if (indexOfCurrent + 1 === this.positions.length) {
                // stay in the same position if it's the last item
                this.vc.scrollToPosition([0, current.position]);
                this.currentPosition = current.position;

            } else {
                // move to next position if it's not the last item
                const next = this.positions[indexOfCurrent + 1];
                this.vc.scrollToPosition([0, next.position]);
                this.currentPosition = next.position;
            }
        }

    );

You can filter it in pipe.您可以在 pipe 中对其进行过滤。

fromEvent(window, 'scroll').pipe(
    filter( () => this.currentPosition !== this.vc.getScrollPosition()[1] ),
    distinctUntilChanged(),
    debounceTime(1000)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM