0

In my component, i.e. componentDidMount() I am subscribing to a state of my rxjs store, which holds an object array workingHours: WorkingHours[], that might have items for this month (for example an amount of 8 working hours for the date 27.02-2023 as one item out of 28).

    this.context.store.state$
      .pipe(map((state) => state.workingHourList[this.props.employeeId] || []))
      .subscribe((workingHours) => {
        if (workingHours.length > 1) {
          this.updateWorkingHours(
            workingHours.filter(
              (wl) => wl.date.getMonth() === this.props.month.getMonth()
            )
          );
        }

The problem is, that workingHours in subscribe((workingHours) => { ... might have zero items, until it finally receives the data from the database. So previously I check, if there are items in workingHours with

if (workingHours.length > 1) {

I don't want to go the bad way of coding and use setTimeout on a few seconds (which works), so I actually want to check if the workingHours are the "finalized" workingHours and there won't come another one.

Tobias S.
  • 21,159
  • 4
  • 27
  • 45
der papa
  • 35
  • 5
  • I don't know much Rxjs (at least, that's what it looks like to me) but isn't this just a matter of [getting the last value](https://stackoverflow.com/questions/37174598/how-to-get-last-value-from-a-subject)? – kelsny Feb 27 '23 at 16:10
  • As I see you use `store$` which is updated by remote request, from here you know nothing about the request state. I guess you should add a new field to the state about the request state (started, completed, failed etc). – Sh Svyatoslav Feb 27 '23 at 16:15
  • BehaviorSubject immediately emits the last value to new subscribers. This is not a new subscription. In my console, the first three outputs for workingHours are empty arrays, then I get a full array – der papa Feb 27 '23 at 17:00
  • You can pass the request state to that subject, and show data when the state is completed – Sh Svyatoslav Feb 28 '23 at 06:15

0 Answers0