rxjs

Why would I use RxJS interval() or timer() polling instead of window.setInterval()?

江枫思渺然 提交于 2020-11-30 06:28:47
问题 Use case: Call a function every minute (60000 ms) that dispatches store action to fetch lastUpdated status of items, which upon response and filtering, updates the store, and updated store is read as an observable and displayed in the view). This needs to happen for as long as the web app is open (so indefinitely). Currently, I'm using this: this.refreshDate = window.setInterval( () => this.store.dispatch(new FetchLastUpdate()) , 60000); And when view is destroyed/dismounted, I delete the

Observable from <button> click event in Angular2

浪尽此生 提交于 2020-11-30 02:58:52
问题 What's the preferred way to create an observable from a button's onclick event using Angular 2? I'm not sure if it's considered best practice to grab the native element from the DOM in the component code (how do I do this?), or if there's some other shortcut I don't know about. 回答1: You can use Observable.fromEvent like explained in Angular2 RxJS getting 'Observable_1.Observable.fromEvent is not a function' error Or just forward to an observable like private obs = new Subject(); public obs$ =

map vs switchMap in RxJS

ε祈祈猫儿з 提交于 2020-11-29 04:34:31
问题 I read the documentation of switchMap and map, but I still don't completely understand the difference. Are there some cases where it does not make a difference at all? 回答1: Both operators are different. switchMap : Maps values to observable. Cancels the previous inner observable. Eg: fromEvent(document, 'click') .pipe( // restart counter on every click // First click: 0, 1, 2... // Second click: cancels the previous interval and starts new one. 0, 1, 2... switchMap(() => interval(1000)) )