Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sortable): unsubscribe item capture when component is destroyed #6546

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/sortable/sortable.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
Output,
EventEmitter,
forwardRef,
TemplateRef
TemplateRef,
OnDestroy,
HostListener,
} from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
import { DraggableItem } from './draggable-item';
import { DraggableItemService } from './draggable-item.service';
import { NgClass, NgStyle, NgIf, NgFor, NgTemplateOutlet } from '@angular/common';

import { takeUntil } from "rxjs/operators";
import { Subject } from "rxjs";

@Component({
selector: 'bs-sortable',
exportAs: 'bs-sortable',
Expand Down Expand Up @@ -57,8 +62,10 @@
standalone: true,
imports: [NgClass, NgStyle, NgIf, NgFor, NgTemplateOutlet]
})
export class SortableComponent implements ControlValueAccessor {
export class SortableComponent implements ControlValueAccessor, OnDestroy {
private static globalZoneIndex = 0;
private _destroy$: Subject<boolean> = new Subject<boolean>();

Check warning on line 67 in src/sortable/sortable.component.ts

View check run for this annotation

Codecov / codecov/patch

src/sortable/sortable.component.ts#L67

Added line #L67 was not covered by tests

/** field name if input array consists of objects */
@Input() fieldName?: string;

Expand Down Expand Up @@ -125,6 +132,7 @@
this.currentZoneIndex = SortableComponent.globalZoneIndex++;
this.transfer
.onCaptureItem()
.pipe(takeUntil(this._destroy$))
.subscribe((item: DraggableItem) => this.onDrop(item));
}

Expand Down Expand Up @@ -251,8 +259,13 @@
// with IE
event.dataTransfer?.setData('Text', 'placeholder');
}
}

@HostListener('unloaded')
public ngOnDestroy(): void {
this._destroy$.next(true);
this._destroy$.complete();

Check warning on line 266 in src/sortable/sortable.component.ts

View check run for this annotation

Codecov / codecov/patch

src/sortable/sortable.component.ts#L264-L266

Added lines #L264 - L266 were not covered by tests
}
}
export declare interface SortableItem {
id: number;
value: string;
Expand Down
Loading