rxjs

Wrap an API function in an RxJs Observable

╄→гoц情女王★ 提交于 2019-12-05 08:15:48
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(actions.CALCULATE_LOCATION) .switchMap((action) => { let location = action.payload; let options = {

rxjs periodic polling of an endpoint with a variable response time

微笑、不失礼 提交于 2019-12-05 08:08:43
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 1 second boundary, or immediately upon obtaining the data from the last outstanding request. s---s---s

Unit test Angular 2 service subject

房东的猫 提交于 2019-12-05 07:35:13
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('SubjectService', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [ SubjectService ] }); });

Rxjs: take(1) usage

南笙酒味 提交于 2019-12-05 06:40:30
I have seen a tutorial on rxjs as like this. My question is: 1) What is the usage of take(1) here? I see a lot of explanation online but i don't really get it. Also, i don't see any benefit of using take(1) in this code. And the author do use take(1) on every return function in REST api related service. 2) The author did not unsubscribe after subscribe. Is it because the author use take(1) therefore manual unsubscribe is not needed? 3) What if i want to implement catch function. Should i implement it before take or after take. getProfile() { // this is a call to REST API return this.service

Debounce without initial delay

丶灬走出姿态 提交于 2019-12-05 06:39:37
Is there an operator in RxJS that debounces without delaying the "first event in a burst", but delaying (and always emitting) the "last event in a burst"? Something like this: ---a----b-c-d-----e-f--- after awesome-debounce(2 dashes) becomes: ---a----b------d--e----f while a normal debounce would be: -----a---------d-------f It's kind of a mix between throttle and debounce... martin Hmmm, this is the easiest solution I can think of. The interesting part for you is the awesomeDebounce() function that creates the sub-chain. It basically just combines throttle() and debounceTime() operators:

Mock ngrx store selectors with parameters in unit tests (Angular)

拈花ヽ惹草 提交于 2019-12-05 06:22:43
问题 I am trying to write unit tests for a service in Angular. I want to mock the store.select function of ngrx, so I can test how let's say, a service, reacts to different values returned by the store selectors. I want to be able to mock each selector individually. My main problem is how to mock parametrised selectors. I have previously used a BehaviourSubject that I map to the select function, but this doesn't allow you to return different values for different selectors. It is not readable

RxJS publishReplay vs publishLast

只谈情不闲聊 提交于 2019-12-05 05:42:07
I am implementing caching HTTP results in Angular application. From what I know both of the following code works, but I need to know if they are doing exactly the same thing, or I am missing something important? publishLast getPosts() { if( !this.posts$ ) { this.posts$ = this.http.get('api').publishLast().refCount(); return this.posts$; } return this.posts$; } publishReplay getPosts() { if( !this.posts$ ) { this.posts$ = this.http.get('api').publishReplay(1).refCount(); return this.posts$; } return this.posts$; } publishLast shares (as the name suggests) the last emitted value - which can only

Invoking epics from within other epics

 ̄綄美尐妖づ 提交于 2019-12-05 05:26:19
问题 First of all many props for this awesome library! I am still struggling with an use case though. I would like to call another epic first and wait till it completes before continuing the current epic. Let's say I have a form where an user can change things. Before loading other data into the form, I want to save the current changes first. Any thoughts on this? 回答1: Sounds like you want: if one epic would like to kick off another epics work and wait for it to complete before proceeding. One

Angular - HTTP interceptor to retry requests with specific error status?

人盡茶涼 提交于 2019-12-05 05:09:44
问题 I am trying to use an interceptor to handle http errors and retry for a special error status, in my case the status code 502. intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(request) .pipe( retryWhen(errors => { return errors .pipe( mergeMap(error => (error.status === 502) ? throwError(error) : of(error)), take(2) ) }) ) } But it's not working, whereas when I am using retry() in this fashion, it's working perfectly intercept(request:

How to implement intervals/polling in angular2 to work with protractor?

扶醉桌前 提交于 2019-12-05 04:48:00
I have an angular2 app I want to test with protractor. In this app I have a page with a graph that is being updated in regular intervals with autogenerated data. Apparently one feature of protractor is waiting for scripts and http calls to finish before executing test code. However, if there is a constantly polling script that never finishes, protractor will wait forever and time out after a certain time. In angular1 this could be solved by implementing the polling with $interval , which protractor does not wait for. Unfortunately in angular2 there is no $interval and the correct way to