rxjs

Exponential backoff implementation with rxjs

浪子不回头ぞ 提交于 2019-12-06 03:56:12
问题 Angular 7 docs provide this example of practical usage of rxjs Observable s in implementing an exponential backoff for an AJAX request: import { pipe, range, timer, zip } from 'rxjs'; import { ajax } from 'rxjs/ajax'; import { retryWhen, map, mergeMap } from 'rxjs/operators'; function backoff(maxTries, ms) { return pipe( retryWhen(attempts => range(1, maxTries) .pipe( zip(attempts, (i) => i), map(i => i * i), mergeMap(i => timer(i * ms)) ) ) ); } ajax('/api/endpoint') .pipe(backoff(3, 250))

How to start immediately an Angular 2 Observable when using interval

落爺英雄遲暮 提交于 2019-12-06 03:55:53
I have the following Angular 2 service that is fetching data every 5 seconds: import { Observable } from "rxjs/Rx"; import { Http, Response } from '@angular/http'; @Injectable() export class DataService{ getData():{ return Observable .interval(5000) .flatMap(() => this.http.get('http://some_url')) .map((res: Response) => res.json()) } Is there a way to have this service executing the request at the start? Otherwise it will wait for 5 seconds before the first execution. Replace interval with timer , as it allows you to specify an initial delay: getData():{ return Observable .timer(0, 5000)

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

我只是一个虾纸丫 提交于 2019-12-06 03:24:03
问题 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

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

安稳与你 提交于 2019-12-06 02:46:16
问题 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); }

Angular 2 share websocket service across components

旧街凉风 提交于 2019-12-06 02:33:56
问题 I am building a web application using angular 2 in which I want to have multiple components listening to the same service. This service returns an observable that returns incoming data from a websocket. I wrote the code based on this example. The current problem is: The data is send from the home component through the service to the server (using websockets) and data is returned. However, only the observer in the home.component is getting called (with id: room.created and data), not the one

How to easily convert or assign an Observable to a Behavior Subject, so other component can share it

笑着哭i 提交于 2019-12-06 01:57:16
问题 I am new to Observable style programming. I have a question: I want to share user info across the app between component - and I use BehaviorSubject to share this info. This is inspired by sharing BehaviorSubject as AuthInfo. If I can share AuthInfo which contain a uid across my app component, why can I use it to share my user object data? The problem is, the user object, I am getting that from Firebase. So it is an Observable, and I have no idea how can I assign this to a Behavior Subject. So

Mock delay() RxJS with Jest

徘徊边缘 提交于 2019-12-06 01:57:01
问题 Is there easy way to mock delay() method of RxJS in an observable with a fake time for example ? I have this method : register(user) { return this._checkLog(user).delay(500).flatMap( ... ) } when i remove delay() method, my tests from _register() all success. 回答1: RxJS v6 For RxJS v6 code like this: code.js import { of } from 'rxjs'; import { delay } from 'rxjs/operators'; export const example = of('hello').pipe( delay(1000) ); ...you can use sinon fake timers like this: import * as sinon

Return a Promise from Redux Observable

别等时光非礼了梦想. 提交于 2019-12-06 01:44:23
Maybe I'm thinking about this wrong, but a common pattern I use with redux-thunk is to return a promise so I can perform some additional actions in the container object when something completes or fails. Using Thunk as an example: Action Creator: const action = ({ data }) => (dispatch) => fetch(`some http url`) .then(response => { if(response.ok) { return response.json(); } return Promise.reject(response); }) Somewhere in the connected component: this.props.action("Some Data") .then(console.log) .catch(error => console.error("ERROR")); Is there a clean way of doing something similar in Redux

Add data to http response using rxjs

孤街醉人 提交于 2019-12-06 01:32:48
I have a trip entity which contains driver id. I can get fetch trip using RESTFull endpoint, e.g. /trips/2/ . //example response { id: "2", driver_id: "123" } I can fetch driver details using the endpoint. e.g. /drivers/123/ , My final respected response is //expected response from observable { id: "2", driver_id: "123", detailed_driver: { name: "abc", id: "123" } } Currently I do it as follows this.http("/trips/2/").map(data => data.json()).subscribe(trip => { this.http("/drivers/" + trip.driver_id + "/").map(data => data.json()).subscribe(driver => { trip.detailed_driver = driver; this.trip

What functionalities that RxJS provide for Angular2?

…衆ロ難τιáo~ 提交于 2019-12-06 00:49:28
There is some references of RxJS in the Angular2 project. What is RxJS being used for in Angular2? A list of what angular2 uses RxJS for Http (for example its get method returns an Observable ) EventEmitter (like you said, extends from Subject ) AsyncPipe which supports Promise , Observable or EventEmitter . QueryList's changes method returns an EventEmitter . Update NG_ASYNC_VALIDATORS which implements Validator and overrides the method validate to return either a Promise or an Observable . Update Note about NG_ASYNC_VALIDATORS NG_ASYNC_VALIDATORS doesn't use Observable directly, it doesn't