From 3874f784f49b25e325e41e9d5044bd7810e10454 Mon Sep 17 00:00:00 2001 From: Denis Severin Date: Fri, 22 Jul 2022 15:58:51 +0300 Subject: [PATCH] fix(core): add complete subscriptions on destroy --- .../overflow-layout.component.ts | 38 +++++++++++-------- .../overflow-layout.service.ts | 21 ++++++---- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/libs/core/src/lib/overflow-layout/overflow-layout.component.ts b/libs/core/src/lib/overflow-layout/overflow-layout.component.ts index 737e4cdc241..c93738fb333 100644 --- a/libs/core/src/lib/overflow-layout/overflow-layout.component.ts +++ b/libs/core/src/lib/overflow-layout/overflow-layout.component.ts @@ -230,22 +230,28 @@ export class OverflowLayoutComponent implements AfterViewInit, OnDestroy, Overfl /** @hidden */ ngAfterViewInit(): void { - this._overflowLayoutService.detectChanges.subscribe(() => { - this._cdr.detectChanges(); - }); - - this._overflowLayoutService.onResult.subscribe((result) => { - this._hiddenItems = result.hiddenItems; - this._showMore = result.showMore; - this.hiddenItemsCount.emit(result.hiddenItems.length); - this.visibleItemsCount.emit(this._allItems.filter((i) => !i.hidden).length); - this._cdr.detectChanges(); - }); - - this._items.changes.subscribe(() => { - this._allItems = this._items.toArray(); - this._cdr.detectChanges(); - }); + this._subscription.add( + this._overflowLayoutService.detectChanges.subscribe(() => { + this._cdr.detectChanges(); + }) + ); + + this._subscription.add( + this._overflowLayoutService.onResult.subscribe((result) => { + this._hiddenItems = result.hiddenItems; + this._showMore = result.showMore; + this.hiddenItemsCount.emit(result.hiddenItems.length); + this.visibleItemsCount.emit(this._allItems.filter((i) => !i.hidden).length); + this._cdr.detectChanges(); + }) + ); + + this._subscription.add( + this._items.changes.subscribe(() => { + this._allItems = this._items.toArray(); + this._cdr.detectChanges(); + }) + ); this._allItems = this._items.toArray(); this._cdr.detectChanges(); diff --git a/libs/core/src/lib/overflow-layout/overflow-layout.service.ts b/libs/core/src/lib/overflow-layout/overflow-layout.service.ts index 96a73555246..18f057eb6c8 100644 --- a/libs/core/src/lib/overflow-layout/overflow-layout.service.ts +++ b/libs/core/src/lib/overflow-layout/overflow-layout.service.ts @@ -1,5 +1,5 @@ import { FocusKeyManager } from '@angular/cdk/a11y'; -import { ElementRef, Injectable, Optional, QueryList } from '@angular/core'; +import { ElementRef, Injectable, OnDestroy, Optional, QueryList } from '@angular/core'; import { resizeObservable, RtlService } from '@fundamental-ngx/core/utils'; import { debounceTime, distinctUntilChanged, filter, Observable, skip, Subject, Subscription } from 'rxjs'; import { OverflowLayoutItemContainerDirective } from './directives/overflow-layout-item-container.directive'; @@ -26,7 +26,7 @@ export class OverflowLayoutListeningResult { } @Injectable() -export class OverflowLayoutService { +export class OverflowLayoutService implements OnDestroy { /** * Overflow Layout config. */ @@ -81,6 +81,11 @@ export class OverflowLayoutService { /** @hidden */ constructor(private _elRef: ElementRef, @Optional() private _rtlService: RtlService | null) {} + /** @hidden */ + ngOnDestroy(): void { + this._subscription.unsubscribe(); + } + startListening(config: OverflowLayoutConfig): void { this.setConfig(config); this.fitVisibleItems(); @@ -283,13 +288,13 @@ export class OverflowLayoutService { return; } - const rtlSub = this._rtlService.rtl.subscribe((isRtl) => { - this._dir = isRtl ? 'rtl' : 'ltr'; - - this._keyboardEventsManager = this._keyboardEventsManager.withHorizontalOrientation(this._dir); - }); + this._subscription.add( + this._rtlService.rtl.subscribe((isRtl) => { + this._dir = isRtl ? 'rtl' : 'ltr'; - this._subscription.add(rtlSub); + this._keyboardEventsManager = this._keyboardEventsManager.withHorizontalOrientation(this._dir); + }) + ); } /** @hidden */