track subscriptions and remove nested subscribe

This commit is contained in:
Art Lowel
2022-01-27 18:16:43 +01:00
parent 74a6e3ce6a
commit 16884682d8

View File

@@ -1,4 +1,4 @@
import { Component, Input, OnInit } from '@angular/core'; import { Component, Input, OnInit, OnDestroy } from '@angular/core';
import { defaultIfEmpty, filter, map, switchMap, take } from 'rxjs/operators'; import { defaultIfEmpty, filter, map, switchMap, take } from 'rxjs/operators';
import { import {
AbstractSimpleItemActionComponent AbstractSimpleItemActionComponent
@@ -8,7 +8,7 @@ import {
combineLatest as observableCombineLatest, combineLatest as observableCombineLatest,
combineLatest, combineLatest,
Observable, Observable,
of as observableOf of as observableOf, Subscription
} from 'rxjs'; } from 'rxjs';
import { RelationshipType } from '../../../core/shared/item-relationships/relationship-type.model'; import { RelationshipType } from '../../../core/shared/item-relationships/relationship-type.model';
import { VirtualMetadata } from '../virtual-metadata/virtual-metadata.component'; import { VirtualMetadata } from '../virtual-metadata/virtual-metadata.component';
@@ -45,7 +45,7 @@ import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
*/ */
export class ItemDeleteComponent export class ItemDeleteComponent
extends AbstractSimpleItemActionComponent extends AbstractSimpleItemActionComponent
implements OnInit { implements OnInit, OnDestroy {
/** /**
* The current url of this page * The current url of this page
@@ -87,6 +87,11 @@ export class ItemDeleteComponent
*/ */
public modalRef: NgbModalRef; public modalRef: NgbModalRef;
/**
* Array to track all subscriptions and unsubscribe them onDestroy
*/
private subs: Subscription[] = [];
constructor(protected route: ActivatedRoute, constructor(protected route: ActivatedRoute,
protected router: Router, protected router: Router,
protected notificationsService: NotificationsService, protected notificationsService: NotificationsService,
@@ -117,7 +122,7 @@ export class ItemDeleteComponent
const label = this.item.firstMetadataValue('dspace.entity.type'); const label = this.item.firstMetadataValue('dspace.entity.type');
if (isNotEmpty(label)) { if (isNotEmpty(label)) {
this.entityTypeService.getEntityTypeByLabel(label).pipe( this.subs.push(this.entityTypeService.getEntityTypeByLabel(label).pipe(
getFirstSucceededRemoteData(), getFirstSucceededRemoteData(),
getRemoteDataPayload(), getRemoteDataPayload(),
switchMap((entityType) => this.entityTypeService.getEntityTypeRelationships(entityType.id)), switchMap((entityType) => this.entityTypeService.getEntityTypeRelationships(entityType.id)),
@@ -141,16 +146,14 @@ export class ItemDeleteComponent
), ),
); );
}) })
).subscribe((types: RelationshipType[]) => this.types$.next(types)); ).subscribe((types: RelationshipType[]) => this.types$.next(types)));
} else {
this.types$.next([]);
} }
this.types$.pipe( this.subs.push(this.types$.pipe(
take(1), take(1),
).subscribe((types) => ).subscribe((types) =>
this.objectUpdatesService.initialize(this.url, types, this.item.lastModified) this.objectUpdatesService.initialize(this.url, types, this.item.lastModified)
); ));
} }
/** /**
@@ -330,7 +333,7 @@ export class ItemDeleteComponent
*/ */
performAction() { performAction() {
this.types$.pipe( this.subs.push(this.types$.pipe(
switchMap((types) => switchMap((types) =>
combineLatest( combineLatest(
types.map((type) => this.isSelected(type)) types.map((type) => this.isSelected(type))
@@ -342,13 +345,14 @@ export class ItemDeleteComponent
map((selectedTypes) => selectedTypes.map((type) => type.id)), map((selectedTypes) => selectedTypes.map((type) => type.id)),
) )
), ),
).subscribe((types) => { switchMap((types) =>
this.itemDataService.delete(this.item.id, types).pipe(getFirstCompletedRemoteData()).subscribe( this.itemDataService.delete(this.item.id, types).pipe(getFirstCompletedRemoteData())
(rd: RemoteData<NoContent>) => { )
this.notify(rd.hasSucceeded); ).subscribe(
} (rd: RemoteData<NoContent>) => {
); this.notify(rd.hasSucceeded);
}); }
));
} }
/** /**
@@ -364,4 +368,14 @@ export class ItemDeleteComponent
this.router.navigate([getItemEditRoute(this.item)]); this.router.navigate([getItemEditRoute(this.item)]);
} }
} }
/**
* Unsubscribe from all subscriptions
*/
ngOnDestroy(): void {
this.subs
.filter((sub) => hasValue(sub))
.forEach((sub) => sub.unsubscribe());
}
} }