Skip to content

Commit 7abd20b

Browse files
alekarthamzahamidi
authored andcommitted
Fix multiple subscriptions to same observables on form activation
1 parent 880d9c2 commit 7abd20b

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

projects/ajsf-core/src/lib/json-schema-form.component.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
SimpleChanges,
1515
} from '@angular/core';
1616
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
17+
import { Subject } from 'rxjs';
18+
import { takeUntil } from 'rxjs/operators';
1719
import { convertSchemaToDraft6 } from './shared/convert-schema-to-draft6.function';
1820
import { forEach, hasOwn } from './shared/utility.functions';
1921
import { FrameworkLibraryService } from './framework-library/framework-library.service';
@@ -79,6 +81,9 @@ export const JSON_SCHEMA_FORM_VALUE_ACCESSOR: any = {
7981
providers: [ JsonSchemaFormService, JSON_SCHEMA_FORM_VALUE_ACCESSOR ],
8082
})
8183
export class JsonSchemaFormComponent implements ControlValueAccessor, OnChanges, OnInit {
84+
// TODO: quickfix to avoid subscribing twice to the same emitters
85+
private unsubscribeOnActivateForm$ = new Subject<void>();
86+
8287
debugOutput: any; // Debug information, if requested
8388
formValueSubscription: any = null;
8489
formInitialized = false;
@@ -667,7 +672,7 @@ export class JsonSchemaFormComponent implements ControlValueAccessor, OnChanges,
667672
* and activate the form.
668673
*/
669674
private activateForm() {
670-
675+
this.unsubscribeOnActivateForm$.next();
671676
// If 'schema' not initialized
672677
if (isEmpty(this.jsf.schema)) {
673678

@@ -721,17 +726,17 @@ export class JsonSchemaFormComponent implements ControlValueAccessor, OnChanges,
721726
// }
722727

723728
// Subscribe to form changes to output live data, validation, and errors
724-
this.jsf.dataChanges.subscribe(data => {
729+
this.jsf.dataChanges.pipe(takeUntil(this.unsubscribeOnActivateForm$)).subscribe(data => {
725730
this.onChanges.emit(this.objectWrap ? data['1'] : data);
726731
if (this.formValuesInput && this.formValuesInput.indexOf('.') === -1) {
727732
this[`${this.formValuesInput}Change`].emit(this.objectWrap ? data['1'] : data);
728733
}
729734
});
730735

731736
// Trigger change detection on statusChanges to show updated errors
732-
this.jsf.formGroup.statusChanges.subscribe(() => this.changeDetector.markForCheck());
733-
this.jsf.isValidChanges.subscribe(isValid => this.isValid.emit(isValid));
734-
this.jsf.validationErrorChanges.subscribe(err => this.validationErrors.emit(err));
737+
this.jsf.formGroup.statusChanges.pipe(takeUntil(this.unsubscribeOnActivateForm$)).subscribe(() => this.changeDetector.markForCheck());
738+
this.jsf.isValidChanges.pipe(takeUntil(this.unsubscribeOnActivateForm$)).subscribe(isValid => this.isValid.emit(isValid));
739+
this.jsf.validationErrorChanges.pipe(takeUntil(this.unsubscribeOnActivateForm$)).subscribe(err => this.validationErrors.emit(err));
735740

736741
// Output final schema, final layout, and initial data
737742
this.formSchema.emit(this.jsf.schema);

0 commit comments

Comments
 (0)