I am facing an issue with subscribing to an observable using the async pipe in Angular. The data in the pipe may come from different sources and have varying structures based on the source.
The current implementation is not working as expected because I am overriding the initial value of this.data$
, resulting in source A not being subscribed to.
Is there a way to 'split' the pipe based on a filter?
problem.component.ts:
// Data source A and logic A
this.data$ = this.service.context.pipe(
filter(context => context.flag === true),
switchMap(context => this.dataSourceA(context.id)),
map(data => this.processDataA(data))
);
// Data source B and logic B
this.data$ = this.service.context.pipe(
filter(context => context.flag === false),
switchMap(context => this.dataSourceB(context.id)),
map(data => this.processDataB(data))
);
problem.component.html
<pro-table [data]="data$ | async"></pro-table>