wannabegeek / ng2-split-pane

Split View module for Angular 2
MIT License
43 stars 25 forks source link

Split pane makes change detection run with every mousemove #53

Open BlindDespair opened 5 years ago

BlindDespair commented 5 years ago

I found a performance problem with the component. The MouseMove listener here is constantly listening for mousemove in ngZone which runs change detection for entire application all the time user moves mouse, although it's not even needed as it is supposed to only care about mousemove when isResizing is set. I can propose you couple solutions: inject NgZone into splitPane and change your method to something like

@HostListener('mousemove', ['$event'])
  onMousemove(event: MouseEvent) {
    this.ngZone.runOutsideOfAngular(() => {
       if (this.isResizing) {
         this.ngZone.run(() => {
            let coords = PositionService.offset(this.primaryComponent);
             this.applySizeChange(event.pageX - coords.left);
             return false;
         });
      }
    });
    }

This would quit angular zone for the handler and only reenter it when isResizing is set to true. Other way to solve it is to use rxjs for event handling and only subscribe to mousemove event after mousedown and takeUntil mouseup. This way handler will be even non existent while not needed, but I assume it would require more refactoring.