rxjs

Chaining Observables in RxJS

雨燕双飞 提交于 2019-12-17 04:28:19
问题 I'm learning RxJS and Angular 2. Let's say I have a promise chain with multiple async function calls which depend on the previous one's result which looks like: var promiseChain = new Promise((resolve, reject) => { setTimeout(() => { resolve(1); }, 1000); }).then((result) => { console.log(result); return new Promise((resolve, reject) => { setTimeout(() => { resolve(result + 2); }, 1000); }); }).then((result) => { console.log(result); return new Promise((resolve, reject) => { setTimeout(() =>

Angular2 http.get() ,map(), subscribe() and observable pattern - basic understanding

China☆狼群 提交于 2019-12-17 03:45:28
问题 Now, I have an initial page where I have three links. Once you click on the last 'friends' link, appropriate friends Component gets initiated. In there, I want to fetch/get list of my friends strored into friends.json file. Till now everything works fine. But I'm still a newbie for angular2's HTTP service using RxJs's observables, map, subscribe concept. I've tried to understand it and read few articles but until I get into practical work, I'm not gonna understand those concepts properly.

Convert Promise to Observable

自闭症网瘾萝莉.ら 提交于 2019-12-17 02:09:25
问题 I am trying to wrap my head around observables. I love the way observables solve development and readability issues. As I read, benefits are immense. Observables on HTTP and collections seem to be straight forward. How can I convert something like this to observable pattern. This is from my service component, to provide authentication. I'd prefer this to work like other HTTP services in Angular2 - with support for data, error and completion handlers. firebase.auth()

Is it necessary to unsubscribe from observables created by Http methods?

淺唱寂寞╮ 提交于 2019-12-16 19:53:24
问题 Do you need to unsubscribe from Angular 2 http calls to prevent memory leak? fetchFilm(index) { var sub = this._http.get(`http://example.com`) .map(result => result.json()) .map(json => { dispatch(this.receiveFilm(json)); }) .subscribe(e=>sub.unsubscribe()); ... 回答1: So the answer is no, you don't. Ng2 will clean it up itself. The Http service source, from Angular's Http XHR backend source: Notice how it runs the complete() after getting the result. This means it actually unsubscribes on

Recursively combining HTTP results based on response

倾然丶 夕夏残阳落幕 提交于 2019-12-14 03:59:41
问题 There's an API(https://panelapp.genomicsengland.co.uk/api/v1/panels/?page=1) that I want to consume all the data to my angular apps. The problem is that their API have pagination, and I want to retrieve all of the content at once. As you can see on the API, they actually have "next" attribute on their response which point to the next page. I want to be able to keep requesting from the API as long as "next" attribute is not null then combine all their response into one. I have tried using

Angular: set selected value for <select>

半腔热情 提交于 2019-12-14 03:47:47
问题 I have data set for my <select> loaded asynchronously. I use hot observable, as the data can change in time. The problem is I am unable to set selected value and also Angular does not point to first element by itself (there is no value set until user does it). I'm trying to subscribe to my own observable and... it doesn't work, I wonder why? How can I solve this problem? PLNKR: https://plnkr.co/edit/uDmTSHq4naYGUjjhEe5Q @Component({ selector: 'my-app', template: ` <div> <h2>Hello {{name}}</h2

RxJS - take n last elements from an observable

核能气质少年 提交于 2019-12-14 03:40:07
问题 I want to take 3 last elements from an observable. Let's say that my timeline looks like this: --a---b-c---d---e---f-g-h-i------j-> where: a, b, c, d, e, f, g, h, i, j are emitted values Whenever a new value is emitted I want to get it immediately, so it can look like this: [a] [a, b] [a, b, c] [b, c, d] [c, d, e] [d, e, f] [e, f, g] [f, g, h] ... and so on I think that this is super useful. Imagine building a chat where you want to display 10 last messages. Whenever a new message comes you

Invoking observables with Subject next() not working

ⅰ亾dé卋堺 提交于 2019-12-14 03:29:26
问题 Why does this function only work once? I click a button to call the next() on the Subject queue which works but if I click the other button it doesn't work. getData(text): Observable<string> { const timer$ = timer(2000); const observable = new Observable<string>(observer => { timer$.pipe( map(() => { observer.next('http response ' + text); }) ).subscribe(); }); return observable; } I setup a Subject and use next() which should make the observable emit data. queue = new Subject(); streamA$:

Angular 2 beta.17: Property 'map' does not exist on type 'Observable<Response>'

馋奶兔 提交于 2019-12-14 03:21:05
问题 I just upgraded from Angular 2 beta16 to beta17 , which in turn requires rxjs 5.0.0-beta.6. (Changelog here: https://github.com/angular/angular/blob/master/CHANGELOG.md#200-beta17-2016-04-28) In beta16 all was working well regarding Observable/map functionality. The following errors appeared after I upgraded and occur when typescript attempts to transpile: Property 'map' does not exist on type 'Observable' (anywhere where I've used map with an observable) c:/path/node_modules/rxjs/add

Which rxjs operator to make extra transformation after mapping?

寵の児 提交于 2019-12-14 02:36:57
问题 this.service.retrieveObject(id).map((object)=>{ return transformation1(object); }) .map((object)=>{ return transformation2(object); }); In the exemple bellow the second map call doesn't have access to object after transformation1 exexuted. Which operator do I need to use to make nested transformations (transformation2 over transformation1)? 回答1: map is the correct operator to make transformations. map is essentially an a -> b function where you input an a and output a b . Any subsequent calls