Skip to content

Commit 4e544ef

Browse files
committed
perf(angular): call swiperRef outside of angular
1 parent c990909 commit 4e544ef

File tree

1 file changed

+116
-105
lines changed

1 file changed

+116
-105
lines changed

src/angular/src/swiper.component.ts

+116-105
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,9 @@ export class SwiperComponent implements OnInit {
532532
this._changeDetectorRef.detectChanges();
533533
},
534534
});
535-
new Swiper(this.elementRef.nativeElement, swiperParams);
535+
this.zone.runOutsideAngular(() => {
536+
new Swiper(this.elementRef.nativeElement, swiperParams);
537+
});
536538
}
537539

538540
style: any = null;
@@ -558,13 +560,15 @@ export class SwiperComponent implements OnInit {
558560
this.currentVirtualData = virtualData;
559561
this._activeSlides.next(virtualData.slides);
560562
this._changeDetectorRef.detectChanges();
561-
this.swiperRef.updateSlides();
562-
this.swiperRef.updateProgress();
563-
this.swiperRef.updateSlidesClasses();
564-
if (this.swiperRef.lazy && this.swiperRef.params.lazy['enabled']) {
565-
this.swiperRef.lazy.load();
566-
}
567-
this.swiperRef.virtual.update(true);
563+
this.zone.runOutsideAngular(() => {
564+
this.swiperRef.updateSlides();
565+
this.swiperRef.updateProgress();
566+
this.swiperRef.updateSlidesClasses();
567+
if (this.swiperRef.lazy && this.swiperRef.params.lazy['enabled']) {
568+
this.swiperRef.lazy.load();
569+
}
570+
this.swiperRef.virtual.update(true);
571+
});
568572
return;
569573
}
570574

@@ -577,115 +581,120 @@ export class SwiperComponent implements OnInit {
577581
if (!(changedParams && this.swiperRef && !this.swiperRef.destroyed)) {
578582
return;
579583
}
580-
const {
581-
params: currentParams,
582-
pagination,
583-
navigation,
584-
scrollbar,
585-
virtual,
586-
thumbs,
587-
} = this.swiperRef;
588-
589-
if (changedParams.pagination) {
590-
if (
591-
this.pagination &&
592-
typeof this.pagination !== 'boolean' &&
593-
this.pagination.el &&
594-
pagination &&
595-
!pagination.el
596-
) {
597-
this.updateParameter('pagination', this.pagination);
598-
pagination.init();
599-
pagination.render();
600-
pagination.update();
601-
} else {
602-
pagination.destroy();
603-
pagination.el = null;
584+
585+
this.zone.runOutsideAngular(() => {
586+
const {
587+
params: currentParams,
588+
pagination,
589+
navigation,
590+
scrollbar,
591+
virtual,
592+
thumbs,
593+
} = this.swiperRef;
594+
595+
if (changedParams.pagination) {
596+
if (
597+
this.pagination &&
598+
typeof this.pagination !== 'boolean' &&
599+
this.pagination.el &&
600+
pagination &&
601+
!pagination.el
602+
) {
603+
this.updateParameter('pagination', this.pagination);
604+
pagination.init();
605+
pagination.render();
606+
pagination.update();
607+
} else {
608+
pagination.destroy();
609+
pagination.el = null;
610+
}
604611
}
605-
}
606612

607-
if (changedParams.scrollbar) {
608-
if (
609-
this.scrollbar &&
610-
typeof this.scrollbar !== 'boolean' &&
611-
this.scrollbar.el &&
612-
scrollbar &&
613-
!scrollbar.el
614-
) {
615-
this.updateParameter('scrollbar', this.scrollbar);
616-
scrollbar.init();
617-
scrollbar.updateSize();
618-
scrollbar.setTranslate();
619-
} else {
620-
scrollbar.destroy();
621-
scrollbar.el = null;
613+
if (changedParams.scrollbar) {
614+
if (
615+
this.scrollbar &&
616+
typeof this.scrollbar !== 'boolean' &&
617+
this.scrollbar.el &&
618+
scrollbar &&
619+
!scrollbar.el
620+
) {
621+
this.updateParameter('scrollbar', this.scrollbar);
622+
scrollbar.init();
623+
scrollbar.updateSize();
624+
scrollbar.setTranslate();
625+
} else {
626+
scrollbar.destroy();
627+
scrollbar.el = null;
628+
}
622629
}
623-
}
624630

625-
if (changedParams.navigation) {
626-
if (
627-
this.navigation &&
628-
typeof this.navigation !== 'boolean' &&
629-
this.navigation.prevEl &&
630-
this.navigation.nextEl &&
631-
navigation &&
632-
!navigation.prevEl &&
633-
!navigation.nextEl
634-
) {
635-
this.updateParameter('navigation', this.navigation);
636-
navigation.init();
637-
navigation.update();
638-
} else if (navigation.prevEl && navigation.nextEl) {
639-
navigation.destroy();
640-
navigation.nextEl = null;
641-
navigation.prevEl = null;
631+
if (changedParams.navigation) {
632+
if (
633+
this.navigation &&
634+
typeof this.navigation !== 'boolean' &&
635+
this.navigation.prevEl &&
636+
this.navigation.nextEl &&
637+
navigation &&
638+
!navigation.prevEl &&
639+
!navigation.nextEl
640+
) {
641+
this.updateParameter('navigation', this.navigation);
642+
navigation.init();
643+
navigation.update();
644+
} else if (navigation.prevEl && navigation.nextEl) {
645+
navigation.destroy();
646+
navigation.nextEl = null;
647+
navigation.prevEl = null;
648+
}
649+
}
650+
if (changedParams.thumbs && this.thumbs && this.thumbs.swiper) {
651+
this.updateParameter('thumbs', this.thumbs);
652+
const initialized = thumbs.init();
653+
if (initialized) thumbs.update(true);
642654
}
643-
}
644-
if (changedParams.thumbs && this.thumbs && this.thumbs.swiper) {
645-
this.updateParameter('thumbs', this.thumbs);
646-
const initialized = thumbs.init();
647-
if (initialized) thumbs.update(true);
648-
}
649655

650-
if (changedParams.controller && this.controller && this.controller.control) {
651-
this.swiperRef.controller.control = this.controller.control;
652-
}
656+
if (changedParams.controller && this.controller && this.controller.control) {
657+
this.swiperRef.controller.control = this.controller.control;
658+
}
653659

654-
this.swiperRef.update();
660+
this.swiperRef.update();
661+
});
655662
}
656663

657664
updateSwiper(changedParams: SimpleChanges | any) {
658-
if (changedParams.config) {
659-
return;
660-
}
661-
if (!(changedParams && this.swiperRef && !this.swiperRef.destroyed)) {
662-
return;
663-
}
664-
for (const key in changedParams) {
665-
if (ignoreNgOnChanges.indexOf(key) >= 0) {
666-
continue;
665+
this.zone.runOutsideAngular(() => {
666+
if (changedParams.config) {
667+
return;
668+
}
669+
if (!(changedParams && this.swiperRef && !this.swiperRef.destroyed)) {
670+
return;
671+
}
672+
for (const key in changedParams) {
673+
if (ignoreNgOnChanges.indexOf(key) >= 0) {
674+
continue;
675+
}
676+
const newValue = changedParams[key]?.currentValue ?? changedParams[key];
677+
this.updateParameter(key, newValue);
667678
}
668-
const newValue = changedParams[key]?.currentValue ?? changedParams[key];
669-
this.updateParameter(key, newValue);
670-
}
671679

672-
if (changedParams.allowSlideNext) {
673-
this.swiperRef.allowSlideNext = this.allowSlideNext;
674-
}
675-
if (changedParams.allowSlidePrev) {
676-
this.swiperRef.allowSlidePrev = this.allowSlidePrev;
677-
}
678-
if (changedParams.direction) {
679-
this.swiperRef.changeDirection(this.direction, false);
680-
}
681-
if (changedParams.breakpoints) {
682-
if (this.loop && !this.loopedSlides) {
683-
this.calcLoopedSlides();
680+
if (changedParams.allowSlideNext) {
681+
this.swiperRef.allowSlideNext = this.allowSlideNext;
684682
}
685-
this.swiperRef.currentBreakpoint = null;
686-
this.swiperRef.setBreakpoint();
687-
}
688-
this.swiperRef.update();
683+
if (changedParams.allowSlidePrev) {
684+
this.swiperRef.allowSlidePrev = this.allowSlidePrev;
685+
}
686+
if (changedParams.direction) {
687+
this.swiperRef.changeDirection(this.direction, false);
688+
}
689+
if (changedParams.breakpoints) {
690+
if (this.loop && !this.loopedSlides) {
691+
this.calcLoopedSlides();
692+
}
693+
this.swiperRef.currentBreakpoint = null;
694+
this.swiperRef.setBreakpoint();
695+
}
696+
this.swiperRef.update();
697+
});
689698
}
690699

691700
calcLoopedSlides() {
@@ -749,6 +758,8 @@ export class SwiperComponent implements OnInit {
749758
}
750759

751760
ngOnDestroy() {
752-
this.swiperRef?.destroy();
761+
this.zone.runOutsideAngular(() => {
762+
this.swiperRef?.destroy();
763+
});
753764
}
754765
}

0 commit comments

Comments
 (0)