2

NOTE: I'm calling the set function in a component that has an ngFor. So when I call set function the ngOnInit gets executed multiple times so the set function executes as well multiple times. I have an observable with an object that I update to keep data consistent.

private app= this.subject.asObservable().distinctUntilChanged();

I access the data using this method

select<T>(name: string): Observable<T> {
   return this.app.pluck(name);
}

When I update a this "app" on every user selection then I enter an infinite loop. So $info starts printing the same information over and over again. And the "set" gets called over and over again. I'm using the async pipe to subscribe and unsubscribe in the html view. ANY IDEAS ?

updateFruits(fruit) {
  this.app.set('Fruits', fruit);
}

set(name: string, state: any) {
  this.subject.next({ ...this.value, [name]: state });
}

   this.info$ = combineLatest(fruits$)
  .pipe(
      distinctUntilChanged(),
      debounceTime(0)
      map(fruits => fruits)
   );
AcarMeel
  • 21
  • 3

1 Answers1

0

I think to use operators like pluck or distinctUntilChanged you have to use pipe.

private app = this.subject.asObservable().pipe(distinctUntilChanged());

select<T>(name: string): Observable<T> {
   return this.app.pipe(pluck(name));
}

I've never seen before that method set for observables too, only next.

-- Edit

I see, I had thought that this.app was as observable.

I tried to reproduce the bug but everything seems fine: Hope that helps better: https://stackblitz.com/edit/rxjs-stack

Yasser Nascimento
  • 1,302
  • 7
  • 13
  • "set" refers to this method I defined, where I update the value set(name: string, state: any) { this.subject.next({ ...this.value, [name]: state }); } – AcarMeel Jun 06 '20 at 20:37
  • Thanks! Could it be because the component where I try to "set" the value uses ngFor to display the info? I see that after calling set function the ngOnInit runs over and over again :o – AcarMeel Jun 06 '20 at 23:29