I need to implement a functionality to skip or ignore the subscription on the fromEvent() observable when a specific condition is met: if
this.currentPosition === this.vc.getScrollPosition()[1]
, I do not want to subscribe to the observable. This is because the scroll action is automatically moving to the next item, and my intention is to wait for user interaction before scrolling to the next item on the 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) {
// remain in the same position if it's the last item
this.vc.scrollToPosition([0, current.position]);
this.currentPosition = current.position;
} else {
// move to the 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;
}
}
);