rxjs5

What is the difference between mergeMap and mergeMapTo?

一曲冷凌霜 提交于 2019-12-10 23:34:31
问题 In rxjs5 doc, it mentions 'To reduce polymorphism and get better performance out of operators, some operators have been split into more than one operator'. What does it actually mean and how to use the mergeMapTo operator? 回答1: From the docs, mergeMapTo: It's like mergeMap , but maps each value always to the same inner Observable. I see mergeMapTo as a shortcut to always output the same value. mergeMapTo doesn't care about the source value. Also from the docs: Maps each source value to the

Resend request angular 2

北城以北 提交于 2019-12-10 20:08:47
问题 In angular 2 app every request to API has header with token, in case token has expired API responds with 401 http code. I have a method to update token, but how I can resend previous request pausing others while a new token is in the process of getting? 回答1: You could extend the Http class for this this way, catch the error using the catch operator of observables: An approach could be to extend the HTTP object to intercept errors: @Injectable() export class CustomHttp extends Http {

Observable.forkJoin wrong return type when more than 6 arguments

淺唱寂寞╮ 提交于 2019-12-10 17:47:03
问题 I have an issue with Observable.forkJoin inferring the wrong return type and then causing errors when I pass more than 6 arguments. Observable.forkJoin(service.getType1, service.getType2, service.getType3 ...) .subscribe(x => { this.type1Arr = x[0]; this.type2Arr = x[1]; this.type3Arr = x[2]; Each function call from the service returns an Observable<Array<type>> . The compiler is determining that the return should be Type1[][] when I have more than 6 calls from the service passed in. It works

How to transform a Subject into an Observable in RxJs 5

冷暖自知 提交于 2019-12-10 16:38:22
问题 How can we transform a Subject into an Observable in RxJs 5 ? This functionality is useful for example when we want to expose the Subject for subscription but don't want to yield control of calling next() on it, and prefer to keep the issuing of new values private. The docs (see here) mention something like this: var subject = new Rx.Subject(); var obs = subject.asObservable(); But in RxJs 5 this currently does not work (alpha 8), we get the following error: "TypeError: subject.asObservable

Handling Angular 2 http errors centrally

£可爱£侵袭症+ 提交于 2019-12-10 15:43:08
问题 I have some code that handles all http access in a class that handles adding tokens. It returns an Observable. I want to catch errors in that class - in particular authentication problems. I am an RXjs beginner and can't figure out how to do this and still return an Observable. A pointer to some fairly comprehensive rxJS 5 documentation (that isn't source code!) would be useful. 回答1: You can leverage the catch operator when executing an HTTP call within a service: getCompanies() { return this

RxJS Map array to observable and back to plain object in array

≯℡__Kan透↙ 提交于 2019-12-10 15:38:37
问题 I have an array of objects from which I need to pass each object separately into async method (process behind is handled with Promise and then converted back to Observable via Observable.fromPromise(...) - this way is needed because the same method is used in case just single object is passed anytime; the process is saving objects into database). For example, this is an array of objects: [ { "name": "John", ... }, { "name": "Anna", ... }, { "name": "Joe",, ... }, { "name": "Alexandra", ... },

How to check if a RxJS Observable contains a string in Angular2?

 ̄綄美尐妖づ 提交于 2019-12-10 15:34:13
问题 I am new to Angular2 and Observable, I want to check if a Observable getRoles which is of type Observable<string[]> contains a string. public hasRole(name: string): boolean { // getRoles is of type Observable<string[]> let getRoles = this.tokenService.getTokenInformation().map(element => element.roles); if (/* check if name is inside of getRoles */) { return true; } return false; } 回答1: Observables are asynchronous so you can't use let getRoles = ...map(...) . The map() method is not executed

Angular2 observable share is not working

夙愿已清 提交于 2019-12-10 15:24:48
问题 Angular2 Observable share is not working and duplicate http calls going BuildingService.ts @Injectable() export class BuildingService { constructor(private http: Http){ } buildings$: Observable<Building[]>; this.buildings: Building[]; getData() : Observable<Building[]>{ this.buildings$ = this.http.get('http://localhost:8080/buildings').share().map(this.extractData); this.buildings$.subscribe(buildings => this.buildings = buildings); return this.buildings$; } private extractData(res: Response)

RxJs / Typescript throws 'property clientX does not exist on type {}'

别说谁变了你拦得住时间么 提交于 2019-12-10 14:37:56
问题 I am trying to do something very simple, just log the event.clientX on mouseover, and this is the module code: import { Observable, Subject } from 'rxjs'; import StarSky from './starsky'; import constants from '../constants/index'; const HERO_Y = constants.HERO_Y(StarSky.canvas); export const StarShip = Observable.fromEvent(StarSky.canvas,'mousemove') .map((event)=> { return { x: event.clientX, y: HERO_Y } }) .startWith({ x: StarSky.canvas.width /2, y: HERO_Y }) Then I subscribe to it later,

RxJS5 - Operator to convert object map to array

早过忘川 提交于 2019-12-10 13:34:14
问题 I have a service that returns an object map which is then used in Angular's ngFor which only takes arrays. So, I am using the map operator with lodash's _toArray to convert the data to an array. Although this works, I then have to import lodash everywhere I need to do this and it seems brittle. I was going to look into creating a custom operator, but perhaps there is an operator that already does this? I can't seem to find the right one(s) Data: { 0 : {data : 'lorem'}, 1 : {data : 'lorem'}, 2