fix: Debounce operator instead of manual timeouts.
This commit is contained in:
@@ -19,7 +19,7 @@ import {
|
|||||||
} from '@angular/forms'
|
} from '@angular/forms'
|
||||||
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'
|
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'
|
||||||
import { MatFormFieldControl } from '@angular/material/form-field'
|
import { MatFormFieldControl } from '@angular/material/form-field'
|
||||||
import { Subject } from 'rxjs'
|
import { debounceTime, filter, Subject } from 'rxjs'
|
||||||
import { environment } from 'src/environments/environment'
|
import { environment } from 'src/environments/environment'
|
||||||
|
|
||||||
export interface UserSearchResult {
|
export interface UserSearchResult {
|
||||||
@@ -52,9 +52,8 @@ export class UserSearchComponent
|
|||||||
OnDestroy,
|
OnDestroy,
|
||||||
DoCheck {
|
DoCheck {
|
||||||
protected loading: boolean = false
|
protected loading: boolean = false
|
||||||
control: FormControl = new FormControl()
|
control: FormControl = new FormControl<UserSearchResult | string>("")
|
||||||
protected list: UserSearchResult[] = []
|
protected list: UserSearchResult[] = []
|
||||||
private timeout?: NodeJS.Timeout
|
|
||||||
private _onChange!: (_: UserSearchResult) => void
|
private _onChange!: (_: UserSearchResult) => void
|
||||||
private _onTouched!: any
|
private _onTouched!: any
|
||||||
|
|
||||||
@@ -158,19 +157,15 @@ export class UserSearchComponent
|
|||||||
if (this.ngControl != null) {
|
if (this.ngControl != null) {
|
||||||
; (this.ngControl as NgControl).valueAccessor = this
|
; (this.ngControl as NgControl).valueAccessor = this
|
||||||
}
|
}
|
||||||
this.control.valueChanges.subscribe(() => {
|
this.control.valueChanges.pipe(debounceTime(500), filter(v => typeof v == "string")).subscribe(v => {
|
||||||
if (typeof this.control.value == 'object') return
|
|
||||||
this.loading = true
|
this.loading = true
|
||||||
if (this.timeout) clearTimeout(this.timeout)
|
this.http.get<UserSearchResult[]>(environment.apiEndpoint + `/admin/usearch`, {
|
||||||
this.timeout = setTimeout(() => {
|
params: { q: v },
|
||||||
this.http.get<any[]>(environment.apiEndpoint + `/admin/usearch`, {
|
|
||||||
params: { q: this.control.value },
|
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
}).subscribe(v => {
|
}).subscribe(v => {
|
||||||
this.list = v
|
this.list = v
|
||||||
this.loading = false
|
this.loading = false
|
||||||
})
|
})
|
||||||
}, 500)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
ngDoCheck(): void {
|
ngDoCheck(): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user