Debouncing Actions
class SearchNews {
static readonly type = '[News] Search news';
constructor(public title: string) {}
}
@Component({
selector: 'app-news-portal',
template: `
<app-news-search [lastSearchedTitle]="lastSearchedTitle()" (search)="search($event)" />
<app-news [news]="news()" />
`,
standalone: true,
imports: [NewsSearchComponent, NewsComponents]
})
export class NewsPortalComponent {
news: Signal<News[]> = this.store.selectSignal(NewsState.getNews);
lastSearchedTitle = this.store.selectSignal(NewsState.getLastSearchedTitle);
constructor(
private store: Store,
actions$: Actions
) {
actions$
.pipe(
ofActionDispatched(SearchNews),
map((action: SearchNews) => action.title),
debounceTime(2000),
takeUntilDestroyed()
)
.subscribe(title => {
store.dispatch(new GetNews(title));
});
}
search(title: string): void {
this.store.dispatch(new SearchNews(title));
}
}Alternative Approach: Using cancelUncompleted
Last updated

