Debouncing Actions
class SearchNews {
static readonly type = '[News] Search news';
constructor(public title: string) {}
}
@Component({
selector: 'app-news-portal',
template: `
<app-news-search
(search)="search($event)"
[lastSearchedTitle]="lastSearchedTitle$ | async"
></app-news-search>
<app-news [news]="news$ | async"></app-news>
`
})
export class NewsPortalComponent implements OnDestroy {
@Select(NewsState.getNews) news$: Observable<News[]>;
lastSearchedTitle$ = this.store.selectOnce(NewsState.getLastSearchedTitle);
private destroy$ = new Subject<void>();
constructor(private store: Store, actions$: Actions) {
actions$
.pipe(
ofActionDispatched(SearchNews),
map((action: SearchNews) => action.title),
debounceTime(2000),
takeUntil(this.destroy$)
)
.subscribe(title => {
store.dispatch(new GetNews(title));
});
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
search(title: string): void {
this.store.dispatch(new SearchNews(title));
}
}Last updated

