Skip to content

Commit

Permalink
fix: Unsubscribe layout components subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
DailisLangovskis authored and raitisbe committed Oct 14, 2022
1 parent 858a2ee commit 6a67c18
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
38 changes: 25 additions & 13 deletions projects/hslayers/src/components/layout/layout.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import {
Component,
ElementRef,
Input,
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core';

import {Subject, delay, takeUntil} from 'rxjs';

import {HsConfig} from '../../config.service';
import {HsEventBusService} from '../core/event-bus.service';
import {HsLayoutService} from './layout.service';
Expand All @@ -15,21 +18,20 @@ import {HsOverlayPanelContainerService} from './overlay-panel-container.service'
import {HsPanelContainerService} from './panels/panel-container.service';
import {HsShareUrlService} from '../permalink/share-url.service';
import {HsUtilsService} from '../utils/utils.service';
import {delay} from 'rxjs';

@Component({
selector: 'hs-layout',
templateUrl: './partials/layout.html',
})
export class HsLayoutComponent implements AfterViewInit, OnInit {
export class HsLayoutComponent implements AfterViewInit, OnInit, OnDestroy {
@Input() app = 'default';
@ViewChild('hslayout') hslayout: ElementRef;
@ViewChild(HsMapHostDirective, {static: true})
mapHost: HsMapHostDirective;
panelSpaceWidth: number;
sidebarPosition: string;
sidebarVisible: boolean;

private ngUnsubscribe = new Subject<void>();
panelVisible(which, app: string, scope?): boolean {
return this.HsLayoutService.panelVisible(which, app, scope);
}
Expand All @@ -49,6 +51,11 @@ export class HsLayoutComponent implements AfterViewInit, OnInit {
private hsShareUrlService: HsShareUrlService
) {}

ngOnDestroy(): void {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}

ngOnInit(): void {
this.HsLayoutService.get(this.app).layoutElement =
this.elementRef.nativeElement;
Expand Down Expand Up @@ -80,13 +87,16 @@ export class HsLayoutComponent implements AfterViewInit, OnInit {
viewContainerRef: this.mapHost.viewContainerRef,
app: this.app,
});
this.HsLayoutService.panelSpaceWidth.subscribe(({app, width}) => {
if (this.app == app) {
this.panelSpaceWidth = width;
}
});
this.HsLayoutService.panelSpaceWidth
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(({app, width}) => {
if (this.app == app) {
this.panelSpaceWidth = width;
}
});
this.HsLayoutService.sidebarPosition
.pipe(delay(0))
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(({app, position}) => {
if (this.app == app) {
this.sidebarPosition = position;
Expand All @@ -98,11 +108,13 @@ export class HsLayoutComponent implements AfterViewInit, OnInit {
}
}
});
this.HsLayoutService.sidebarVisible.subscribe(({app, visible}) => {
if (this.app == app) {
this.sidebarVisible = visible;
}
});
this.HsLayoutService.sidebarVisible
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe(({app, visible}) => {
if (this.app == app) {
this.sidebarVisible = visible;
}
});

window.addEventListener(
'resize',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class HsSidebarComponent implements OnInit, OnDestroy {
) {}
ngOnDestroy(): void {
this.end.next();
this.end.complete();
}
ngOnInit(): void {
const panel = this.HsShareUrlService.getParamValue(HS_PRMS.panel);
Expand Down

0 comments on commit 6a67c18

Please sign in to comment.