Skip to content

Commit d74034b

Browse files
authored
fix: do not setup Hammer if view has been destroyed (#427)
1 parent f802382 commit d74034b

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

libs/@ngu/carousel/src/lib/ngu-carousel/ngu-carousel.component.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ import {
2929
ViewChild,
3030
ViewContainerRef
3131
} from '@angular/core';
32-
import { EMPTY, fromEvent, interval, merge, Observable, of, Subject, Subscription } from 'rxjs';
32+
import {
33+
EMPTY,
34+
from,
35+
fromEvent,
36+
interval,
37+
merge,
38+
Observable,
39+
of,
40+
Subject,
41+
Subscription
42+
} from 'rxjs';
3343
import { debounceTime, filter, map, startWith, switchMap, takeUntil } from 'rxjs/operators';
3444
import {
3545
NguCarouselDefDirective,
@@ -279,7 +289,7 @@ export class NguCarousel<T>
279289

280290
if (isPlatformBrowser(this.platformId)) {
281291
this._carouselInterval();
282-
if (!this.vertical.enabled) {
292+
if (!this.vertical.enabled && this.inputs.touch) {
283293
this._setupHammer();
284294
}
285295
this._setupWindowResizeListener();
@@ -340,8 +350,12 @@ export class NguCarousel<T>
340350

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

@@ -392,7 +406,6 @@ export class NguCarousel<T>
392406
ev.srcEvent.stopPropagation();
393407
});
394408
});
395-
}
396409
}
397410

398411
/** handle touch input */

0 commit comments

Comments
 (0)