Skip to content

Commit 0375b49

Browse files
committed
fix(sortable): unsubscribe item capture when component is destroyed
Unsubscribed subscription in sortable component was causing memory to leak. We need to ensure all subscriptions are released whenever a component is destroyed.
1 parent 663c70e commit 0375b49

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

src/sortable/sortable.component.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import {
44
Output,
55
EventEmitter,
66
forwardRef,
7-
TemplateRef
7+
TemplateRef,
8+
OnDestroy,
9+
HostListener,
810
} from '@angular/core';
911
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
1012
import { DraggableItem } from './draggable-item';
1113
import { DraggableItemService } from './draggable-item.service';
1214

15+
import { takeUntil } from "rxjs/operators";
16+
import { Subject } from "rxjs";
17+
1318
@Component({
1419
selector: 'bs-sortable',
1520
exportAs: 'bs-sortable',
@@ -53,8 +58,10 @@ import { DraggableItemService } from './draggable-item.service';
5358
}
5459
]
5560
})
56-
export class SortableComponent implements ControlValueAccessor {
61+
export class SortableComponent implements ControlValueAccessor, OnDestroy {
5762
private static globalZoneIndex = 0;
63+
private _destroy$: Subject<boolean> = new Subject<boolean>();
64+
5865
/** field name if input array consists of objects */
5966
@Input() fieldName?: string;
6067

@@ -121,6 +128,7 @@ export class SortableComponent implements ControlValueAccessor {
121128
this.currentZoneIndex = SortableComponent.globalZoneIndex++;
122129
this.transfer
123130
.onCaptureItem()
131+
.pipe(takeUntil(this._destroy$))
124132
.subscribe((item: DraggableItem) => this.onDrop(item));
125133
}
126134

@@ -247,8 +255,13 @@ export class SortableComponent implements ControlValueAccessor {
247255
// with IE
248256
event.dataTransfer?.setData('Text', 'placeholder');
249257
}
250-
}
251258

259+
@HostListener('unloaded')
260+
public ngOnDestroy(): void {
261+
this._destroy$.next(true);
262+
this._destroy$.complete();
263+
}
264+
}
252265
export declare interface SortableItem {
253266
id: number;
254267
value: string;

0 commit comments

Comments
 (0)