rxjs

How do I obtain a rolling buffer of the last two items emitted from a reactive stream?

南笙酒味 提交于 2019-12-11 00:33:26
问题 I have a stream that emits numbers x . What I want is dx (difference in x) so I need a rolling buffer which emits x_n and x_(n-1) so I can map to dx = x_n - x_(n-1) . In a marble diagram this would look like ... SOURCE --A------B------C--------D------F--G----- RESULT ---------AB-----BC-------CD-----DF-FG---- This would be handy for other operations like rolling averages etc. I have checked the operator docs but can't seem to find anything similar. sample is sort of close but is time dependent

Subject and Observable, how to delete item, filter() list and next()

送分小仙女□ 提交于 2019-12-11 00:12:48
问题 I have a list of songs setup with Subject and Observable (shown with | async in view), and now I want to delete a song off the list, do some filter() and call next() on the Subject. How and where do I filter? Right now I am doing getValue() on Subject and passing that to next() on, well, Subject. This just seems wrong and circularish. I also tried subscribing to the Subject and getting the data that way, filtering it and calling next() inside subscribe() , but I got a RangeError. I could

ngrx - conditionally stop/remove Effect/Action

泄露秘密 提交于 2019-12-10 23:56:15
问题 I am currently building an application with Ionic2 and ngrx and I am trying to stop certian Actions if there is no network connection. With stop I mean to somehow make them invisible for other Effects and the store or stop them from further "propagating". Is there a way to do something like this? @Effect() checkNetworkConnection$ = this.actions$ .ofType(book.ActionTypes.LOAD_BOOKS, book.ActionTypes.CREATE_BOOK) .if(Network.connection === 'none') .do(() => new ShowNetworkAlertAction()) /

RxJs: how to get values emitted before we subscribe?

随声附和 提交于 2019-12-10 23:16:30
问题 With RxJs, once we start subscribe to an observable, we will start getting values once they are emitted, but how do I get all the values emitted by an observable before I've subscribed to it? 回答1: An observable is just a function that returns 0 or more values between now and the end of time. Like any other function it doesn't do anything before it's called (subscribed to). That being said, you can transform your observable to a hot observable by calling: // This makes the observable

Angular - subscribe multiple times without firing multiple calls?

徘徊边缘 提交于 2019-12-10 23:10:55
问题 I have a service call, whose response is cached inside my Angular service like this: public cacheMyServiceResponse(): Observable<any> { return this.appConfig.getEndpoint('myService') .pipe( switchMap((endpoint: Endpoint) => this.http.get(endpoint.toUrl())), tap((body: any) => { if (body) { // this endpoint can also return a 204 (No Content), where body is null this.cache = body.myData; } }), take(1), catchError(error => { this.errorService.trackError(error.status); return of(true); }) ); } So

How to ignore all from an observable if the other observable has data in RxJS?

拥有回忆 提交于 2019-12-10 22:33:16
问题 I have two observables, one receives data from the browser localstorage and the other is from the database through WebAPI . I want to subscribe to them so if the observable from the localstorage has data, don't initiate the one to get the data from the database . If the observable from the localstorage does not have any data, invoke the ajax call to get the data from the WebAPI . In the following example, I should get only 20, 40, 60, 80, 100 because the first observable has data. The second

Unable to import 'Rx' to my Angular 2 application

泄露秘密 提交于 2019-12-10 21:38:55
问题 I am pretty new at Angular2. I am trying to learn it using a dummy app. I have recently gone through a tutorial on RxJS and got a basic hold on Observables (or atleast I assume so). Based on that, I have an idea of returning a list of users from my array in service as a stream. I intent to use interval for it and display a kind of lazy loading effect on screen. My intention is something like: getUsers() { return Rx.Observable.from(this.users); //want to add interval too on this } However, I

Subscription Code Firing off Incrementally Every Time Component Loads

喜夏-厌秋 提交于 2019-12-10 21:28:46
问题 In my component's ngOnInit I have the following: public Subscription: Subscription; ngOnInit() { this.subscription = this.myService.currentData.subscribe( i => { this.currentData = i; this.function(this.currentData) }); } When my component loads, I have it subscribe to some data in a service to later use in a function. Loading it the first time works great. However, when I load to another module then come back, the function will fire off two times. Every time I repeat this process the

Why is rxjs not canceling my promise?

旧时模样 提交于 2019-12-10 19:12:17
问题 I am using rxjs in my angular app. I don't always get the data back in the order I want when multiple REST calls are made. Controller: constructor() { rx.Observable.fromPromise(this.getExpenses(toDate, fromDate))).subscribe(res => { that.setExpenses(res); }); } getExpenses(toDate, fromDate) { return this.expenseService.getExpenses(fromDate, toDate); } updateDate() { rx.Observable.fromPromise(this.getExpenses(toDate, fromDate))).subscribe(res => { that.setExpenses(res); }); } Service:

RxJs staircase when chaining Observable

与世无争的帅哥 提交于 2019-12-10 18:40:13
问题 Before I was using Promise with async/await syntax in Typescript looking as following const fooData = await AsyncFooData(); const barData = await AsyncBarData(); ... do something with fooData and barData If I do it with RxJs Observable<T> it becomes something like this for me AsyncFooData().subscribe(fooData => { AsyncBarData().subscribe(barData => { ... do something with fooData and barData }) }) Is there some better way to do this? Because it becomes fast not readable, ala Staircase, if I