I'm using the angular material's table component, mat-table, along with pagination.
I have the filter method that serves when the user types a text in the field to do a search, it is sent to the backend, the text, the size and the page.
However, I tried to fit the switchmap, to cancel the previous requests, because with each typed letter, the method is triggered and sends a call to the server. So I want to cancel all previous calls and leave only the last one.
I'm already using the debounceTime to define a time after the user finishes typing to start the calls.
Below is the filter method that is triggered when the user starts typing in the search field.
_search$: Subject<string> = new Subject<string>();
search = '';
applyFilter(search: string) {
this._search$.next(search.toLocaleLowerCase());
this._search$.pipe(
debounceTime(GlobalVariables.DEBOUNCETIME),
)
.subscribe((data: string) => {
this.currentPage = 0;
this.loadData(data, this.pageSize, this.currentPage);
});
}
This is another method that sends a request to the database and repopulates the table.
loadData(search: string = '', limite: number, pagina: number) {
if (search === 'sim') {
search= 'true';
} else if (search === 'nao' || search === 'não') {
search= 'false';
}
this.service.listartecnicosPaginacao(search, limite, pagina).subscribe((res: any) => {
this.dataSource.data = res['lista'];
setTimeout(() => {
this.paginator.pageIndex = this.currentPage;
this.paginator.length = res.totalElemento;
});
});
}
I tried to do it as below.
applyFilter(search: string) {
this._search$.next(search.toLocaleLowerCase());
this.currentPage = 0;
this._search$.pipe(
debounceTime(VariaveisGlobais.DEBOUNCETIME),
switchMap(data => this.loadData(data, this.pageSize, this.currentPage))
)
.subscribe();
}
And I get it as a message.
(method) ListTableComponent.loadData(busca: string | undefined, limite: number, pagina: number): voidType 'void' is not assignable to type 'ObservableInput<any>'.ts(2322) switchMap.d.ts(2, 79): The expected type comes from the return type of this signature.list-table.component.ts(94, 17): Did you mean to mark this function as 'async'?
And even putting the switchMap((data:any) => xxxx()) in this way, the message above continues.