rxjs

How to use EventManager to listen to window.resize events in angular?

最后都变了- 提交于 2019-12-23 12:04:13
问题 I'm borrowing some code from this stackoverflow: Angular window resize event The author of the answer says I should be using the EventManager if I want to listen for window events from a service and not break Angular Universal. That being said, is this answer still true? If so, could someone show me why when I subscribe to the onResize$ Observable logs nothing when the window is resized? import { EventManager } from '@angular/platform-browser'; import { Observable } from 'rxjs/Observable';

Debouncetime in Angular6 ngModelChange

て烟熏妆下的殇ゞ 提交于 2019-12-23 11:39:10
问题 I have a complex calculator app written in Angular6 which calculates the results based of several inputs in the ngModelChange event and to show these results in charts directly. The calculation is done server side. Now I want to add a debouncetime, so the server dont receive a request with every key pressed. My calculation method which is fires in the ngModelChange looks like this: async calculate(){ if(this.checkInputs()){ try{ let returnDto = await this.webApiService.calculate(new

Feedback loop without Subject in RX

回眸只為那壹抹淺笑 提交于 2019-12-23 10:15:17
问题 I have the following motion equations move = target_position - position position = position + move where target_position is a stream and position is initialized at zero. I would like to have a stream of position. I have tried something like this (in rx pseudo code) moves = Subject() position = moves.scan(sum,0) target_position.combine_latest(position,diff).subscribe( moves.on_next) It works but I have read that using Subject should be avoided. Is it possible to compute the position stream

Why `combineLatest` returns OperatorFunction<{}, number>

十年热恋 提交于 2019-12-23 09:32:09
问题 I am using rxjs 6 I have two observables in my code import { combineLatest } from 'rxjs/operators'; loading$: Observable<boolean>; loaded$: Observable<boolean>; The observables are not coming from http request but from the store I need to combine/convert the sequences in to one single observable value based on this logic: If the sequence true , true or false , false - need to return new observable true otherwise need to return false I tried to use combineLatest to achieve that: combineLatest(

Handle Error in RxJs flatMap stream and continue processing

与世无争的帅哥 提交于 2019-12-23 09:26:32
问题 I am using RxJs in an Angular 2 application to fetch data from an API for multiple pages in parallel and save any failed requests for future re-try. To do so, I want to catch errors generated by flatMap-ing http get requests (code below) and continue with further stream processing. In case of error, my current solution causes stream to discontinue. Rx.Observable.range(1, 5) .flatMap(pageNo => { params.set('page', ''+pageNo); return this.http.get(this.API_GET, params) .catch( (err) => { //save

Angular HttpClient - rxjs map - to an array of Type

拈花ヽ惹草 提交于 2019-12-23 09:26:15
问题 I have the following HttpClient.post call which return an array of Objects. import { map } from 'rxjs/operators/map'; public getArray(profileId: number): Observable<any> { return this.http.post("/api/url", postObject, HttpSettings.GetDefaultHttpRequestOptions()) .pipe(map(data => { console.log(data); // logs (2) [{…}, {…}] return data; })); } I need to Instantiate an Array of objects. I can't just type assert because I need the Thing constructor to parse some json and other. Essentially what

Returning caught error observable from catchError in HttpInterceptor causes error loop

自闭症网瘾萝莉.ら 提交于 2019-12-23 09:24:53
问题 I have a simple interceptor that handles requests and catches any http error using RXJS catchError. The second argument received in catchError is the caught observable. I want to, in some cases, return this error and let it propagate up to the error handler in the subscribe function. The problem is that returning th caught error causes an infinite loop (as seen in the example: https://stackblitz.com/edit/angular-u4gakr) The intercept function in the interceptor where the catchError is stuck

Sequential subscription to an array of observables

心已入冬 提交于 2019-12-23 09:17:30
问题 Here, I've used forkJoin from rxjs to subscribe to an array of observables parallelly. But I want to subscribe to them one by one, What will be the best solution? Below is my code : var observables = []; Observable.forkJoin(observables) .subscribe(() => { this.msgs = []; this.msgs.push({ severity: 'success', summary: 'Saved Successfully' }); this.onSaveComplete(); }, (error: any) => this.errorMessage = <any>error); }, (error: any) => this.errorMessage = <any>error); 回答1: Alternate of forkJoin

Should we create one Epic per action type? in redux-observable

為{幸葍}努か 提交于 2019-12-23 09:15:04
问题 I am in process of redux-observable learning, and I have some doubts: Should we create an Epic for each action to watch? export const actionEpic = action $ => action$.ofType('ACTION') export const action2Epic = action $ => action$.ofType('ACTION2') Or can we create it for many like reducers with switch? import every single Epic to convine in the middleware is a lot of work 回答1: A large majority of epics will begin by matching a single action, e.g. action$.ofType(SOMETHING) . This is because

What should I use instead of toPromise() when using await on an Observable?

 ̄綄美尐妖づ 提交于 2019-12-23 09:14:55
问题 This page says "toPromise has been deprecated! (RxJS 5.5+)" but I've been using it lately with AngularFire2 (when I only want one result) like this: const foo = await this.afs.doc(`docPath`).valueChanges().toPromise(); Should I not be doing this? If not, what is the await alternative? UPDATE: After the answer below I've changed this: const foo = await this.afs.doc(`docPath`).valueChanges().toPromise(); ...to this: const foo = await (new Promise(resolve => this.afs.doc(`docPath`).valueChanges(