I am facing an issue with my code where the setDischarges method is not being executed if the condition in the filter (!!discharges && !!discharges.length) is met.
loading: boolean;
this.discharge$ = this.dischargeService.getObservable('discharges');
this.subDischarge = this.discharge$
.pipe(
filter((discharges: Discharge[]) => !!discharges && !!discharges.length),
map((discharges: Discharge[]) => discharges),
)
.subscribe((discharges) => this.setDischarges(discharges));
I have attempted to update the loading variable value based on that condition, but it is not working as expected. The issue arises because the code does not subscribe when the condition is not met and therefore cannot execute setDischarges. I need a solution where the loading variable is updated whether the condition is met or not, without affecting the subscription when the condition is not met.
loading: boolean;
this.discharge$ = this.dischargeService.getObservable('discharges');
this.subDischarge = this.discharge$
.pipe(
filter((discharges: Discharge[]) => !!discharges && !!discharges.length
? this.loading = false : this.loading = true),
map((discharges: Discharge[]) => discharges),
)
.subscribe((discharges) => this.setDischarges(discharges));