I need to check that username typed by user is unique. Thus, I'm sending an HTTP request for every input
event from the target element. And, of course, I need to debounce this operation, so as to get 1 HTTP request for X input
events following each other within a specific interval.
The problem is - as soon as I add switchMap
the code stops working and switchMap
callback is never called.
The working code without a switchMap
:
public static createUniqueFieldValidator(
target: 'username' | 'email' | 'phone',
checker: (value: string) => boolean,
userService: UserService
): AsyncValidatorFn {
return (control: AbstractControl): Observable<NullableType<ValidationErrors>> => {
if (!!control.value && checker(control.value)) {
return fromPromise<SomeReturnedType>(
userService.checkUnique({ [ target ]: control.value })
).pipe<NullableType<ValidationErrors>>(.......);
}
return of<null>(null);
};
}
If I add a switchMap
its callback is never called
public static createUniqueFieldValidator(
target: 'username' | 'email' | 'phone',
checker: (value: string) => boolean,
userService: UserService
): AsyncValidatorFn {
return (control: AbstractControl): Observable<NullableType<ValidationErrors>> => {
if (!!control.value && checker(control.value)) {
// We do get here, I've checked
return timer(300).pipe<NullableType<ValidationErrors>>(
switchMap<number, Observable<NullableType<ValidationErrors>>>(
(): Observable<NullableType<ValidationErrors>> => {
console.log('SwitchMap');// The whole callback is never called
// Here I'm supposed to make an HTTP request instead of returning a null
return of<null>(null);
}
)
);
}
return of<null>(null);
};
}
If I replace timer
with
of<string>(control.value).pipe<NullableType<ValidationErrors>>(delay(), switchMap.......)
it doesn't work either.
If I replace switchMap
with switchMapOf
it works but without a debounce time.
timer
ordebounceTime
? NOTE: Rememeber that if your inputs has another sync Validator and not pass, the asyncValidator is not executed – Eliseoof
/delay
option seems more reasonable to me but doesn't work either. It looks like Observable is stopped being subscribed to once you add aswitchMap
. – WeekendMan