I have the following code in a a module where I've implemented a dynamic forms functionality using Angular 4 and reactive forms:
constructor(private formActions: FormActions, private fb: FormBuilder, private uxService: SygmaUxService) {
}
ngOnInit() {
this.form = this.fb.group({});
this.formActions.getForm().subscribe((form) => {
this.form = this.fb.group({});
this.form.valueChanges.subscribe((data) => {
this.formActions.setCurrentFormValues(data);
this.formActions.setCurrentFormStatus(this.form.status.toLowerCase());
});
this.formConfig = form.formConfig;
this.formData = form.formData;
this.uxService.unblockDocument();
});
this.formActions.getFormRules().subscribe((rules) => {
this.formRules = rules;
});
}
As you can see first I subscribe to the form to execute that logic every time user selects a new form, and inside that subscribe function I first assign the form to a completely new instance this.form = this.fb.group({});
and then, I need to subscribe to the changes of that new instance this.form.valueChanges.subscribe(...)
.
So basically, every single time the user switches the form, I'll be creating a new subscription to the changes of that form, but what about the other subscriptions to the other form instances that I don't use anymore and I never unsubscribe? Is there any kind of automatic garbage collection?
I assume that the behaviour I should expect is that when I assign a new form instance this.form = this.fb.group({});
, the old form instance will be elegible for garbage collection and the subscription to that instance will be automatically removed once that instance is destroyed and garbage collected ? Am I correct on this?