Skip to content

Commit

Permalink
perf(resizable): only register mouse move events when a rezize is active
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Lewis committed Mar 4, 2017
1 parent 820612e commit a74f9e2
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/resizable.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ export class Resizable implements OnInit, OnDestroy, AfterViewInit {
*/
@ContentChildren(ResizeHandle) resizeHandles: QueryList<ResizeHandle>;

private eventListeners: {
touchmove?: Function,
mousemove?: Function
} = {};

/**
* @private
*/
Expand Down Expand Up @@ -470,6 +475,7 @@ export class Resizable implements OnInit, OnDestroy, AfterViewInit {
this.mousedown.complete();
this.mouseup.complete();
this.mousemove.complete();
this.unsubscribeEventListeners();
}

/**
Expand All @@ -479,21 +485,20 @@ export class Resizable implements OnInit, OnDestroy, AfterViewInit {
@HostListener('document:mousedown', ['$event.clientX', '$event.clientY'])
onMousedown(mouseX: number, mouseY: number): void {
this.zone.runOutsideAngular(() => {
if (!this.eventListeners.touchmove) {
this.eventListeners.touchmove = this.renderer.listenGlobal('document', 'touchmove', (event: any) => {
this.onMousemove(event, event.targetTouches[0].clientX, event.targetTouches[0].clientY);
});
}
if (!this.eventListeners.mousemove) {
this.eventListeners.mousemove = this.renderer.listenGlobal('document', 'mousemove', (event: any) => {
this.onMousemove(event, event.clientX, event.clientY);
});
}
this.mousedown.next({mouseX, mouseY});
});
}

/**
* @private
*/
@HostListener('document:touchmove', ['$event', '$event.targetTouches[0].clientX', '$event.targetTouches[0].clientY'])
@HostListener('document:mousemove', ['$event', '$event.clientX', '$event.clientY'])
onMousemove(event: any, mouseX: number, mouseY: number): void {
this.zone.runOutsideAngular(() => {
this.mousemove.next({mouseX, mouseY, event});
});
}

/**
* @private
*/
Expand All @@ -506,4 +511,17 @@ export class Resizable implements OnInit, OnDestroy, AfterViewInit {
});
}

private onMousemove(event: any, mouseX: number, mouseY: number): void {
this.zone.runOutsideAngular(() => {
this.mousemove.next({mouseX, mouseY, event});
});
}

private unsubscribeEventListeners(): void {
Object.keys(this.eventListeners).forEach(type => {
this.eventListeners[type]();
delete this.eventListeners[type];
});
}

}

0 comments on commit a74f9e2

Please sign in to comment.