1- import { Component , Input , OnInit } from '@angular/core' ;
1+ import { Component , Input , OnInit , OnDestroy } from '@angular/core' ;
22import { defaultIfEmpty , filter , map , switchMap , take } from 'rxjs/operators' ;
33import {
44 AbstractSimpleItemActionComponent
88 combineLatest as observableCombineLatest ,
99 combineLatest ,
1010 Observable ,
11- of as observableOf
11+ of as observableOf , Subscription
1212} from 'rxjs' ;
1313import { RelationshipType } from '../../../core/shared/item-relationships/relationship-type.model' ;
1414import { VirtualMetadata } from '../virtual-metadata/virtual-metadata.component' ;
@@ -45,7 +45,7 @@ import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';
4545 */
4646export class ItemDeleteComponent
4747 extends AbstractSimpleItemActionComponent
48- implements OnInit {
48+ implements OnInit , OnDestroy {
4949
5050 /**
5151 * The current url of this page
@@ -87,6 +87,11 @@ export class ItemDeleteComponent
8787 */
8888 public modalRef : NgbModalRef ;
8989
90+ /**
91+ * Array to track all subscriptions and unsubscribe them onDestroy
92+ */
93+ private subs : Subscription [ ] = [ ] ;
94+
9095 constructor ( protected route : ActivatedRoute ,
9196 protected router : Router ,
9297 protected notificationsService : NotificationsService ,
@@ -117,7 +122,7 @@ export class ItemDeleteComponent
117122
118123 const label = this . item . firstMetadataValue ( 'dspace.entity.type' ) ;
119124 if ( isNotEmpty ( label ) ) {
120- this . entityTypeService . getEntityTypeByLabel ( label ) . pipe (
125+ this . subs . push ( this . entityTypeService . getEntityTypeByLabel ( label ) . pipe (
121126 getFirstSucceededRemoteData ( ) ,
122127 getRemoteDataPayload ( ) ,
123128 switchMap ( ( entityType ) => this . entityTypeService . getEntityTypeRelationships ( entityType . id ) ) ,
@@ -141,16 +146,14 @@ export class ItemDeleteComponent
141146 ) ,
142147 ) ;
143148 } )
144- ) . subscribe ( ( types : RelationshipType [ ] ) => this . types$ . next ( types ) ) ;
145- } else {
146- this . types$ . next ( [ ] ) ;
149+ ) . subscribe ( ( types : RelationshipType [ ] ) => this . types$ . next ( types ) ) ) ;
147150 }
148151
149- this . types$ . pipe (
152+ this . subs . push ( this . types$ . pipe (
150153 take ( 1 ) ,
151154 ) . subscribe ( ( types ) =>
152155 this . objectUpdatesService . initialize ( this . url , types , this . item . lastModified )
153- ) ;
156+ ) ) ;
154157 }
155158
156159 /**
@@ -330,7 +333,7 @@ export class ItemDeleteComponent
330333 */
331334 performAction ( ) {
332335
333- this . types$ . pipe (
336+ this . subs . push ( this . types$ . pipe (
334337 switchMap ( ( types ) =>
335338 combineLatest (
336339 types . map ( ( type ) => this . isSelected ( type ) )
@@ -342,13 +345,14 @@ export class ItemDeleteComponent
342345 map ( ( selectedTypes ) => selectedTypes . map ( ( type ) => type . id ) ) ,
343346 )
344347 ) ,
345- ) . subscribe ( ( types ) => {
346- this . itemDataService . delete ( this . item . id , types ) . pipe ( getFirstCompletedRemoteData ( ) ) . subscribe (
347- ( rd : RemoteData < NoContent > ) => {
348- this . notify ( rd . hasSucceeded ) ;
349- }
350- ) ;
351- } ) ;
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+ ) ) ;
352356 }
353357
354358 /**
@@ -364,4 +368,14 @@ export class ItemDeleteComponent
364368 this . router . navigate ( [ getItemEditRoute ( this . item ) ] ) ;
365369 }
366370 }
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+
367381}
0 commit comments