Search code examples
angulartypescriptangular7

Update data subscribed to without starting another cycle


I am getting data from a ngrx/store I am subscribing to the data

this.store.select(somedataList)
  .subscribe(myList => {

  myList.propertyA = "a different value";

});

Once I have subscribed and changed the property values I need to I update the data in the store.

this.store.dispatch(new UpdateSomeDataList(myList));

Once I call "dispatch" the ".subscribe" kicks off again and creates an endless loop.

How can I update the data without creating an endless loop?


Solution

  • If you need a subscription to fire only once - use take(1)/first() RxJS's operators

    this.store.select(somedataList).pipe(take(1))
    

    and in this case, your subscription would unsubscribe after the first value

    Calling dispatch after subscribing for same store's part - it would be looped, so you need to rethink your data flow