rxjs5

Flattening nested Observables

旧城冷巷雨未停 提交于 2019-12-01 01:31:12
问题 I'm a stuck in nested observable hell and could do with a hand. I have the following block of code return this.findUser(term).map( users => { return users.map( user => this.getLastLogin(user.user_id).map( last_login => { user.last_login = last_login; return user; })); }); findUser returns Observable<User[]> and getLastLogin returns Observable<number> . I'm basically hoping to fetch a list of users and then update this with the information from another value. Right now the code above is

How to handle multiple action types in one epic? Any cons of doing the same?

元气小坏坏 提交于 2019-11-30 23:13:24
问题 Pretty new to redux-observables, rxjs and observables. Wanted to know how can I handle another action, say 'ActionTwo' in the same epic const Epic1 = (action$,store) => { return action$.ofType('ActionOne') .mergeMap((action) => { return ajax({'method': 'GET', 'url': 'someUrl') .map(response => resultActoin(action.userId, response.response)); } ); } Something like const Epic1 = (action$){ if('ActionOne') make a API call. if('ActionTwo') make some other API call. else do nothing. } 回答1: Is it

Simple way to get the current value of a BehaviorSubject with rxjs5

混江龙づ霸主 提交于 2019-11-30 21:26:47
问题 Previously in rxjs4 there was a method in the BehaviorSubject called: getValue() (doc here). This method does not exist any more in rxjs5 . So the only solution that I found to get the value of a BehaviorSubject was: let value; myBehaviorSubject.take(1).subscribe( (e) => value = e ); This code run synchronously (I do not exactly understand why, but it does ...) and get the value. It work, but it's not as clean as it could be if getValue() was present: let value = myBehaviorSubject.getValue();

Cannot create observable from Observable.bindNodeCallback(fs.readFile) in TypeScript

青春壹個敷衍的年華 提交于 2019-11-30 19:12:41
I am trying to use rxjs 5 to write a Node.js server in TypeScript, but I hit an error when converting fs.readFile to its rxjs form. I expect the following code would work in TypeScript // This is a JavaScript example from the official documentation. It should // also work at the TypeScript envrionment. import * as fs from 'fs'; import { Observable } from 'rxjs'; let readFileAsObservable = Observable.bindNodeCallback(fs.readFile); // This is the line that throws the error. let result = readFileAsObservable('./roadNames.txt', 'utf8'); result.subscribe(x => console.log(x), e => console.error(e));

RXJS control observable invocation

自作多情 提交于 2019-11-30 17:34:44
I use RxJs version 5 within my Angular 2 project. I want to create some observables but I don't want the observables being invoked immediately. In version 4 you could control the invocation with (for example) the Controlled command or Pausable Buffers . But that functionality is not ( yet ) available in version 5. How can I get the this kind of functionality in RxJs 5? My ultimate goal is to queue the created observables and invoke them one by one. The next one is only invoked when the previous one is processed successfully. When one fails, the queue is emptied. EDIT With the the comment of

RxJS 5, converting an observable to a BehaviorSubject(?)

蓝咒 提交于 2019-11-30 17:20:37
I have a parent observable that, once it has a subscriber, will do a lookup and emit a single value, then complete. I'd like to convert that into an observable (or behavior subject or whatever works) that does the following: once it has at least one subscriber, it gets the result from the parent observable (once). Then it emits that value to all of its subscribers, and also emits that single value to all future subscribers, when they subscribe. It should continue with this behavior even if its subscriber count drops to zero. It seems like this should be easy. Here is what didn't work: theValue

How to Stop observable.timer in Angular2

有些话、适合烂在心里 提交于 2019-11-30 17:09:51
I am implementing the following functions in Angular2's Component: export class MypageEditComponent { ngOnInit() { this.timer = Observable.timer(100, 100); this.timer.subscribe(t => { this.setFormData(); } private setFormData() { this.editUserAcountType = this.authStore.registerInfo.account_type; this.editAddress = this.authStore.registerInfo.email; this.editUserName = this.authStore.registerInfo.username; } } I want to stop the repeat of Observable.timer once the value is correctly stored with setFormData() . But do not know how, please tell me. There're are basically two ways: call

RxJS - pause, upon resume give last paused value

时光怂恿深爱的人放手 提交于 2019-11-30 14:42:21
I have a hot observable fed by a socket. I can use the pausable to pause the socket feed. But once I 'unpause' the observable, I need to display the last values that the socket could have sent while the subscription was paused. I don't want to keep track of the last values the socket sends manually...How could this be pausible? From the example in the documentation, see comments below: var pauser = new Rx.Subject(); var source = Rx.Observable.fromEvent(document, 'mousemove').pausable(pauser); var subscription = source.subscribe( function (x) { //somehow after pauser.onNext(true)...push the

Using RxJs and Angular 2 in order to deal with server-sent events

∥☆過路亽.° 提交于 2019-11-30 14:10:22
问题 I am trying to display server-sent events emitted values in an angular 2 /RxJs app. The backend regularly sends individual strings to the client through server-sent events. I am not sure how to deal with the retrieved values on the angular 2/RxJs side. Here is my client (a ng component): import {Component, OnInit} from 'angular2/core'; import {Http, Response} from 'angular2/http'; import 'rxjs/Rx'; import {Observable} from 'rxjs/Observable'; @Component({ selector: 'my-app', template: `<h1>My

Combine RxJS operators into new operator using TypeScript

ぃ、小莉子 提交于 2019-11-30 13:17:25
问题 I frequently find my self adding the same sequence of operators to observables, e.g. observable$ .do(x => console.log('some text', x)) .publishReplay() .refCount(); I'm looking for a way to combine these 3 operators in a small reusable operator (e.g. .cache('some text') ) that I can chain to any observable. How can I define this in Typescript, so that I could import rxjs/Observable and this operator, like I do with rxjs operators? 回答1: To implement the operator you have described, create a