rxjs

Dispatch multiple actions from effects: difference between different rxjs operators

送分小仙女□ 提交于 2019-12-07 05:28:37
问题 I need to dispatch multiple actions from an ngrx effect once an http request is successful. There are several ways that seem to work and some that don't, I can't understand what the difference is. @Effect() loadUser = this.actions .pipe( ofType(campaignActions.type.LOAD_CAMPAIGN), map((action: userActions.LoadUserAction) => action.payload), switchMap((payload: { id: number }) => this.s.getUser(payload.id) .pipe( switchMap((data) => { return from([ new userActions.LoadUserSuccessAction(), new

How to test Subject with jasmine marbles

牧云@^-^@ 提交于 2019-12-07 05:26:59
问题 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

Synchronous stream of responses from a stream of requests with RxJS

时间秒杀一切 提交于 2019-12-07 05:23:18
问题 I'm new to RxJS and was wondering if anyone could help me. I want to create a synchronous stream of responses (preferably with the corresponding requests) from a stream of requests(payload data). I basically want the requests to be sent one by one, each waiting for the response from the last one. I tried this, but it sends everything at once ( jsbin ): var requestStream, responseStream; requestStream = Rx.Observable.from(['a','b','c','d','e']); responseStream = requestStream.flatMap(

Should I unsubscribe from Cold Observable?

别等时光非礼了梦想. 提交于 2019-12-07 05:22:14
问题 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() 回答1: 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

Batching using RxJS?

一世执手 提交于 2019-12-07 04:10:04
问题 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

angular2: how to test a component which has an observable time interval

天大地大妈咪最大 提交于 2019-12-07 04:00:35
问题 I have a slide-show component that has an Input array of slide objects and shows each one as long as it's been defined in slide.time of itself. also there are two buttons that clicking them has to slide to the next item and reset the timer. in order to make this work, I'm using Observables like this: /** * a "SUBJECT" for pausing(restarting) the slider's auto-slide on user's click on left and right arrows * @type {Subject} */ private pauser = new Subject(); /** * the main observable for timer

Unit test Angular 2 service subject

亡梦爱人 提交于 2019-12-07 03:35:49
问题 I'm trying to write a test for an angular service which has a Subject property and a method to call .next() on that subject. The service is the following: @Injectable() export class SubjectService { serviceSubjectProperty$: Subject<any> = new Subject(); callNextOnSubject(data: any) { this.serviceSubjectProperty$.next(data); } } And the test file for that service: import { TestBed, inject } from '@angular/core/testing'; import { SubjectService } from './subject.service'; describe(

rxjs periodic polling of an endpoint with a variable response time

走远了吗. 提交于 2019-12-07 03:19:18
问题 I want to poll an endpoint no faster than once a second, and no slower than the time it takes to poll the endpoint. There should never be more than one request outstanding. I want a reactive programming way to poll an endpoint at least once a second, but if the endpoint takes longer than 1 second, the next request fires immediately. In the marble diagram below, the 2nd and 3rd requests take longer than 1 second, but the 4th and 5th requests finish quicker. The next request fires either on the

Why should I use HttpClient over fetch?

五迷三道 提交于 2019-12-07 03:11:22
问题 Angular 2+ introduces HttpClient which makes an HTTP request and sends them down an RxJS observable. My question is why would I choose to use HttpClient's API over the standard fetch for making single HTTP requests? I'm familiar with RxJS and I understand this chart of "the four fundamental effects". | one | many ------------------------------------- sync | T | Iterable<T> async | Promise<T> | Observable<T> Http requests are async and return one value. Why should that be a Observable? I

Wrap an API function in an RxJs Observable

心不动则不痛 提交于 2019-12-07 02:59:28
问题 I am a newbie to RxJs and I have an API that I am using for geocoding that provides a function like the following: simpleGeocode(options) * where options = { address: {addr: ... }, success: Function, failure: Function}. The success function returns the geocoded LatLon object. I am using this in Angular app with NGRX Effects and so I would like it to have it as an Observable so I can use the standard Effect setup like: @Effect() public calculateLocation: Observable<void> = this.actions .ofType