rxjs

RxJs avoid external state but still access previous values

妖精的绣舞 提交于 2019-12-07 08:59:46
问题 I'm using RxJs to listen to a amqp queu (not really relevant). I have a function createConnection that returns an Observable that emits the new connection object. Once I have a connection, I want to send messages through it every 1000ms and after 10 messages I want to close the connection. I'm trying to avoid external state, but if I don't store the connection in an external variable, how can I close it? See I begin with the connection, then flatMap and push messages, so after a few chains I

Return a Observable from a Subscription with RxJS

☆樱花仙子☆ 提交于 2019-12-07 08:44:07
问题 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

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

为君一笑 提交于 2019-12-07 08:35:43
问题 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

Multiple observables working together

情到浓时终转凉″ 提交于 2019-12-07 08:20:27
问题 I have an input that as the user types it performs a real-time search. For example, let's say he has searched for the following: car The result would be: [ { id: "1" name: "Ferrari" }, { id: "2" name: "Porsche" } ] This I was able to do successfully, here is how: class WordComponent { word: Subject<string> = new Subject<string>(); result: any[] = []; constructor(private http: Http) { this.subscribe(); } subscribe(): void { this.word.debounceTime(400) .distinctUntilChanged() .switchMap((word:

Get a single document by foreign key in using RxJs and AngularFirestore

我是研究僧i 提交于 2019-12-07 08:10:48
I've written a query that looks like this: getPaymentByBookingId(id: string): Observable<Payment> { return this.afs.collection<Payment>('payments', ref => ref.where('bookingId', '==', id).limit(1)) .valueChanges() .pipe( flatMap(array => from(array)), first() ); } Where a Payment object has a unique reference to a Booking id. I want to find the Payment for a given ID. This seems like quite a convoluted way to right this query. Is there a simpler way to do this - including if there is a code smell here that makes this hard? You can do it solely using Javascript native array API .find() , and it

Hot and shared Observable from an EventEmitter

♀尐吖头ヾ 提交于 2019-12-07 07:58:39
问题 Is there a way to have a hot observable from an EventEmitter (or equivalent available in Angular 2 alpha 46 / RxJS 5 alpha)? i.e. if we subscribe after the value is resolved, it triggers with the previously resolved value . Similar to what we have when always returning the same promise. Ideally, only using Angular 2 objects (I read somewhere a light RxJS would be embedded later to remove the dependency), otherwise importing RxJS is fine. AsyncSubject seems to match my need, but it is not

Multiple HTTP Requests with RxJS, with parameters

不想你离开。 提交于 2019-12-07 07:47:26
I call this api https://jokerleb.com/wp-json/wp/v2/ads/ I grab the id of each ad and then I append it to this url https://jokerleb.com/wp-json/wp/v2/media?parent=24385 My code is loadAds() { let i = 0; this.HttpClient.get(ENV.site_url + ENV.ads_url) .subscribe(res => { this.ads = this.ads.concat(res); this.HttpClient.get(ENV.site_url + ENV.ads_thumb_url + this.ads[i].id + "&&ads/" + this.ads[i].id) .subscribe(res => { this.items = this.items.concat(res); console.log(ENV.site_url + ENV.ads_thumb_url + this.ads[i].id + "&&ads/" + this.ads[i].id); i++; }) } ) } The code works but: I end up with

rxjs5 merge and error handling

你离开我真会死。 提交于 2019-12-07 06:15:30
问题 I would like to combine/merge multiple Observables and when each of them is completed execute a finally function. The merge operator seems to execute each subscription in parallel, which is what I need, but if any of them throws an error the execution is halted. RxJS version 4 has an operator mergeDelayError that should keep the all subscriptions executing till all of them are completed, but this operator isn't implemented in version 5 . Should I revert to a different operator? var source1 =

Why handle errors with catchError and not in the subscribe error callback in Angular

和自甴很熟 提交于 2019-12-07 05:51:17
问题 So I'd normally write my http requests like so Service getData(){ return this.http.get('url') } Component getTheData() { this.service.getData().subscribe( (res) => { //Do something }, (err) => { console.log('getData has thrown and error of', err) }) But looking through Angular documentation they seem to format it like so in a Service getHeroes(): Observable<Hero[]> { return this.http.get<Hero[]>(this.heroesUrl) .pipe( catchError(this.handleError('getHeroes', [])) ); } What's the implicit

RxJS iif arguments are called when shouldn't

人盡茶涼 提交于 2019-12-07 05:39:30
问题 I want to conditionally dispatch some actions using iif utility from RxJS. The problem is that second argument to iif is called even if test function returns false. This throws an error and app crashes immediately. I am new to to the power of RxJS so i probably don't know something. And i am using connected-react-router package if that matters. export const roomRouteEpic: Epic = (action$, state$) => action$.ofType(LOCATION_CHANGE).pipe( pluck('payload'), mergeMap(payload => iif( () => {