1- import { Component , Input , OnInit } from '@angular/core' ;
2- import { defaultIfEmpty , filter , map , switchMap , take } from 'rxjs/operators' ;
3- import { AbstractSimpleItemActionComponent } from '../simple-item-action/abstract-simple-item-action.component' ;
1+ import { Component , Input , OnInit , OnDestroy } from '@angular/core' ;
2+ import { defaultIfEmpty , filter , map , switchMap , take } from 'rxjs/operators' ;
3+ import {
4+ AbstractSimpleItemActionComponent
5+ } from '../simple-item-action/abstract-simple-item-action.component' ;
46import { NgbModal , NgbModalRef } from '@ng-bootstrap/ng-bootstrap' ;
57import {
68 combineLatest as observableCombineLatest ,
79 combineLatest ,
810 Observable ,
9- of as observableOf
11+ of as observableOf , Subscription
1012} from 'rxjs' ;
1113import { RelationshipType } from '../../../core/shared/item-relationships/relationship-type.model' ;
1214import { VirtualMetadata } from '../virtual-metadata/virtual-metadata.component' ;
@@ -32,6 +34,7 @@ import { followLink } from '../../../shared/utils/follow-link-config.model';
3234import { getItemEditRoute } from '../../item-page-routing-paths' ;
3335import { RemoteData } from '../../../core/data/remote-data' ;
3436import { NoContent } from '../../../core/shared/NoContent.model' ;
37+ import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject' ;
3538
3639@Component ( {
3740 selector : 'ds-item-delete' ,
@@ -42,7 +45,7 @@ import { NoContent } from '../../../core/shared/NoContent.model';
4245 */
4346export class ItemDeleteComponent
4447 extends AbstractSimpleItemActionComponent
45- implements OnInit {
48+ implements OnInit , OnDestroy {
4649
4750 /**
4851 * The current url of this page
@@ -60,7 +63,7 @@ export class ItemDeleteComponent
6063 * A list of the relationship types for which this item has relations as an observable.
6164 * The list doesn't contain duplicates.
6265 */
63- types$ : Observable < RelationshipType [ ] > ;
66+ types$ : BehaviorSubject < RelationshipType [ ] > = new BehaviorSubject ( [ ] ) ;
6467
6568 /**
6669 * A map which stores the relationships of this item for each type as observable lists
@@ -84,6 +87,11 @@ export class ItemDeleteComponent
8487 */
8588 public modalRef : NgbModalRef ;
8689
90+ /**
91+ * Array to track all subscriptions and unsubscribe them onDestroy
92+ */
93+ private subs : Subscription [ ] = [ ] ;
94+
8795 constructor ( protected route : ActivatedRoute ,
8896 protected router : Router ,
8997 protected notificationsService : NotificationsService ,
@@ -113,8 +121,8 @@ export class ItemDeleteComponent
113121 this . url = this . router . url ;
114122
115123 const label = this . item . firstMetadataValue ( 'dspace.entity.type' ) ;
116- if ( label !== undefined ) {
117- this . types$ = this . entityTypeService . getEntityTypeByLabel ( label ) . pipe (
124+ if ( isNotEmpty ( label ) ) {
125+ this . subs . push ( this . entityTypeService . getEntityTypeByLabel ( label ) . pipe (
118126 getFirstSucceededRemoteData ( ) ,
119127 getRemoteDataPayload ( ) ,
120128 switchMap ( ( entityType ) => this . entityTypeService . getEntityTypeRelationships ( entityType . id ) ) ,
@@ -138,16 +146,14 @@ export class ItemDeleteComponent
138146 ) ,
139147 ) ;
140148 } )
141- ) ;
142- } else {
143- this . types$ = observableOf ( [ ] ) ;
149+ ) . subscribe ( ( types : RelationshipType [ ] ) => this . types$ . next ( types ) ) ) ;
144150 }
145151
146- this . types$ . pipe (
152+ this . subs . push ( this . types$ . pipe (
147153 take ( 1 ) ,
148154 ) . subscribe ( ( types ) =>
149155 this . objectUpdatesService . initialize ( this . url , types , this . item . lastModified )
150- ) ;
156+ ) ) ;
151157 }
152158
153159 /**
@@ -327,7 +333,7 @@ export class ItemDeleteComponent
327333 */
328334 performAction ( ) {
329335
330- this . types$ . pipe (
336+ this . subs . push ( this . types$ . pipe (
331337 switchMap ( ( types ) =>
332338 combineLatest (
333339 types . map ( ( type ) => this . isSelected ( type ) )
@@ -339,13 +345,14 @@ export class ItemDeleteComponent
339345 map ( ( selectedTypes ) => selectedTypes . map ( ( type ) => type . id ) ) ,
340346 )
341347 ) ,
342- ) . subscribe ( ( types ) => {
343- this . itemDataService . delete ( this . item . id , types ) . pipe ( getFirstCompletedRemoteData ( ) ) . subscribe (
344- ( rd : RemoteData < NoContent > ) => {
345- this . notify ( rd . hasSucceeded ) ;
346- }
347- ) ;
348- } ) ;
348+ switchMap ( ( types ) =>
349+ this . itemDataService . delete ( this . item . id , types ) . pipe ( getFirstCompletedRemoteData ( ) )
350+ )
351+ ) . subscribe (
352+ ( rd : RemoteData < NoContent > ) => {
353+ this . notify ( rd . hasSucceeded ) ;
354+ }
355+ ) ) ;
349356 }
350357
351358 /**
@@ -361,4 +368,14 @@ export class ItemDeleteComponent
361368 this . router . navigate ( [ getItemEditRoute ( this . item ) ] ) ;
362369 }
363370 }
371+
372+ /**
373+ * Unsubscribe from all subscriptions
374+ */
375+ ngOnDestroy ( ) : void {
376+ this . subs
377+ . filter ( ( sub ) => hasValue ( sub ) )
378+ . forEach ( ( sub ) => sub . unsubscribe ( ) ) ;
379+ }
380+
364381}
0 commit comments