Skip to content

Commit

Permalink
story/fix-fvm-dropdown unsubscribe subscriptions on destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
floris-thijssen-ritense committed Dec 31, 2024
1 parent e5ab5b3 commit 1138e64
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
import {Component, EventEmitter, Input, OnDestroy, OnInit, Output, ViewChild} from '@angular/core';
import moment from 'moment';
import {
BehaviorSubject,
Expand All @@ -22,7 +22,7 @@ import {
debounceTime,
EMPTY, filter,
Observable,
of, Subject,
of, Subject, Subscription,
switchMap,
take,
tap, withLatestFrom,
Expand Down Expand Up @@ -52,7 +52,7 @@ moment.defaultFormat = 'DD MMM YYYY HH:mm';
standalone: true,
imports: [CommonModule, FormioModule],
})
export class FormViewModelComponent implements OnInit {
export class FormViewModelComponent implements OnInit, OnDestroy {
@ViewChild('formio') formio: FormioComponent;

@Input() set options(optionsValue: any) {
Expand Down Expand Up @@ -153,6 +153,9 @@ export class FormViewModelComponent implements OnInit {
})
);

private focusSubscription: Subscription
private updateSubscription: Subscription

constructor(
private readonly viewModelService: ViewModelService,
private readonly translateService: TranslateService,
Expand All @@ -166,7 +169,7 @@ export class FormViewModelComponent implements OnInit {
this.loadInitialViewModel();
}

this.focus$.pipe()
this.focusSubscription = this.focus$
.pipe(withLatestFrom(this.change$))
.subscribe(data => {
const dataAtFocus = !!data[1] && !!data[1].data ? JSON.parse(JSON.stringify(data[1].data)) : null
Expand All @@ -181,7 +184,7 @@ export class FormViewModelComponent implements OnInit {
})
})

this.updateForm.pipe(filter(it => it), debounceTime(500)).subscribe(() => {
this.updateSubscription = this.updateForm.pipe(filter(it => it), debounceTime(500)).subscribe(() => {
if (this.isStartForm$.value) {
this.updateViewModelForStartForm();
} else {
Expand All @@ -190,6 +193,11 @@ export class FormViewModelComponent implements OnInit {
})
}

public ngOnDestroy(): void {
this.focusSubscription.unsubscribe()
this.updateSubscription.unsubscribe()
}

public beforeSubmitHook(instance: FormViewModelComponent): (submission, callback) => void {
return (submission, callback) => instance.beforeSubmit(submission, callback);
}
Expand Down

0 comments on commit 1138e64

Please sign in to comment.