rxjs5

Best way to “flatten” an array inside an RxJS Observable

老子叫甜甜 提交于 2019-11-28 21:08:12
My backend frequently returns data as an array inside an RxJS 5 Observable (I'm using Angular 2). I often find myself wanting to process the array items individually with RxJS operators and I do so with the following code ( JSBin ): const dataFromBackend = Rx.Observable.of([ { name: 'item1', active: true }, { name: 'item2', active: false }, { name: 'item3', active: true } ]); dataFromBackend // At this point, the obs emits a SINGLE array of items .do(items => console.log(items)) // I flatten the array so that the obs emits each item INDIVIDUALLY .mergeMap(val => val) // At this point, the obs

Angular2: Unsubscribe from http observable in Service

混江龙づ霸主 提交于 2019-11-28 20:14:54
What is the best practice to unsubscribe within a Angular2 service from a http subscription? Currently I do this but I'm not sure if this will be the best way. import { Injectable } from "@angular/core"; import { Http } from "@angular/http"; import { Subject } from "rxjs/Subject"; import { ISubscription } from "rxjs/Subscription"; @Injectable() export class SearchService { private _searchSource = new Subject<any>(); public search$ = this._searchSource.asObservable(); constructor(private _http: Http) {} public search(value: string) { let sub: ISubscription = this._http.get("/api/search?value="

How to get “Observable.of([]);” to work?

坚强是说给别人听的谎言 提交于 2019-11-28 13:34:44
What is the correct expression and import for Observable.of([]); . import { of } from 'rxjs'; does not work for me. Since RxJS 6 you should import all "creation" observables directly from 'rxjs' (assuming you have path maps set when bundling your app). More detailed explanation: https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#import-paths import { of } from 'rxjs'; of(1).subscribe(console.log); See demo: https://stackblitz.com/edit/typescript-e3lxkb?file=index.ts No need to use Observable.of(T) Instead you can use the below syntax in the rxjs 6.3.3 return new Observable<AppUser>();

How would I use `do` as an RxJS lettable operator?

孤者浪人 提交于 2019-11-28 13:28:05
RxJS 5.5 allows grabbing lettable operators and piping them like so: import { ajax } from 'rxjs/observable/dom/ajax' import { catchError, map, retry } from 'rxjs/operators' ajax.getJSON('https://example.com/api/test') .pipe( retry(3, 1000), map(fetchUserFulfilled), catchError(console.error) ) How would I use the do operator between these commands? The do operator was renamed in RxJS 5.5 to tap because it collided with the JavaScript do keyword. For more info see: https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md#pipeable-operators 来源: https://stackoverflow.com/questions

Angular 2 RxJS Observable: RetryWhen filter retry on error Status

放肆的年华 提交于 2019-11-28 12:38:31
I am using Angular 2 HTTP library which returns an observable. I want to implement retry on certain error status/code. I have an issue, if the error is not 429, Observable.of(error) is getting executed in error case to retry, but when all your 2 retry fails the execution of flow goes to success block instead of catch block. How to make execution of flow to catch block in all retry fails? return this.http.get(url,options) .retryWhen((errors) => { return errors .mergeMap((error) => (error.status === 429) ? Observable.throw(error) : Observable.of(error)) .take(2); }) .toPromise() .then((res

How does the RxJs 5 share() operator work?

这一生的挚爱 提交于 2019-11-28 10:42:00
Its not 100% clear for me how the RxJs 5 share() operator works, see here the latest docs . Jsbin for the question here . If I create an observable with a series of 0 to 2, each value separated by one second: var source = Rx.Observable.interval(1000) .take(5) .do(function (x) { console.log('some side effect'); }); And if I create two subscribers to this observable: source.subscribe((n) => console.log("subscriptor 1 = " + n)); source.subscribe((n) => console.log("subscriptor 2 = " + n)); I get this in the console: "some side effect ..." "subscriptor 1 = 0" "some side effect ..." "subscriptor 2

RxJS takeWhile but include the last value

醉酒当歌 提交于 2019-11-28 09:46:40
I have a RxJS5 pipeline looks like this Rx.Observable.from([2, 3, 4, 5, 6]) .takeWhile((v) => { v !== 4 }) I want to keep the subscription until I see 4, but I want to last element 4 also to be included in the result. So the example above should be 2, 3, 4 However, according to official document , takeWhile operator is not inclusive. Which means when it encounters the element which doesn't match predicate we gave, it completes the stream immediately without the last element. As a result, the above code will actually output 2, 3 So my question is, what's the easiest way I can achieve takeWhile

Performing advanced http requests in rxjs

馋奶兔 提交于 2019-11-28 09:41:22
问题 I have the following objects: class Question { idQuestion: string; question: string; typeQuestion: string; } class Answer { idAnswer: string; idQuestion: string; answer: string; } class Option { idOption: string; idQuestion: string; option; } And I want to populate the following object: class QuestionOptionsAnswer { question: Question; answer: Answer; options: Option[]; } For now, I have a service for each kind of object, so we can illustrate it in the following way: questionService

RxJs Subject.subscribe method not working as expected

眉间皱痕 提交于 2019-11-28 09:31:50
问题 Subject.subscribe method when called outputs the following error: TypeError: Cannot read property '_subscribe' of undefined at BidirectionalSubject._subscribe (Rx.js:10239) at BidirectionalSubject._subscribe (Rx.js:10239) at BidirectionalSubject.Observable.subscribe (Rx.js:9924) at AppComponent.doIt (app.component.ts:32) at ChangeDetector_AppComponent_0.handleEventInternal (eval at ChangeDetectorJITGenerator.generate (angular2.dev.js:1), <anonymous>:29:29) at ChangeDetector_AppComponent_0

Is Rx.Subject a hot observable?

懵懂的女人 提交于 2019-11-28 09:22:17
问题 The code const a = new Rx.Subject().do(x => console.log('a')) const b = a.mapTo(0) const c = a.mapTo(1) const d = Rx.Observable.merge(b, c) d.subscribe(x => console.log('d')) a.next(3) And the output a d a d Why does a got printed twice? Isn't Rx.Subject a hot observable? 回答1: The Subject itself is hot/shared. However: Any(most!) operators that you append will create a new stream, with the previous stream(in this case the Subject ) as source - the new stream, however, is (for most operators)