clean up router event subscription
authorGalen Charlton <gmc@equinoxinitiative.org>
Wed, 26 Feb 2020 21:59:46 +0000 (16:59 -0500)
committerGalen Charlton <gmc@equinoxinitiative.org>
Wed, 26 Feb 2020 21:59:46 +0000 (16:59 -0500)
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/eg2/src/app/staff/acq/search/acq-search.component.ts

index fca4c6a..43e3edb 100644 (file)
@@ -1,6 +1,8 @@
-import {Component, OnInit, AfterViewInit, ViewChild, ViewChildren, QueryList} from '@angular/core';
+import {Component, OnInit, AfterViewInit, ViewChild, ViewChildren, QueryList, OnDestroy} from '@angular/core';
 import {NgbTabset, NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
-import {Router, ActivatedRoute, ParamMap, NavigationEnd} from '@angular/router';
+import {Router, ActivatedRoute, ParamMap, RouterEvent, NavigationEnd} from '@angular/router';
+import {filter, takeUntil} from 'rxjs/operators';
+import {Subject} from 'rxjs';
 import {StaffCommonModule} from '@eg/staff/common.module';
 import {IdlService, IdlObject} from '@eg/core/idl.service';
 import {PcrudService} from '@eg/core/pcrud.service';
@@ -14,7 +16,7 @@ import {PicklistResultsComponent} from './picklist-results.component';
   templateUrl: './acq-search.component.html'
 })
 
-export class AcqSearchComponent implements OnInit, AfterViewInit {
+export class AcqSearchComponent implements OnInit, AfterViewInit, OnDestroy {
 
     searchType = '';
     validSearchTypes = ['lineitems', 'purchaseorders', 'invoices', 'selectionlists'];
@@ -30,6 +32,7 @@ export class AcqSearchComponent implements OnInit, AfterViewInit {
     @ViewChildren(PicklistResultsComponent) plResults: QueryList<PurchaseOrderResultsComponent>;
 
     previousUrl: string = null;
+    public destroyed = new Subject<any>();
 
     constructor(
         private router: Router,
@@ -69,7 +72,10 @@ export class AcqSearchComponent implements OnInit, AfterViewInit {
                 this.ngOnInit(); // TODO: probably overkill
             });
         });
-        this.router.events.subscribe(routeEvent => {
+        this.router.events.pipe(
+            filter((event: RouterEvent) => event instanceof NavigationEnd),
+            takeUntil(this.destroyed)
+        ).subscribe(routeEvent => {
             if (routeEvent instanceof NavigationEnd) {
                 // force reset of grid data source if we're navigating from
                 // a search tab to the same search tab
@@ -126,4 +132,9 @@ export class AcqSearchComponent implements OnInit, AfterViewInit {
 
     ngAfterViewInit() {}
 
+    ngOnDestroy(): void {
+        this.destroyed.next();
+        this.destroyed.complete();
+    }
+
 }