reppners / ngx-drag-drop

Angular directives using the native HTML Drag And Drop API
https://reppners.github.io/ngx-drag-drop/
BSD 3-Clause "New" or "Revised" License
307 stars 120 forks source link

Move dragover event outside the angular zone #13

Closed mtraynham closed 6 years ago

mtraynham commented 6 years ago

@HostListener events are within the Angular Zone and trigger change detection on the Host element and all child elements. This can get unwieldy if the drop zone has a bunch of items as it will trigger change detection on all components in the hierarchy every time the user moves the mouse on drag.

For this library, it seemed appropriate to change only the dragover. It's the method called most often, and you aren't doing anything with the component references, you're only updating the DOM.

Even though this moves the event outside the Angular Zone, we can still invoke EventEmitters. If the user needs to run change detection on dragOver, they can do so manually by subscribing to the EventEmitter and running changeDetectorRef.detectChanges() (they would have had to injected ChangeDetectorRef).

The eventListener needs to be cleaned up on component destroy, so I had store the reference to the function as a private instance variable.

Before this change:

screen shot 2018-04-20 at 8 36 43 pm

After this change

screen shot 2018-04-20 at 8 43 46 pm
reppners commented 6 years ago

Awesome! Thanks!