rxjs

Using a single subscription variable with BehaviorSubject

流过昼夜 提交于 2019-12-17 21:29:23
问题 I use BehaviorSubject in my Angular app and I get observable to my Details component from DataService as shown below: DataService.ts: export class DataService { private messageTracker = new BehaviorSubject<any>(); private fileTracker = new BehaviorSubject<any>(); getMessageTracker(): Observable<any> { return this.messageTracker.asObservable(); } getFileTracker(): Observable<any> { return this.fileTracker.asObservable(); } //set methods omitted for brevity } DetailComponent : export class

Loop of subscribes inside a subscribe?

孤街醉人 提交于 2019-12-17 21:21:12
问题 I'm currently struggling with multiples/forEach subscribe inside a subscribe, I'm trying to retreive a list of objects then retreive their images thanks to their ID. Currently I've done this : this.appTypeService.get().pipe( map((apps: AppTypeEntity[]) => { return apps.map((app) => new AppTypeEntity(app)); }) ).subscribe((apps: AppTypeEntity[]) => { apps.forEach((app: AppTypeEntity) => { if (app.Logo != null) { this.appTypeService.getPhoto(app.Id).subscribe((image: string) => { app.Image =

How to create a RxJS buffer that groups elements in NodeJS but that does not rely on forever running interval?

喜你入骨 提交于 2019-12-17 21:07:03
问题 I'm capturing events from an application using Rx.Observable.fromEvent in a NodeJS. These are sent to another server using request (https://www.npmjs.com/package/request). To avoid a high network load I need to buffer those events at a given timeout between sent requests. Problem Using bufferWithTime(200) will keep the node process running and I can't know when the application has finished to close the stream. Is there any way to use Rx buffers to say: When Element 1 is pushed set a timer

What does RxJS.Observable debounce do?

喜夏-厌秋 提交于 2019-12-17 19:34:11
问题 Can anybody explain in plain English what RxJS Observavle debounce function does? I imagine it emits an event once in a while depending on the parameters, but my code below doesn't work as I expected. var x$ = Rx.Observable.fromEvent(window, 'click') .map(function(e) {return {x:e.x, y:e.y};}) .debounce(1000) .subscribe(function(el) { console.log(el); }); and the JsBin version. I expected that this code would print one click once per second, no matter how fast I am clicking. Instead it prints

How to import `Observable` from `Rx` (not angular)

这一生的挚爱 提交于 2019-12-17 18:59:08
问题 I'm using SystemJS to load my es2015 project into the browser. This is what I did import {Observable} from 'rxjs/Rx'; const button = document.querySelector('button'); const start$ = Observable.fromEvent(button, 'click'); In this case Observable is undefined . So I tried import Observable from 'rxjs/Observable'; In which case Observable is an object but Observable.fromEvent is undefined (it seems to be an empty object) Finally I did import Rx from 'rxjs/Rx' // Rx.Observable which did work. Any

What are RxJS Subject's and the benifits of using them?

痞子三分冷 提交于 2019-12-17 18:56:02
问题 I found the rxJS docs define them as What is a Subject? An RxJS Subject is a special type of Observable that allows values to be multicasted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast. and it goes on to give examples but I'm looking for a basic ELI5 explanation. From my understanding is it helps handle and define items in a sequence. Is that correct? I think it would be most helpful

How do I throw an error on a behaviour subject and continue the stream?

吃可爱长大的小学妹 提交于 2019-12-17 18:52:00
问题 On one end, I have a stream which may occasionally throw an error: this.behaviorSubject.error(error) Later on, however, I want to continue the stream: this.behaviorSubject.next(anotherValue) on the other end, I have a subscriber subscribed to behaviorSubject.asObservable() . In the subscribtion, I'm handling the value and the error: .subscribe( ( value ) => { /* ok */ }, ( error ) => { /* some error */ } ); I want the effect to be the same as a simple onSuccess and onError callback, where

Angular2: convert array to Observable

佐手、 提交于 2019-12-17 17:54:23
问题 I have a component that gets the data from a service via http, the problem is that I don't want to hit the API backend every time I show this component. I want my service to check if the data is in memory, if it is, return an observable with the array in memory, and if not, make the http request. My component import {Component, OnInit } from 'angular2/core'; import {Router} from 'angular2/router'; import {Contact} from './contact'; import {ContactService} from './contact.service'; @Component(

Separate observable values by specific amount of time in RxJS

北战南征 提交于 2019-12-17 17:44:08
问题 What would be the most idiomatic way to yield values of an Observable by a specific amount of time? For example, let's say I have an Observable created from a big Array and I want to yield a value every 2 seconds. Is a combination of interval and selectMany the best way? 回答1: For your specific example, the idea is to map each value from the array to an observable that will yield its result after a delay, then concatenate the resulting stream of observables: var delayedStream = Rx.Observable

Performance of using same observable in multiple places in template with async pipe

冷暖自知 提交于 2019-12-17 17:37:35
问题 In my component template I am calling async pipe for same Observable in 2 places. Shall I subscribe to it and use returned array in my template or using async pipe for same Observable in multiple places of template has no negative effect to performence? 回答1: Every use of observable$ | async will create a new subscription(and therefor an individual stream) to the given observable$ - if this observable contains parts with heavy calculations or rest-calls, those calculations and rest-calls are