rxjs5

Reactive Caching of HTTP Service

不想你离开。 提交于 2019-12-04 15:49:26
问题 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

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

浪子不回头ぞ 提交于 2019-12-04 13:17: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

Implementing fromSubscriber in rxjs

送分小仙女□ 提交于 2019-12-04 12:58:00
I ran into an interesting issue today. I'm working on an app where we have file uploads, and we want to implement a progress bar. The app is written using React/Redux/Redux-Observable. I want to dispatch actions for upload progress. Here's what I did to implement it: withProgress(method, url, body = {}, headers = {}) { const progressSubscriber = Subscriber.create(); return { Subscriber: progressSubscriber, Request: this.ajax({ url, method, body, headers, progressSubscriber }), }; } I have a class that I use to make all my ajax requests. this.ajax calls Observable.ajax with the passed in

Fork join two firebase observables

会有一股神秘感。 提交于 2019-12-04 10:20:30
I am using angular2fire. I am querying and trying to get all the tours from a city. getAllTours(cityId) { return this.af.database.list(`/cities/${cityId}/tours`) .map((tours): any => { tours.map((tour: any) => { tour.tour = this.af.database.object(`/tours/${tour.$key}/tours`) }); return tours; }) } If i console.log the tour object, i get a array of "FirebaseObjectObservable". I have to loop through all the FirebaseObjectObservable, to get the actual data. I was wondering if i could forkJoin all the observables and get the output as an array with a single subscribe function. Is this a right

Angular 2 RxJS check if mouse event is still active after delay

时光怂恿深爱的人放手 提交于 2019-12-04 08:34:11
I'm using Angular 2 to make a directive. I have the following events bound to the host component: host: { '(mouseenter)': 'onMouseEnter($event)', '(mouseleave)': 'onMouseLeave($event)' } I also created two streams and listeners on the directive to manage the two events export class PopupDirective { private _mouseEnterStream: EventEmitter<any> = new EventEmitter(); private _mouseLeaveStream: EventEmitter<any> = new EventEmitter(); onMouseEnter($event) { this._mouseEnterStream.emit($event); } onMouseLeave($event) { this._mouseLeaveStream.emit($event); } } I want my subscribe to only be called if

Understanding back-pressure in rxjs - only cache 5 images waiting for upload

∥☆過路亽.° 提交于 2019-12-04 08:22:33
I am working on a node project that needs to submit thousands of images for processing. Before these images are uploaded to the processing server they need to be resized so I have something along the lines of this: imageList .map(image => loadAndResizeImage) .merge(3) .map(image => uploadImage) .merge(3) .subscribe(); Image resizing typically takes a few tenths of a second, uploading and processing takes around 4 seconds. How can I prevent thousands of resized images building up in memory as I wait for the upload queue to clear? I probably want to have 5 images resized and waiting to go so

RxJS5 finalize operator not called

白昼怎懂夜的黑 提交于 2019-12-04 04:47:31
I'm trying to trigger a callback when all my observables are executed. In my other, older project i used finally like so and that worked like a charm: this.myService.callDummy() .finally(() => console.log('Works!')) .subscribe(result => ...) But now I'm using a newer version of RxJS with Pipeable operators , but the finally call (now renamed to finalize ) never gets executed. There is little information to be found and I'm not sure what I'm doing wrong. combineLatest( this.route.queryParams, this.myService.callDummy1(), this.myService.callDummy2() ) .pipe(finalize(() => console.log('Does not

Unit Test RxJS Observable.timer using typescript, karma and jasmine

橙三吉。 提交于 2019-12-04 04:15:31
Hi I'm relatively new to Angular2, Karma and Jasmine. Currently I'm using Angular 2 RC4 Jasmine 2.4.x I have an Angular 2 service which periodically calls an http service like this: getDataFromDb() { return Observable.timer(0, 2000).flatMap(() => { return this.http.get(this.backendUrl) .map(this.extractData) .catch(this.handleError); }); } Now I want to test the functionality. For testing purposes I have just tested the "http.get" on a separate function without the Observable.timer by doing: const mockHttpProvider = { deps: [MockBackend, BaseRequestOptions], useFactory: (backend: MockBackend,

Typescript Select Ids from object [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-04 03:26:43
This question already has an answer here: From an array of objects, extract value of a property as array 14 answers I am new to Typescript. I want to select ids from observable This is my observable let myObj = [{ "id": 1, "text": "Mary" }, { "id": 2, "text": "Nancy" }, { "id": 3, "text": "Paul" }, { "id": 4, "text": "Cheryl" }, { "id": 5, "text": "Frances" }] Expected Result : let selectedIds = [1,2,3,4,5]; Can I do this without creating an array and pushing the ids in a for loop. Use Array#map to map one array to another: const myObj = [{"id":1,"text":"Mary"},{"id":2,"text":"Nancy"},{"id":3,

Angular - what is the preferred way to terminate Observables?

时光毁灭记忆、已成空白 提交于 2019-12-04 03:16:29
From my understanding of Angular and RxJs there are two ways to terminate Observables. You can unsubscribe() from them or use takeUntil() and complete() . Below are examples of each approach (in pseudocode). The unsubscribe() approach private _id: number; private _subscriptions: Subscription[] = []; constructor(private _route: ActivatedRoute) { this._getId(); } public ngOnDestroy(): void { this._subscriptions.forEach( subscription => subscription.unsubscribe() ); } private _getId(): void { this._subscriptions.push( this._route.params.subscribe(params => this._id = +params['id']) ); } The