0

I am new to NgRx and not an expert at RxJs hence the question. We have to use it in an angular app. I have a component where user selects an orderId from a drop down in one of its fields. An other component has a search button to trigger a search based on the order id. Searching returns a list of items related the orderId and shows it in a table component. I am saving the order id in the store in the view state as soon as the user selects it. Now when the user clicks on search I have to get this order Id from the store. I tried to do it like so :

onClickSearch() {
    this.store.pipe(select(fromOrderIdSelectors.selectOrderId))
          .subscribe((orderId: string) => {
    this.store.dispatch(fromOrderActions.getOrderItems({orderId : orderId});
    }));
}

This works fine for the search but since I am subscribing to the store now, the search request is dispatched as soon as the order id is set in the store even from any other place in the app. Should I get and unsubscribe every time to the observable manually? Or is there some RxJs magic I can use to implement this.

humbleCoder
  • 667
  • 14
  • 27
  • 1
    It sounds like you could just add `take(1)` so it'll unsubscribe automatically – martin Dec 26 '20 at 11:30
  • I dont have to then destroy it or something right since it unsubscribes itself? I did this this.store.pipe( select(fromOrderActions.selectOrderId), take(1) ).subscribe ..... – humbleCoder Dec 26 '20 at 11:54
  • Got the answer from your answer here https://stackoverflow.com/questions/40563065/difference-between-unsubscribe-to-take1. You are guru it seems. Do you know a good talk or course or book to understand RxJs in depth? – humbleCoder Dec 26 '20 at 12:02
  • @martin Unless I call subscribe on the observable it doesn't create a memory leak right? I ask since take(1) will unsubscribe only when at least one item is emitted. – humbleCoder Dec 26 '20 at 12:12

0 Answers0