rxjs

What's difference between “reduce” and “scan”

◇◆丶佛笑我妖孽 提交于 2019-12-05 14:50:50
问题 I'm studying RXJS and stuck with the problem: the same code with operators "reduce" and "scan" works in different ways, but I think that must return equal result. Example below. Please help. const txtElement1 = document.getElementById('txt1'); const txtElement2 = document.getElementById('txt2'); const txtElement3 = document.getElementById('txt3'); // function return Observable function get(array, initValue) { return Rx.Observable.create(observer => { let timer = initValue; array.forEach(item

Angular 5 Interceptor - Only call second interceptor after failed retry on first interceptor

走远了吗. 提交于 2019-12-05 14:48:16
I am building an angular 5 app where I have 2 interceptors: One to retry failed 504 requests Another to show error messages to users about failed requests I want that the second interceptor only gets called when the error is not 504 or when it is 504 and has already been retried by the first interceptor. I have created a sample with both interceptors: https://stackblitz.com/edit/angular-interceptors-ckbvsb Hope to get some assistance! Thanks UPDATE: Thanks everyone for your assistance! I have ended up implementing it like this: @Injectable() export class I1 implements HttpInterceptor {

Lazy loading : Observable not subscribed

大憨熊 提交于 2019-12-05 14:14:04
In my Angular App, I have a parent component : console.component.html <div class="main"> <ibe-snackbar></ibe-snackbar> <router-outlet></router-outlet> </div> routler-outlet can load two child component. One is loaded eagerly the other one is loaded with lazy loading. Here's the routes file console.component.routes const ConsoleRoutes: Routes = [ { path: 'console', component: ConsoleComponent, data: {context : CONSOLE_CONTEXT}, children: [ { path: "projects", component: ProjectsListComponent, data : {context : PROJECT_CONTEXT}}, --> Loaded eagerly { path: "projects/:projectId/users",

Why handle errors with catchError and not in the subscribe error callback in Angular

断了今生、忘了曾经 提交于 2019-12-05 11:11:45
So I'd normally write my http requests like so Service getData(){ return this.http.get('url') } Component getTheData() { this.service.getData().subscribe( (res) => { //Do something }, (err) => { console.log('getData has thrown and error of', err) }) But looking through Angular documentation they seem to format it like so in a Service getHeroes(): Observable<Hero[]> { return this.http.get<Hero[]>(this.heroesUrl) .pipe( catchError(this.handleError('getHeroes', [])) ); } What's the implicit upside of this as it seems quite verbose to me and I've personally never had the need to pipe my errors. 1

Promise和RxJS处理异步对比

微笑、不失礼 提交于 2019-12-05 10:54:52
Promise 处理异步: let promise = new Promise(resolve => { setTimeout(() => { resolve('---promise timeout---'); }, 2000); }); promise.then(value => console.log(value)); RxJS 处理异步: import {Observable} from 'rxjs'; let stream = new Observable(observer => { setTimeout(() => { observer.next('observable timeout'); }, 2000); }); stream.subscribe(value => console.log(value)); 从上面列子可以看到 RxJS 和 Promise 的基本用法非常类似,除了一些关键词不同。Promise 里面用的是 then() 和 resolve(),而 RxJS 里面用的是 next() 和 subscribe()。 来源: https://www.cnblogs.com/zhx119/p/11921476.html

Hot and shared Observable from an EventEmitter

泪湿孤枕 提交于 2019-12-05 10:46:40
Is there a way to have a hot observable from an EventEmitter (or equivalent available in Angular 2 alpha 46 / RxJS 5 alpha)? i.e. if we subscribe after the value is resolved, it triggers with the previously resolved value . Similar to what we have when always returning the same promise. Ideally, only using Angular 2 objects (I read somewhere a light RxJS would be embedded later to remove the dependency), otherwise importing RxJS is fine. AsyncSubject seems to match my need, but it is not available in RxJS 5 alpha. I tried the following, without success (never triggers). Any idea about how to

Start first call of IntervalObservable instant

流过昼夜 提交于 2019-12-05 10:41:07
I'm using an IntervalObservable to make continuous calls to the server side of my application. I can subscribe and unsubscribe to to the Oberservable and everything works fine with one exception: The first call to the server is delayed, but I want it to be instant. The behaviour of the IntervalObservable is in principle correct, but does not match my requirements. @Injectable() export class LoggerService { constructor(private http: Http) { } private apiURL = 'assets/file.json'; getList() { return IntervalObservable.create(1000).flatMap(() => this.http.get(this.apiURL)) .map(this.extractData)

[Rx.js]when does Zip operator emit error?

家住魔仙堡 提交于 2019-12-05 10:39:06
I am learning Rx.js and have one problem with zip operator: var error =Rx.Observable.throw('Oop!'); var age$ = Rx.Observable.concat(Rx.Observable.of(21,22,23),error); var sex$ = Rx.Observable.of("male","male","female","female"); var name$ = Rx.Observable.of("jack","john","james","lucy"); var example = Rx.Observable.zip(age$,sex$,name$,(age,sex,name)=>{ return {age,sex,name} }); and i subscribe the example source and print some message: example.subscribe({ next: (value) => { console.log(value); }, error: (err) => { console.log('Error: ' + err); }, complete: () => { console.log('complete'); } })

Synchronous stream of responses from a stream of requests with RxJS

爱⌒轻易说出口 提交于 2019-12-05 10:33:52
I'm new to RxJS and was wondering if anyone could help me. I want to create a synchronous stream of responses (preferably with the corresponding requests) from a stream of requests(payload data). I basically want the requests to be sent one by one, each waiting for the response from the last one. I tried this, but it sends everything at once ( jsbin ): var requestStream, responseStream; requestStream = Rx.Observable.from(['a','b','c','d','e']); responseStream = requestStream.flatMap( sendRequest, (val, response)=>{ return {val, response}; } ); responseStream.subscribe( item=>{ console.log(item

RxJS: distinguish single click from drag

萝らか妹 提交于 2019-12-05 10:32:48
I have an AngularJS component that should react to either a single click or a drag (resizing an area). I started to use RxJS (ReactiveX) in my application, so I try to find a solution using it. The Angular side of the request is minor... To simplify the problem (and to train myself), I made a slider directive, based on the rx.angular.js drag'n'drop example: http://plnkr.co/edit/UqdyB2 See the Slide.js file (the other code is for other experiments). The code of this logic is: function(scope, element, attributes) { var thumb = element.children(0); var sliderPosition = element[0]