1
0

97732 Remove unnecessary subscriptions

This commit is contained in:
Koen Pauwels
2023-02-08 11:13:03 +01:00
parent ac5a359b1d
commit ad870829d2
2 changed files with 9 additions and 23 deletions

View File

@@ -1,6 +1,6 @@
import { Component, OnInit, OnDestroy } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ContextHelpService } from '../../shared/context-help.service'; import { ContextHelpService } from '../../shared/context-help.service';
import { Observable, Subscription } from 'rxjs'; import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
/** /**
@@ -12,22 +12,15 @@ import { map } from 'rxjs/operators';
templateUrl: './context-help-toggle.component.html', templateUrl: './context-help-toggle.component.html',
styleUrls: ['./context-help-toggle.component.scss'] styleUrls: ['./context-help-toggle.component.scss']
}) })
export class ContextHelpToggleComponent implements OnInit, OnDestroy { export class ContextHelpToggleComponent implements OnInit {
buttonVisible$: Observable<boolean>; buttonVisible$: Observable<boolean>;
constructor( constructor(
private contextHelpService: ContextHelpService, private contextHelpService: ContextHelpService,
) { } ) { }
private subs: Subscription[];
ngOnInit(): void { ngOnInit(): void {
this.buttonVisible$ = this.contextHelpService.tooltipCount$().pipe(map(x => x > 0)); this.buttonVisible$ = this.contextHelpService.tooltipCount$().pipe(map(x => x > 0));
this.subs = [this.buttonVisible$.subscribe()];
}
ngOnDestroy() {
this.subs.forEach(sub => sub.unsubscribe());
} }
onClick() { onClick() {

View File

@@ -61,8 +61,7 @@ export class ContextHelpWrapperComponent implements OnInit, OnDestroy {
parsedContent$: Observable<ParsedContent>; parsedContent$: Observable<ParsedContent>;
private subs: {always: Subscription[], tooltipBound: Subscription[]} private subs: Subscription[] = [];
= {always: [], tooltipBound: []};
constructor( constructor(
private translateService: TranslateService, private translateService: TranslateService,
@@ -78,14 +77,13 @@ export class ContextHelpWrapperComponent implements OnInit, OnDestroy {
dontParseLinks ? [text] : this.parseLinks(text)) dontParseLinks ? [text] : this.parseLinks(text))
); );
this.shouldShowIcon$ = this.contextHelpService.shouldShowIcons$(); this.shouldShowIcon$ = this.contextHelpService.shouldShowIcons$();
this.subs.always = [this.parsedContent$.subscribe(), this.shouldShowIcon$.subscribe()];
} }
@ViewChild('tooltip', { static: false }) set setTooltip(tooltip: NgbTooltip) { @ViewChild('tooltip', { static: false }) set setTooltip(tooltip: NgbTooltip) {
this.tooltip = tooltip; this.tooltip = tooltip;
this.clearSubs('tooltipBound'); this.clearSubs();
if (this.tooltip !== undefined) { if (this.tooltip !== undefined) {
this.subs.tooltipBound = [ this.subs = [
this.contextHelpService.getContextHelp$(this.id) this.contextHelpService.getContextHelp$(this.id)
.pipe(hasValueOperator()) .pipe(hasValueOperator())
.subscribe((ch: ContextHelp) => { .subscribe((ch: ContextHelp) => {
@@ -159,13 +157,8 @@ export class ContextHelpWrapperComponent implements OnInit, OnDestroy {
}); });
} }
private clearSubs(filter: null | 'tooltipBound' = null) { private clearSubs() {
if (filter === null) { this.subs.forEach(sub => sub.unsubscribe());
[].concat(...Object.values(this.subs)).forEach(sub => sub.unsubscribe()); this.subs = [];
this.subs = {always: [], tooltipBound: []};
} else {
this.subs[filter].forEach(sub => sub.unsubscribe());
this.subs[filter] = [];
}
} }
} }