rxjs

Angular2 observable share is not working

夙愿已清 提交于 2019-12-10 15:24:48
问题 Angular2 Observable share is not working and duplicate http calls going BuildingService.ts @Injectable() export class BuildingService { constructor(private http: Http){ } buildings$: Observable<Building[]>; this.buildings: Building[]; getData() : Observable<Building[]>{ this.buildings$ = this.http.get('http://localhost:8080/buildings').share().map(this.extractData); this.buildings$.subscribe(buildings => this.buildings = buildings); return this.buildings$; } private extractData(res: Response)

Do side effect if observable has not emitted a value within X amount of time

旧时模样 提交于 2019-12-10 14:58:40
问题 I'm working on a use case that requires that if an observable has not emitted a value within a certain amount of time then we should do some side effect. To give a practical use case: open web socket connection if no message has been sent/received within X time then close web socket connection and notify user This requires for a timer to be initiated on every emitted value and upon initial subscription of observable which will then run some function after the allotted time or until a value is

RxJS: Setting default value for observable from another observable

穿精又带淫゛_ 提交于 2019-12-10 14:55:56
问题 I'm trying to create observable stream which takes user id from cookie and, if not found in cookie, fetches it from API. How can I do it in RxJS? var userIdRequest = Rx.Observable.bindCallback(generateIdAsync); var cookieUserIdStream = Rx.Observable.of(getCookieValue("user_id")) .filter(x => x !== null); var userIdStream = cookieUserIdStream.__ifEmptyThen__(userIdRequest()); // <<< ??? // Emulating async request for user id // Will be a JSONp call in real app function generateIdAsync(cb) {

How to get last emitted Observable

走远了吗. 提交于 2019-12-10 14:50:07
问题 I've created an Angular service that basically sets an object with some user information. When I change the user, the service emits an observable, captured by some sibling components to update the info. The problem is that the first time I set the data, there's no one subscribed yet, so I get an undefined value. I've read about converting the observable to 'hot' using publish() or share() but I'm pretty new to RxJS and I still haven't understood how it works at 100% so I'm a little lost. The

RxJs / Typescript throws 'property clientX does not exist on type {}'

别说谁变了你拦得住时间么 提交于 2019-12-10 14:37:56
问题 I am trying to do something very simple, just log the event.clientX on mouseover, and this is the module code: import { Observable, Subject } from 'rxjs'; import StarSky from './starsky'; import constants from '../constants/index'; const HERO_Y = constants.HERO_Y(StarSky.canvas); export const StarShip = Observable.fromEvent(StarSky.canvas,'mousemove') .map((event)=> { return { x: event.clientX, y: HERO_Y } }) .startWith({ x: StarSky.canvas.width /2, y: HERO_Y }) Then I subscribe to it later,

RxJS Angular2 handling 404 in Observable.forkjoin

喜欢而已 提交于 2019-12-10 14:28:54
问题 I'm currently chaining a bunch of http requests, however I am having trouble handling 404 errors before subscribing. My code: in template: ... service.getData().subscribe( data => this.items = data, err => console.log(err), () => console.log("Get data complete") ) ... in service: ... getDataUsingUrl(url) { return http.get(url).map(res => res.json()); } getData() { return getDataUsingUrl(urlWithData).flatMap(res => { return Observable.forkJoin( // make http request for each element in res res

How are asynchronous streams transmitted in RXJS?

对着背影说爱祢 提交于 2019-12-10 13:55:47
问题 I'm trying to understand how the stream is transmitted through the pipe in RXjs. I know that this should not be a concern because that's the whole idea with async streams - but still there's something I want to understand. Looking at this code : var source = Rx.Observable .range(1, 3) .flatMapLatest(function (x) { //`switch` these days... return Rx.Observable.range(x*100, 2); }); source.subscribe(value => console.log('I got a value ', value)) Result : I got a value 100 I got a value 200 I got

Any downside to always using BehaviorSubject instead of Subject (RxJs\Angular)?

半世苍凉 提交于 2019-12-10 13:29:51
问题 I am working on a project in which portions of the code-base are using BehaviorSubject quite liberally. In most cases being used when there is no initial state, or a need to have an initial value outside of the first explicit "onNext/emit". I am having a hard time determining if there is any downside to this? Also if not, why wouldn't everyone just always use BehaviorSubject (even constructed without a parameter) as opposed to a standard Subject? Thanks in advance! 回答1: A BehaviorSubject is

BehaviorSubject initial value not working with share()

﹥>﹥吖頭↗ 提交于 2019-12-10 13:22:51
问题 share() operator is applied to a BehaviorSubject. BehaviorSubject has initial value. Goal is to create a single shared subscribtion. But this shared subscribtion does not seem to work when BehaviorSubject has an initial value. Getting unexpected results. Code shown below: let subject = new Rx.BehaviorSubject(0); let published = subject .do(v => console.log("side effect")) .share(); published.subscribe((v) => console.log(v+" sub1")); published.subscribe((v) => console.log(v+" sub2")); subject

TypeError: You provided 'undefined' where a stream was expected

浪子不回头ぞ 提交于 2019-12-10 12:58:09
问题 I have an Ionic app that has a user provider with a signup() method: doSignup() { // set login to same as email this.account.login = this.account.email; // Attempt to login in through our User service this.user.signup(this.account).subscribe((resp) => { this.navCtrl.push(MainPage); }, (err) => { //console.log('error in signup', err); // ^^ results in 'You provided 'undefined' where a stream was expected' //this.navCtrl.push(MainPage); // Unable to sign up let toast = this.toastCtrl.create({