Fixed issue with a subscribe when section components are destroyed

This commit is contained in:
Giuseppe
2018-10-24 14:54:31 +02:00
parent e2725086b5
commit 9eb00b9e62
4 changed files with 20 additions and 10 deletions

View File

@@ -35,7 +35,7 @@ import { difference } from '../../../shared/object.util';
templateUrl: './section-form.component.html', templateUrl: './section-form.component.html',
}) })
@renderSectionFor(SectionsType.SubmissionForm) @renderSectionFor(SectionsType.SubmissionForm)
export class FormSectionComponent extends SectionModelComponent implements OnDestroy { export class FormSectionComponent extends SectionModelComponent {
public formId; public formId;
public formModel: DynamicFormControlModel[]; public formModel: DynamicFormControlModel[];
@@ -90,7 +90,7 @@ export class FormSectionComponent extends SectionModelComponent implements OnDes
}); });
} }
ngOnDestroy() { onSectionDestroy() {
this.subs this.subs
.filter((subscription) => hasValue(subscription)) .filter((subscription) => hasValue(subscription))
.forEach((subscription) => subscription.unsubscribe()); .forEach((subscription) => subscription.unsubscribe());

View File

@@ -30,7 +30,7 @@ import { FormComponent } from '../../../shared/form/form.component';
templateUrl: './section-license.component.html', templateUrl: './section-license.component.html',
}) })
@renderSectionFor(SectionsType.License) @renderSectionFor(SectionsType.License)
export class LicenseSectionComponent extends SectionModelComponent implements OnDestroy { export class LicenseSectionComponent extends SectionModelComponent {
public formId; public formId;
public formModel: DynamicFormControlModel[]; public formModel: DynamicFormControlModel[];
@@ -140,7 +140,7 @@ export class LicenseSectionComponent extends SectionModelComponent implements On
} }
} }
ngOnDestroy() { onSectionDestroy() {
this.subs this.subs
.filter((subscription) => hasValue(subscription)) .filter((subscription) => hasValue(subscription))
.forEach((subscription) => subscription.unsubscribe()); .forEach((subscription) => subscription.unsubscribe());

View File

@@ -1,8 +1,9 @@
import { Inject, OnInit } from '@angular/core'; import { Inject, OnDestroy, OnInit } from '@angular/core';
import { SectionDataObject } from './section-data.model'; import { SectionDataObject } from './section-data.model';
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { SectionsService } from '../sections.service'; import { SectionsService } from '../sections.service';
import { isNotUndefined } from '../../../shared/empty.util'; import { hasValue, isNotUndefined } from '../../../shared/empty.util';
import { Subscription } from 'rxjs/Subscription';
export interface SectionDataModel { export interface SectionDataModel {
sectionData: SectionDataObject sectionData: SectionDataObject
@@ -11,12 +12,13 @@ export interface SectionDataModel {
/** /**
* An abstract model class for a submission edit form section. * An abstract model class for a submission edit form section.
*/ */
export abstract class SectionModelComponent implements OnInit, SectionDataModel { export abstract class SectionModelComponent implements OnDestroy, OnInit, SectionDataModel {
protected abstract sectionService: SectionsService; protected abstract sectionService: SectionsService;
collectionId: string; collectionId: string;
sectionData: SectionDataObject; sectionData: SectionDataObject;
submissionId: string; submissionId: string;
protected valid: boolean; protected valid: boolean;
private sectionStatusSub: Subscription;
public constructor(@Inject('collectionIdProvider') public injectedCollectionId: string, public constructor(@Inject('collectionIdProvider') public injectedCollectionId: string,
@Inject('sectionDataProvider') public injectedSectionData: SectionDataObject, @Inject('sectionDataProvider') public injectedSectionData: SectionDataObject,
@@ -33,13 +35,21 @@ export abstract class SectionModelComponent implements OnInit, SectionDataModel
protected abstract getSectionStatus(): Observable<boolean>; protected abstract getSectionStatus(): Observable<boolean>;
protected abstract onSectionInit(): void; protected abstract onSectionInit(): void;
protected abstract onSectionDestroy(): void;
protected updateSectionStatus(): void { protected updateSectionStatus(): void {
this.getSectionStatus() this.sectionStatusSub = this.getSectionStatus()
.filter((sectionStatus: boolean) => isNotUndefined(sectionStatus)) .filter((sectionStatus: boolean) => isNotUndefined(sectionStatus))
.startWith(true) .startWith(true)
.subscribe((sectionStatus: boolean) => { .subscribe((sectionStatus: boolean) => {
this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, sectionStatus); this.sectionService.setSectionStatus(this.submissionId, this.sectionData.id, sectionStatus);
}); });
} }
ngOnDestroy(): void {
this.onSectionDestroy();
if (hasValue(this.sectionStatusSub)) {
this.sectionStatusSub.unsubscribe();
}
}
} }

View File

@@ -33,7 +33,7 @@ export const POLICY_DEFAULT_WITH_LIST = 2; // Banner2
templateUrl: './section-upload.component.html', templateUrl: './section-upload.component.html',
}) })
@renderSectionFor(SectionsType.Upload) @renderSectionFor(SectionsType.Upload)
export class UploadSectionComponent extends SectionModelComponent implements OnDestroy { export class UploadSectionComponent extends SectionModelComponent {
public AlertTypeEnum = AlertType; public AlertTypeEnum = AlertType;
public fileIndexes = []; public fileIndexes = [];
@@ -205,7 +205,7 @@ export class UploadSectionComponent extends SectionModelComponent implements OnD
/** /**
* Method provided by Angular. Invoked when the instance is destroyed. * Method provided by Angular. Invoked when the instance is destroyed.
*/ */
ngOnDestroy() { onSectionDestroy() {
this.subs this.subs
.filter((subscription) => hasValue(subscription)) .filter((subscription) => hasValue(subscription))
.forEach((subscription) => subscription.unsubscribe()); .forEach((subscription) => subscription.unsubscribe());