简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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