rxjs

New to RxJS, range is not a function

白昼怎懂夜的黑 提交于 2019-12-05 16:16:51
I'm trying to create a simple TypeScript file to use RxJS. Here is what I did: " npm install rxjs ", referenced " traceur & systemjs " in my " index.html " and created a " test.ts " file with this: import {Observable} from 'rxjs/Rx'; Observable.range(1, 2, 3, 4, 5) .map(x => x * 2) .filter(x => x > 5) .subscribe( x => console.log(x), err => console.log('Error'), ok => console.log('No error') ); I'm configuring systemjs like this: System.config({ defaultJSExtensions: true, map: { 'rxjs': 'node_modules/rxjs' } }); System.import('test'); According to the documentation, the "Observable" type

Return a Observable from a Subscription with RxJS

三世轮回 提交于 2019-12-05 16:16:23
I currently have a service that do a HTTP request to an API to fetch data. There are some logic that I want to do to the observable from within the service, but I still want to also subscribe to the Observable in my Component so that I can return any errors to the Component and to the user. Currently I do: // service.ts getData(): Observable<any> { return this.http.get(url).pipe(catchError(this.handleError) } // component.ts ngOnInit() { this.service.getData().subscribe(res => { // Do my logic that belong to the service.ts // ... // Do my logic that belongs in the component.ts // ... }, err =>

Get one object from observable array

ⅰ亾dé卋堺 提交于 2019-12-05 16:05:32
How do you write a function that takes an Observable<T[]> and returns Observable<T> ? For example, I have two methods: getTransactions(): Observable<Transaction[]>{ ... } getTransaction(id: number): Observable<Transaction>{ return this.getTransactions().pipe(find(txn => txn.id === id)); } I get the error that type 'Observable' is not assignable to type 'Observable'. Type 'Transaction[] is not assignable to type 'Transaction'. Property 'id' is missing in type 'Transaction[]'. As I understand, the observable pipe functions (map, single, find, max, etc.) pertain to a stream of data (i.e., while

Observable closed on error

ε祈祈猫儿з 提交于 2019-12-05 15:52:24
I have an issue with Observable in Angular 2. I subscribe my component to an observable, then when my service has new value, my component is notified. Problem is when the observer push an error, like an HTTP error, my observable is closed, so my component is no more notified. Question How can I do to make my component continue listening my service even when I have an error ? Example Here an example Here my code : Component constructor(private appService: AppService) { //I subscribe my component to an observable this.appService.commentsObservable.subscribe((comments) => { console.log(comments);

Rxjs number of observable subscriptions

强颜欢笑 提交于 2019-12-05 15:43:07
问题 In my Angular 2 app i have many observables and subscriptions. Ofcourse I should unsubscribe when I leave the page, but i'm trying to find out if it's possible to get the number of active subscriptions. Just for debugging info or when i forget to unsubscribe. Is there such information available in rxjs? 回答1: Probably a bit late, but you could leverage the help of rxjs-spy. This solution is equivalent the proposed one, and, in my opinion, better maintainable. I usually enable it globally in

Angular 6 upgrade: debounceTime is not property of Subject

旧时模样 提交于 2019-12-05 15:30:21
问题 I am attempting to upgrade my app from Angular 5 to Angular 6. I followed the steps on the https://update.angular.io/ At least i think i did. The Error is : Property 'debounceTime' does not exist on type 'Subject<string>'. Also my components lost the debounceTime import. I think the ng update removed it. 回答1: I solved it with the help of @Siva636 and @Andrew Lobban. I needed to use pipe: this.field$.pipe( debounceTime(400), distinctUntilChanged()) 回答2: As of Angular 6.1.8, just need to import

Rxjs share() operator with Behavior subject and async pipe - Angular

余生长醉 提交于 2019-12-05 15:28:47
I have a BehaviorSubject that is being consumed as an observable: testForStack$: Observable<boolean>; ngOnInit(){ const bs = new BehaviorSubject(true); this.testForStack$ = bs .asObservable() .do(t => console.log('subscribed')) .share(); } This observable is being piped through three async pipes in the template: Sub1: {{testForStack$ | async}}<br> Sub2: {{testForStack$ | async}}<br> Sub3: {{testForStack$ | async}} The issue is only the first (Sub1) is getting the value of true Sub1: true Sub2: Sub3: If I remove the .share(), all three values get the value of true, but this causes the issue of

Angular 2 Http RetryWhen

空扰寡人 提交于 2019-12-05 15:26:43
I'm trying to use retryWhen in HTTP calls. It works perfectly when try to use like this: return this.http.get(`${environment.apiUrl}/track/${this.user.instance._id}/${this.currentPlayer.playlist.id}/next?s=${this.playerCounter}`, options) .timeout(500, new TimeoutError(`Timeout trying to get next track. [instanceId=${this.user.instance._id}]`)) .retryWhen(attempts => { return Observable.range(1, 3).zip(attempts, i => i).flatMap(i => 3 === i ? Observable.throw(attempts) : Observable.timer(i * 1000)); }) It makes a maximum of 3 tries if get a Timeout error. But, always have a buuut, I want to

Angular2 Observable Timer Condition

╄→尐↘猪︶ㄣ 提交于 2019-12-05 15:04:44
问题 I have a timer: initiateTimer() { if (this.timerSub) this.destroyTimer(); let timer = TimerObservable.create(0, 1000); this.timerSub = timer.subscribe(t => { this.secondTicks = t }); } How would I add the condition to after 60 minutes present a popup to the user? I've tried looking at a couple of questions (this and this) but it's not clicking for me. Still new to RxJS patterns... 回答1: You don't need RxJS for that. You can use good old setTimeout : initiateTimer() { if (this.timer) {

Angular 6 : How to identify forkJoin response

六眼飞鱼酱① 提交于 2019-12-05 14:57:44
Angular 6 : How to identify response of api with forkJoin reqs = []; if (shouldUpdatePhone) { reqs.push(this.customerService.updatePhone(phoneUpdateRequest)) } if (shouldUpdateAddress) { reqs.push(this.customerService.updateAddress(addressUpdateRequest)) } forkJoin(reqs).subscribe(result => { console.log('result :', result); //How to know response is from updatePhone and response from updateAddress? }); How to can i identify response received is belong to updatePhone and updateAddress? based on identification i need to show message to user. this both api returning Array(2) > {model:true, code