rxjs5

Angular 2 polling with RxJS

廉价感情. 提交于 2019-12-04 01:55:06
I'm trying to poll a RESTful endpoint to refresh my live chat messages. I know the best approach for a live chat would be Websockets, I'm just trying to understand how RxJS works with Angular 2. I want to check for new messages every second. I have the following code: return Rx.Observable .interval(1000) .flatMapLatest(() => this.http.get(`${AppSettings.API_ENDPOINT}/messages`)) .map(response => response.json()) .map((messages: Object[]) => { return messages.map(message => this.parseData(message)); }); However my Typescript transpiler is returning this error: Property 'flatMapLatest' does not

Pipe RxJS observable to existing subject

╄→尐↘猪︶ㄣ 提交于 2019-12-04 01:35:57
There is existing subject that is in use: const fooSubject = new BehaviorSubject(null); And there is another observable (another subject in this example): const barSubject = new Subject(); barSubject.subscribe( value => fooSubject.next(), err => fooSubject.error(err), () => fooSubject.complete() ); barSubject.next('bar'); The code works but looks clumsy. Is there a better way to pipe (in broad sense, not necessarily using pipe operator) barSubject observable to fooSubject ? It looks like an operation that could be handled by the library itself. Since Subject is already an observer with methods

How to convert an Observable to a ReplaySubject in RxJS?

笑着哭i 提交于 2019-12-03 17:35:08
问题 Here is what I'm doing now to convert an Observable to a ReplaySubject: const subject = new Rx.ReplaySubject(1); observable.subscribe(e => subject.next(e)); Is this the best way to make the conversion, or is there a more idiomatic way? 回答1: You can use just observable.subscribe(subject) if you want to pass all 3 types of notifications because a Subject already behaves like an observer. For example: let subject = new ReplaySubject(); subject.subscribe( val => console.log(val), undefined, () =>

How to better catch/do/empty with RXJS 5.5.2 Updates

时间秒杀一切 提交于 2019-12-03 16:06:44
As staten in the ionic-angular 3.9.0 release notes ( https://github.com/ionic-team/ionic/blob/master/CHANGELOG.md ), using the advantages of updating to RXJS 5.5.2 could reduce the bundle size and therefore lead to a faster boot time Cool, cool, cool :) The example provided by Ionic, to migrate for example debounceTime is pretty clear, I get it. But it's pretty unclear to me how I should update my following code to take the full advantage of this RXJS update. Anyone could help me to convert it or how to better write it with the goal to save bundle size? import {Observable} from 'rxjs

Angular 2 - Sort list from Observable

风格不统一 提交于 2019-12-03 15:35:28
问题 What is the best way to sort a list of items coming from an Observable and still be able to use the async pipe ? (I read that making a custom sort pipe is not really efficient.) I want to avoid subscribing and keeping a local copy of data and thus just using async pipe... //can I use map here and filter items right in the observable and get rid of subscribe? this.test$ = Observable.of(['one', 'two', 'three']) .subscribe((data) => { data.sort((a, b) => { return a < b ? -1 : 1; }); this.data =

Difference between audit and debounce in rxjs?

陌路散爱 提交于 2019-12-03 14:49:12
问题 I am reading the offical documentaion of rxjs and then i realized they both are doing exactly same thing. To me they both seems to exactly similar. Please someone point out difference between them (if any) 回答1: I'm going to describe the difference between them in terms of their Time versions as that's how I understand them best. Both auditTime and debounceTime will initially start a timer when an event comes in. Both will wait the given amount of time before they emit an event. The difference

How to use Rx.Observable.prototype.let operator?

不想你离开。 提交于 2019-12-03 11:27:21
The example and explanation of the let operator ( https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/let.md ) is not clear. Anyone has a good example/explanation how the let operator works, and when we should use it? &tldr; It is a convenience function for being able to compartmentalize logic and inject it into a pipeline. Longer Explanation The source is probably the most definitive explanation. It is really just passing a function which gets called with a source Observable . Rx.Observable.prototype.let = function(fn) { return fn(this); } The utility of this is

Reactive Caching of HTTP Service

≡放荡痞女 提交于 2019-12-03 09:53:02
I am using RsJS 5 (5.0.1) to cache in Angular 2. It works well. The meat of the caching function is: const observable = Observable.defer( () => actualFn().do(() => this.console.log('CACHE MISS', cacheKey)) ) .publishReplay(1, this.RECACHE_INTERVAL) .refCount().take(1) .do(() => this.console.log('CACHE HIT', cacheKey)); The actualFn is the this.http.get('/some/resource') . Like I say, this is working perfectly for me. The cache is returned from the observable for duration of the RECACHE_INTERVAL . If a request is made after that interval, the actualFn() will be called. What I am trying to

Angular 2 - How to change the interval of an RxJS Observable

拈花ヽ惹草 提交于 2019-12-03 08:31:14
I'm using rxJS Observable Interval to refresh the data being fetched. I can't figure out the way to change the interval setting. I've seen something about using the Subject class provided by rxJS but I can't manage to get it to work. I provided an simplified example in this plunk In the AppComponent I have this method. getTime() { this.timeService.getTime(this.refreshInterval) .subscribe(t => { this.currentTime = t; console.log('Refresh interval is: ' + this.refreshInterval); } ); } And in the service component I currently have this code. getTime(refreshInterval: number) { return Observable

How to two-way bind my own RxJS Subject to an [(ngModel)]?

陌路散爱 提交于 2019-12-03 06:34:55
问题 Is there a short and simple way to pass an RxJS Subject or BehaviorSubject to an an Angular 2 directive for two-way binding? The long way to do it would be as follows: @Component({ template: ` <input type="text" [ngModel]="subject | async" (ngModelChange)="subject.next($event)" /> ` }) I'd like to be able to do something like this: @Component({ template: ` <input type="text" [(ngModel)]="subject" /> ` }) I believe the async pipe is only one-way, so that's not enough. Does Angular 2 provide a