mirror of
https://github.com/tenrok/OverlayScrollbars.git
synced 2026-06-22 23:00:36 +03:00
fix(os-ngx): re-enter Angular zone when dispatching events
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
|||||||
ElementRef,
|
ElementRef,
|
||||||
OnDestroy,
|
OnDestroy,
|
||||||
AfterViewInit,
|
AfterViewInit,
|
||||||
|
NgZone,
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import { OverlayScrollbars } from 'overlayscrollbars';
|
import { OverlayScrollbars } from 'overlayscrollbars';
|
||||||
import type { PartialOptions, EventListeners, EventListenerArgs } from 'overlayscrollbars';
|
import type { PartialOptions, EventListeners, EventListenerArgs } from 'overlayscrollbars';
|
||||||
@@ -55,7 +56,7 @@ export class OverlayScrollbarsComponent implements OnDestroy, AfterViewInit {
|
|||||||
@ViewChild('content', { read: OverlayScrollbarsDirective })
|
@ViewChild('content', { read: OverlayScrollbarsDirective })
|
||||||
private osDirective?: OverlayScrollbarsDirective;
|
private osDirective?: OverlayScrollbarsDirective;
|
||||||
|
|
||||||
constructor(private targetRef: ElementRef<HTMLElement>) {}
|
constructor(private ngZone: NgZone, private targetRef: ElementRef<HTMLElement>) {}
|
||||||
|
|
||||||
osInstance(): OverlayScrollbars | null {
|
osInstance(): OverlayScrollbars | null {
|
||||||
return this.osDirective!.osInstance();
|
return this.osDirective!.osInstance();
|
||||||
@@ -88,12 +89,24 @@ export class OverlayScrollbarsComponent implements OnDestroy, AfterViewInit {
|
|||||||
mergeEvents(originalEvents: OverlayScrollbarsComponent['events']) {
|
mergeEvents(originalEvents: OverlayScrollbarsComponent['events']) {
|
||||||
return mergeEventListeners(
|
return mergeEventListeners(
|
||||||
{
|
{
|
||||||
initialized: (...args) => this.onInitialized.emit(args),
|
initialized: (...args) => this.dispatchEventIfHasObservers(this.onInitialized, args),
|
||||||
updated: (...args) => this.onUpdated.emit(args),
|
updated: (...args) => this.dispatchEventIfHasObservers(this.onUpdated, args),
|
||||||
destroyed: (...args) => this.onDestroyed.emit(args),
|
destroyed: (...args) => this.dispatchEventIfHasObservers(this.onDestroyed, args),
|
||||||
scroll: (...args) => this.onScroll.emit(args),
|
scroll: (...args) => this.dispatchEventIfHasObservers(this.onScroll, args),
|
||||||
},
|
},
|
||||||
originalEvents || {}
|
originalEvents || {}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private dispatchEventIfHasObservers<T>(eventEmitter: EventEmitter<T>, args: T): void {
|
||||||
|
// `observed` is available since RxJS@7.2 because `observers` is being deprecated.
|
||||||
|
if ((eventEmitter as any).observed || eventEmitter.observers.length > 0) {
|
||||||
|
// This is required to re-enter the Angular zone to call the event handler in the Angular
|
||||||
|
// zone too. This will not re-enter the Angular zone if emitter doesn't have any observers,
|
||||||
|
// for instance, it's being listened: `<overlay-scrollbars (osInitialized)="..."`.
|
||||||
|
// Events are dispatched outside of the Angular zone because instance is created in the
|
||||||
|
// `<root>` zone, see `OverlayScrollbarsDirective#osInitialize`.
|
||||||
|
this.ngZone.run(() => eventEmitter.emit(args));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user