rxjs

rxjs5 merge and error handling

会有一股神秘感。 提交于 2019-12-05 10:32:42
I would like to combine/merge multiple Observables and when each of them is completed execute a finally function. The merge operator seems to execute each subscription in parallel, which is what I need, but if any of them throws an error the execution is halted. RxJS version 4 has an operator mergeDelayError that should keep the all subscriptions executing till all of them are completed, but this operator isn't implemented in version 5 . Should I revert to a different operator? var source1 = Rx.Observable.of(1,2,3).delay(3000); var source2 = Rx.Observable.throw(new Error('woops')); var source3

RxJS first() for Observable.of() - no elements in sequence

六眼飞鱼酱① 提交于 2019-12-05 10:07:23
问题 For my tests I am trying to mock an event stream with Observable.of() but when I try const actions$ = Observable.of({}); ... // in the function that is tested actions$ .filter(action => action.type === 'LOAD_REQUEST') .first() .subscribe(() => { ... do something }); I get the following error EmptyError: no elements in sequence in xxx.js This only occurs when I use .first() . How can I mock the event stream so the tests don't fail? 回答1: .first() will emit exactly one item or throw an error (if

Angular2 observable http get conditional repeat

早过忘川 提交于 2019-12-05 10:04:14
I'm using angular2 observable pattern to make http requests. I'm trying to conditional repeat the http get: I want to execute the http get until a condition is met: http.get('url') .map(res => { // if the condition is met I should repeat the http get request }) .subscribe() Is there a way to conditional repeat the http get request? Thanks, Marco You can use expand operator. Here's an example: let request$ = http.get('url'); request$.expand(value => { return value !== 0 ? request$ : Rx.Observable.empty() }) .map(res => { //Do mapping here }) .subscribe() 来源: https://stackoverflow.com/questions

Angular2 Rxjs 404 error

半腔热情 提交于 2019-12-05 10:01:54
I have the following error when trying to start my Angular2 application: Failed to load resource: the server responded with a status of 404 (Not Found) angular2-polyfills.js:332 Error: Error: XHR error (404 Not Found) loading http://localhost:55707/rxjs(…) Here is my index.html: <!DOCTYPE html> <html> <head> <base href="/"> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Streak Maker</title> <link rel="icon" type="image/png" href="/images/favicon.png" /> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css

How to test Subject with jasmine marbles

帅比萌擦擦* 提交于 2019-12-05 09:55:57
Angular 6, Rxjs, Jest, Jasmine-marbles. very common scenario: a component that searches for server-side items. In the component, there are some controls that can change search critera, and I'd like to code in "reactive-style". So in the component code I have something like this: class SearchComponent implements OnInit { public searchData: SearchData = {}; public searchConditions$ = new Subject<SearchData>(); constructor(private searchService: SearchService) { } public results$: Observable<ResultData> = this.searchConditions$.pipe( distinctUntilChanged(this.compareProperties), // omissis but it

RxJS iif arguments are called when shouldn't

ε祈祈猫儿з 提交于 2019-12-05 09:53:59
I want to conditionally dispatch some actions using iif utility from RxJS. The problem is that second argument to iif is called even if test function returns false. This throws an error and app crashes immediately. I am new to to the power of RxJS so i probably don't know something. And i am using connected-react-router package if that matters. export const roomRouteEpic: Epic = (action$, state$) => action$.ofType(LOCATION_CHANGE).pipe( pluck('payload'), mergeMap(payload => iif( () => { console.log('NOT LOGGED'); return /^\/room\/\d+$/.test(payload.location.pathname); // set as '/login' },

Should I unsubscribe from Cold Observable?

别说谁变了你拦得住时间么 提交于 2019-12-05 09:07:45
I know that it's good practice to unsubscribe from Observable to prevent memory leak . But if it's Cold Observable should I also unsubscribe from it? For example one that is returned by Http.get() You dont need to do it because for HTTP observable is calling complete is immediately after action is done. From source code sources i can see that on unsubscribe is called on error and on complete. protected _error(err: any): void { this.destination.error(err); this.unsubscribe(); } protected _complete(): void { this.destination.complete(); this.unsubscribe(); } I went further and did small

Batching using RxJS?

旧街凉风 提交于 2019-12-05 08:44:06
I'm guessing this should be somewhat easy to achieve but I've having trouble (conceptually, I guess) figuring out how to tackle it. What I have is an API that returns an array of JSON objects. I need to step through these objects, and, for each object, make another AJAX call. The issue is the system that handles each AJAX call can only handle two active calls at a time (as it's quite a CPU-intensive task that hooks out into a desktop application). I was wondering how I could achieve this using RxJS (either using version 5 or 4)? EDIT: In addition, is it possible to have a chain of steps

Why use NGRX instead of constructor injected services?

泪湿孤枕 提交于 2019-12-05 08:22:45
Wondering why one would use NGRX or NGXS for an Angular app instead of constructor injected services to handle component IO? Is it only to ensure that component properties references are never mutated without switching out the entire property value reference or is there more to it? Altnernative to NGRX per the answer I developed: Slice . I believe it does everything NgRx / NgXS does (With the exception of a time machine - but this is easy to implement via delta notifications - already supported). but with zero boilerplate. Here's an article showcasing some of the capabilities: https://medium

RxJS - observable doesn't complete when an error occurs

萝らか妹 提交于 2019-12-05 08:21:10
问题 When I create an observable from scratch, and have the observer error, then complete, the done part of the subscription never is invoked. var observer = Rx.Observable.create(function(observer){ observer.onError(new Error('no!')); observer.onCompleted(); }) observer.subscribe( function(x) { console.log('succeeded with ' + x ) }, function(x) { console.log('errored with ' + x ) }, function() { console.log('completed') } ) The output is: errored with Error: no! I'd expect it to be: errored with