From 9963f8f005361f190daaf9cbe8a1c1a61d030851 Mon Sep 17 00:00:00 2001 From: arturovt Date: Tue, 22 Mar 2022 05:02:33 +0200 Subject: [PATCH] perf: mark `scroll` listeners as passive since they are never prevented --- packages/gantt/src/gantt-dom.service.ts | 57 +++++++++++++++---------- packages/gantt/src/root.component.ts | 18 ++++---- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/packages/gantt/src/gantt-dom.service.ts b/packages/gantt/src/gantt-dom.service.ts index eacf9e8e..a71d3864 100644 --- a/packages/gantt/src/gantt-dom.service.ts +++ b/packages/gantt/src/gantt-dom.service.ts @@ -39,7 +39,7 @@ export class GanttDomService implements OnDestroy { private monitorScrollChange() { this.ngZone.runOutsideAngular(() => - merge(fromEvent(this.mainContainer, 'scroll'), fromEvent(this.sideContainer, 'scroll')) + merge(fromEvent(this.mainContainer, 'scroll', { passive: true }), fromEvent(this.sideContainer, 'scroll', { passive: true })) .pipe(takeUntil(this.unsubscribe$)) .subscribe((event) => { this.syncScroll(event); @@ -96,27 +96,40 @@ export class GanttDomService implements OnDestroy { this.disableBrowserWheelEvent(); } - getViewerScroll() { - return fromEvent(this.mainContainer, 'scroll').pipe( - map(() => this.mainContainer.scrollLeft), - pairwise(), - map(([previous, current]) => { - const event: ScrollEvent = { - target: this.mainContainer, - direction: ScrollDirection.NONE - }; - if (current - previous < 0) { - if (this.mainContainer.scrollLeft < scrollThreshold && this.mainContainer.scrollLeft > 0) { - event.direction = ScrollDirection.LEFT; - } - } - if (current - previous > 0) { - if (this.mainContainer.scrollWidth - this.mainContainer.clientWidth - this.mainContainer.scrollLeft < scrollThreshold) { - event.direction = ScrollDirection.RIGHT; - } - } - return event; - }) + /** + * @returns An observable that will emit outside the Angular zone. Note, consumers should re-enter the Angular zone + * to run the change detection if needed. + */ + getViewerScroll(options?: AddEventListenerOptions): Observable { + return new Observable((subscriber) => + this.ngZone.runOutsideAngular(() => + fromEvent(this.mainContainer, 'scroll', options) + .pipe( + map(() => this.mainContainer.scrollLeft), + pairwise(), + map(([previous, current]) => { + const event: ScrollEvent = { + target: this.mainContainer, + direction: ScrollDirection.NONE + }; + if (current - previous < 0) { + if (this.mainContainer.scrollLeft < scrollThreshold && this.mainContainer.scrollLeft > 0) { + event.direction = ScrollDirection.LEFT; + } + } + if (current - previous > 0) { + if ( + this.mainContainer.scrollWidth - this.mainContainer.clientWidth - this.mainContainer.scrollLeft < + scrollThreshold + ) { + event.direction = ScrollDirection.RIGHT; + } + } + return event; + }) + ) + .subscribe(subscriber) + ) ); } diff --git a/packages/gantt/src/root.component.ts b/packages/gantt/src/root.component.ts index 8a4f32e4..f2ac192c 100644 --- a/packages/gantt/src/root.component.ts +++ b/packages/gantt/src/root.component.ts @@ -78,24 +78,26 @@ export class NgxGanttRootComponent implements OnInit { return; } this.dom - .getViewerScroll() + .getViewerScroll({ passive: true }) .pipe(takeUntil(this.unsubscribe$)) .subscribe((event) => { if (event.direction === ScrollDirection.LEFT) { const dates = this.view.addStartDate(); if (dates) { event.target.scrollLeft += this.view.getDateRangeWidth(dates.start, dates.end); - this.ngZone.run(() => { - this.ganttUpper.loadOnScroll.emit({ start: dates.start.getUnixTime(), end: dates.end.getUnixTime() }); - }); + if (this.ganttUpper.loadOnScroll.observers) { + this.ngZone.run(() => + this.ganttUpper.loadOnScroll.emit({ start: dates.start.getUnixTime(), end: dates.end.getUnixTime() }) + ); + } } } if (event.direction === ScrollDirection.RIGHT) { const dates = this.view.addEndDate(); - if (dates) { - this.ngZone.run(() => { - this.ganttUpper.loadOnScroll.emit({ start: dates.start.getUnixTime(), end: dates.end.getUnixTime() }); - }); + if (dates && this.ganttUpper.loadOnScroll.observers) { + this.ngZone.run(() => + this.ganttUpper.loadOnScroll.emit({ start: dates.start.getUnixTime(), end: dates.end.getUnixTime() }) + ); } } });