mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-02 17:53:30 +00:00
fix(core): fix DropZoneDirective listener leak from bind(this) mismatch [AAE-50892] (#12200)
addEventListener was called with this.onDragEnter.bind(this), creating a new function reference each time, while removeEventListener was called with the unbound this.onDragEnter. The references never matched, so drag listeners were never removed on destroy. Each surviving listener closed over the directive and its host cell, retaining the whole detached datatable/task-list subtree via the parentNode chain. Heap analysis showed 624 dragenter + 624 dragover + 624 drop listeners bound to detached cells. Store the bound handlers once and use the same references for both add and remove so listeners are properly cleaned up.
This commit is contained in:
@@ -0,0 +1,76 @@
|
|||||||
|
/*!
|
||||||
|
* @license
|
||||||
|
* Copyright © 2005-2026 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
import { ElementRef } from '@angular/core';
|
||||||
|
import { DropZoneDirective } from './drop-zone.directive';
|
||||||
|
|
||||||
|
describe('DropZoneDirective', () => {
|
||||||
|
let directive: DropZoneDirective;
|
||||||
|
let element: HTMLElement;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
element = document.createElement('div');
|
||||||
|
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [{ provide: ElementRef, useValue: new ElementRef(element) }]
|
||||||
|
});
|
||||||
|
|
||||||
|
directive = TestBed.runInInjectionContext(() => new DropZoneDirective());
|
||||||
|
directive.dropTarget = 'cell';
|
||||||
|
directive.ngOnInit();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should dispatch a namespaced custom event on dragenter while attached', () => {
|
||||||
|
const dispatched: string[] = [];
|
||||||
|
element.addEventListener('cell-dragenter', () => dispatched.push('cell-dragenter'));
|
||||||
|
|
||||||
|
element.dispatchEvent(new DragEvent('dragenter'));
|
||||||
|
|
||||||
|
expect(dispatched).toContain('cell-dragenter');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not handle drag events after the directive is destroyed', () => {
|
||||||
|
const dispatched: string[] = [];
|
||||||
|
element.addEventListener('cell-dragenter', () => dispatched.push('cell-dragenter'));
|
||||||
|
element.addEventListener('cell-dragover', () => dispatched.push('cell-dragover'));
|
||||||
|
element.addEventListener('cell-drop', () => dispatched.push('cell-drop'));
|
||||||
|
|
||||||
|
directive.ngOnDestroy();
|
||||||
|
|
||||||
|
element.dispatchEvent(new DragEvent('dragenter'));
|
||||||
|
element.dispatchEvent(new DragEvent('dragover'));
|
||||||
|
element.dispatchEvent(new DragEvent('drop'));
|
||||||
|
|
||||||
|
expect(dispatched).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should remove listeners using the same references that were added', () => {
|
||||||
|
const addSpy = spyOn(element, 'addEventListener').and.callThrough();
|
||||||
|
const removeSpy = spyOn(element, 'removeEventListener').and.callThrough();
|
||||||
|
|
||||||
|
directive.ngOnInit();
|
||||||
|
directive.ngOnDestroy();
|
||||||
|
|
||||||
|
const addedByEvent = new Map<string, EventListenerOrEventListenerObject>();
|
||||||
|
addSpy.calls.allArgs().forEach(([evt, fn]) => addedByEvent.set(evt as string, fn as EventListenerOrEventListenerObject));
|
||||||
|
|
||||||
|
removeSpy.calls.allArgs().forEach(([evt, fn]) => {
|
||||||
|
expect(fn).toBe(addedByEvent.get(evt as string));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -36,6 +36,10 @@ export class DropZoneDirective implements OnInit, OnDestroy {
|
|||||||
@Input()
|
@Input()
|
||||||
dropColumn: DataColumn;
|
dropColumn: DataColumn;
|
||||||
|
|
||||||
|
private readonly onDragEnterHandler = this.onDragEnter.bind(this);
|
||||||
|
private readonly onDragOverHandler = this.onDragOver.bind(this);
|
||||||
|
private readonly onDropHandler = this.onDrop.bind(this);
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const elementRef = inject(ElementRef);
|
const elementRef = inject(ElementRef);
|
||||||
|
|
||||||
@@ -44,16 +48,16 @@ export class DropZoneDirective implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.ngZone.runOutsideAngular(() => {
|
this.ngZone.runOutsideAngular(() => {
|
||||||
this.element.addEventListener('dragenter', this.onDragEnter.bind(this));
|
this.element.addEventListener('dragenter', this.onDragEnterHandler);
|
||||||
this.element.addEventListener('dragover', this.onDragOver.bind(this));
|
this.element.addEventListener('dragover', this.onDragOverHandler);
|
||||||
this.element.addEventListener('drop', this.onDrop.bind(this));
|
this.element.addEventListener('drop', this.onDropHandler);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy() {
|
ngOnDestroy() {
|
||||||
this.element.removeEventListener('dragenter', this.onDragEnter);
|
this.element.removeEventListener('dragenter', this.onDragEnterHandler);
|
||||||
this.element.removeEventListener('dragover', this.onDragOver);
|
this.element.removeEventListener('dragover', this.onDragOverHandler);
|
||||||
this.element.removeEventListener('drop', this.onDrop);
|
this.element.removeEventListener('drop', this.onDropHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
onDragEnter(event: DragEvent) {
|
onDragEnter(event: DragEvent) {
|
||||||
|
|||||||
Reference in New Issue
Block a user