Skip to content

Commit

Permalink
perf: lazily initialise all mousemove listeners until needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlewis92 committed Oct 7, 2019
1 parent 41c2019 commit 81134ee
Showing 1 changed file with 40 additions and 36 deletions.
76 changes: 40 additions & 36 deletions src/resizable.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
share,
auditTime,
switchMap,
startWith
startWith,
tap
} from 'rxjs/operators';
import { Edges } from './interfaces/edges.interface';
import { BoundingRectangle } from './interfaces/bounding-rectangle.interface';
Expand Down Expand Up @@ -392,24 +393,25 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
* @hidden
*/
ngOnInit(): void {
// TODO - use some fancy Observable.merge's for this
this.pointerEventListeners.pointerDown
.pipe(takeUntil(this.destroy$))
.subscribe(({ clientX, clientY }) => {
this.mousedown.next({ clientX, clientY });
});

this.pointerEventListeners.pointerMove
.pipe(takeUntil(this.destroy$))
.subscribe(({ clientX, clientY, event }) => {
this.mousemove.next({ clientX, clientY, event });
});
const mousedown$: Observable<{
clientX: number;
clientY: number;
edges?: Edges;
}> = merge(this.pointerEventListeners.pointerDown, this.mousedown);

const mousemove$ = merge(
this.pointerEventListeners.pointerMove,
this.mousemove
).pipe(
tap(({ event }) => {
if (currentResize) {
event.preventDefault();
}
}),
share()
);

this.pointerEventListeners.pointerUp
.pipe(takeUntil(this.destroy$))
.subscribe(({ clientX, clientY }) => {
this.mouseup.next({ clientX, clientY });
});
const mouseup$ = merge(this.pointerEventListeners.pointerUp, this.mouseup);

let currentResize: {
edges: Edges;
Expand All @@ -434,12 +436,6 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
};
};

const mouseMove: Observable<any> = this.mousemove.pipe(share());

mouseMove.pipe(filter(() => !!currentResize)).subscribe(({ event }) => {
event.preventDefault();
});

this.resizeEdges$
.pipe(
startWith(this.resizeEdges),
Expand All @@ -450,9 +446,10 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
);
}),
switchMap(legacyResizeEdgesEnabled =>
legacyResizeEdgesEnabled ? mouseMove : EMPTY
legacyResizeEdgesEnabled ? mousemove$ : EMPTY
),
auditTime(MOUSE_MOVE_THROTTLE_MS)
auditTime(MOUSE_MOVE_THROTTLE_MS),
takeUntil(this.destroy$)
)
.subscribe(({ clientX, clientY }) => {
const resizeEdges: Edges = getResizeEdges({
Expand Down Expand Up @@ -489,7 +486,7 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
);
});

const mousedrag: Observable<any> = this.mousedown
const mousedrag: Observable<any> = mousedown$
.pipe(
mergeMap(startCoords => {
function getDiff(moveCoords: { clientX: number; clientY: number }) {
Expand Down Expand Up @@ -535,10 +532,15 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
};
}

return merge(
mouseMove.pipe(take(1)).pipe(map(coords => [, coords])),
mouseMove.pipe(pairwise())
)
return (merge(
mousemove$.pipe(take(1)).pipe(map(coords => [, coords])),
mousemove$.pipe(pairwise())
) as Observable<
[
{ clientX: number; clientY: number },
{ clientX: number; clientY: number }
]
>)
.pipe(
map(([previousCoords, newCoords]) => {
return [
Expand Down Expand Up @@ -576,7 +578,7 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
};
})
)
.pipe(takeUntil(merge(this.mouseup, this.mousedown)));
.pipe(takeUntil(merge(mouseup$, mousedown$)));
})
)
.pipe(filter(() => !!currentResize));
Expand Down Expand Up @@ -617,7 +619,8 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
})
})
: true;
})
}),
takeUntil(this.destroy$)
)
.subscribe((newBoundingRect: BoundingRectangle) => {
if (currentResize && currentResize.clonedNode) {
Expand Down Expand Up @@ -657,7 +660,7 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
currentResize!.currentRect = newBoundingRect;
});

this.mousedown
mousedown$
.pipe(
map(({ clientX, clientY, edges }) => {
return (
Expand All @@ -675,7 +678,8 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
.pipe(
filter((edges: Edges) => {
return Object.keys(edges).length > 0;
})
}),
takeUntil(this.destroy$)
)
.subscribe((edges: Edges) => {
if (currentResize) {
Expand Down Expand Up @@ -755,7 +759,7 @@ export class ResizableDirective implements OnInit, OnChanges, OnDestroy {
});
});

this.mouseup.subscribe(() => {
mouseup$.pipe(takeUntil(this.destroy$)).subscribe(() => {
if (currentResize) {
this.renderer.removeClass(this.elm.nativeElement, RESIZE_ACTIVE_CLASS);
this.renderer.setStyle(document.body, 'cursor', '');
Expand Down

0 comments on commit 81134ee

Please sign in to comment.