rxjs

Why RXJS angular behaviorSubject emit mutiple values

允我心安 提交于 2020-07-22 06:28:08
问题 enter image description hereI use BehaviorSubject to share data between my app components, I have a performance issue due to mutiple emission of same value from BehaviorSubject . For instance, I call http to get team Object from backend and store it in a behaviorSubject, many components subscribe to this BehaviorSubject . Each component gets the value from subscription and does a sequence of manipulations on the value. The essence of the problem is that the value is emitted many times and

RXJS: Combining multiple calls from foreach into a single observable

白昼怎懂夜的黑 提交于 2020-07-22 06:09:29
问题 I've been trying to combine multiple async calls for a while now, and everytime I get close, I get stuck at a foreach loop I'm trying to solve. At the moment I pass an array of categories to a function, which will return an observable containing an array of all the questions (of all the categories combined). I was thinking of getQuestions(categories: Category[]): Observable<Question[]> { let q: Question[]; categories.forEach(c => { this.cs .getQuestionsCategory(c.id) .pipe(map(questions => q

Observable emit only one value using Delay within ConcatMap

大城市里の小女人 提交于 2020-07-22 06:03:14
问题 Having the following code: range(1, 30) .pipe( windowCount(10), concatMap(x => x.pipe(delay(5000))), ) .subscribe(console.log); For some reason only the first value is emitted (1..10), Could someone point out what's wrong with the above code? Thanks. expected output: 1..10 (delay) 11..20 (delay) and so on.... 回答1: This is happening because windowCount will complete the previous inner "window" before creating a new one. You delay each "window" by 5s but when concatMap wants to subscribe to the

Angular Observable need to complete first

房东的猫 提交于 2020-07-22 03:24:26
问题 @Input() list: string[]; ngOnInit() : void { this.valueMap = new Map<any, any>(); this.getDataFromService(); this.buildContainer(); } private getDataFromService(): void { this.list.forEach(value-> { this.fetchService(value).subscribe(data ->{ this.valueMap.set(value,data); } ) }) } private buildContainer(): void { console.log(this.valueMap.size); // shows always 0 even when service returns the data } Now thing is that I have to use this valueMap in the method buidlContainer() , hence I need

Angular Observable need to complete first

喜你入骨 提交于 2020-07-22 03:23:16
问题 @Input() list: string[]; ngOnInit() : void { this.valueMap = new Map<any, any>(); this.getDataFromService(); this.buildContainer(); } private getDataFromService(): void { this.list.forEach(value-> { this.fetchService(value).subscribe(data ->{ this.valueMap.set(value,data); } ) }) } private buildContainer(): void { console.log(this.valueMap.size); // shows always 0 even when service returns the data } Now thing is that I have to use this valueMap in the method buidlContainer() , hence I need

ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable in Angular Services

我的梦境 提交于 2020-07-21 03:54:04
问题 I have written multiple services to hit different APIs. The services written for post are working somehow but giving this error ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at subscribeTo (subscribeTo.js:27) As for the services written for the GET method the same error is shown but i get no data in return. Here is my service GetClientData(): Observable<ClientDto> { let url_ = this.baseUrl + "https:/

RxJs: Executing 3 observables one after another and using results from first in second, and first and second in third requests

孤街浪徒 提交于 2020-07-20 11:30:58
问题 I need to be able to execute 3 observables one after another so that I can use result value from 1st one in second and also 1st and 2nd result in the third one. Something like this ( it doesn't work as the serviceId is not visible in the third request ): private setupStuff(): void { this.initRouteParams().pipe( switchMap(serviceId => this.getFileInfo(serviceId)), switchMap(fileName => this.getExistingFile(serviceId, fileName) .subscribe(response => { console.log(response); })) ); } 回答1: You

RxJs switchMap with angular HttpClient

|▌冷眼眸甩不掉的悲伤 提交于 2020-07-19 00:56:49
问题 I have a use case whenever a new request is triggered, any http requests that are already in flight should be cancelled / ignored. For eg : A request (say #2) comes in while the request #1 takes too long to respond / slow network connectivity. In this case #2 gets a very quick response from server then at any point even if the #1 comes back with a response the HTTP response observable should be ignored. The problem i face is, first the component displays response values from request #2 and

Is unsubscribe needed if an Observable uses an async pipe?

馋奶兔 提交于 2020-07-09 09:08:16
问题 I need to determine whether two different approaches for handling Observables are equally valid, or if one will cause memory issues. In the following example, foo$ and bar are template variables that receive their values from a service. Each has its own Observable. In the component, bar is explicitly given its value from a subscription and later ends that subscription in OnDestroy() . foo$ , however, does not explicitly subscribe to a service but rather uses an async pipe in the template. Are

Is unsubscribe needed if an Observable uses an async pipe?

末鹿安然 提交于 2020-07-09 09:05:13
问题 I need to determine whether two different approaches for handling Observables are equally valid, or if one will cause memory issues. In the following example, foo$ and bar are template variables that receive their values from a service. Each has its own Observable. In the component, bar is explicitly given its value from a subscription and later ends that subscription in OnDestroy() . foo$ , however, does not explicitly subscribe to a service but rather uses an async pipe in the template. Are