Skip to content

Commit

Permalink
fix: do not setup Hammer if view has been destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt committed Apr 10, 2023
1 parent f802382 commit b8dbf14
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions libs/@ngu/carousel/src/lib/ngu-carousel/ngu-carousel.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ import {
ViewChild,
ViewContainerRef
} from '@angular/core';
import { EMPTY, fromEvent, interval, merge, Observable, of, Subject, Subscription } from 'rxjs';
import {
EMPTY,
from,
fromEvent,
interval,
merge,
Observable,
of,
Subject,
Subscription
} from 'rxjs';
import { debounceTime, filter, map, startWith, switchMap, takeUntil } from 'rxjs/operators';
import {
NguCarouselDefDirective,
Expand Down Expand Up @@ -279,7 +289,7 @@ export class NguCarousel<T>

if (isPlatformBrowser(this.platformId)) {
this._carouselInterval();
if (!this.vertical.enabled) {
if (!this.vertical.enabled && this.inputs.touch) {
this._setupHammer();
}
this._setupWindowResizeListener();
Expand Down Expand Up @@ -340,8 +350,12 @@ export class NguCarousel<T>

/** Get Touch input */
private _setupHammer(): void {
if (this.inputs.touch) {
import('hammerjs').then(() => {
from(import('hammerjs'))
// Note: the dynamic import is always a microtask which may run after the view is destroyed.
// `takeUntil` is used to prevent setting Hammer up if the view had been destroyed before
// the HammerJS is loaded.
.pipe(takeUntil(this._destroy$))
.subscribe(() => {
const hammertime = (this._hammertime = new Hammer(this.touchContainer.nativeElement));
hammertime.get('pan').set({ direction: Hammer.DIRECTION_HORIZONTAL });

Expand Down Expand Up @@ -392,7 +406,6 @@ export class NguCarousel<T>
ev.srcEvent.stopPropagation();
});
});
}
}

/** handle touch input */
Expand Down

0 comments on commit b8dbf14

Please sign in to comment.