I'm trying to pass the return of a service layer with subject to the components.
My service that connects with api:
public getPerfilNew$(): Observable<PerfilInvestidor> {
return this.http.get<PerfilInvestidor>(`${environment.api.basePosicaoConsolidada}/consolidado`)
.pipe(
map(res => res),
shareReplay(1),
catchError(err => {
console.log('Error in perfil', err);
return throwError(err);
})
)
}
public getPositionConsolidated(): Observable<PosicaoConsolidada> {
return this.http.get<PosicaoConsolidada>(`${environment.api.basePosicaoConsolidada}/consolidado`)
.pipe(
map(res => res),
shareReplay(1),
catchError(err => {
console.log('Error investiments', err);
return throwError(err);
})
)
}
In my component: I tried it
public loadData() {
//find out if the profile failed or no
this.homeGuardService.getPerfilNew$.pipe(
takeUntil(this.unsubscribe)
).subscribe(res => {
this.perfilInvestidor = res;
this.perfilFailed = this.perfilInvestidor.status ? true : false;
console.log('perfil is failed --->', this.perfilFailed)
})
//checking for investments
this.homeGuardService.getPositionConsolidated().subscribe(res => {
this.positionConsolidated = res
if (this.positionConsolidated) {
this.investments = true
}
});
this.isEverthingFailed = !this.investments && this.perfilFailed
}
I need external subscribe values to match them in my variable isEverthingFailed teria que utilizar subject? BehaviorSubject? Because that way the variable investments and perfilFailed is undefined
With that amount of observables I compromise the memory? I'm open to suggestions
You can use forkJoin or combineLatest to get both the streams and subscribe to them:
public loadData() {
forkJoin([this.homeGuardService.getPerfilNew$(), this.homeGuardService.getPositionConsolidated()])
.subscribe(([perfile, position]) => {
this.perfilInvestidor = perfile;
this.perfilFailed = this.perfilInvestidor.status ? true : false;
console.log('perfil is failed --->', this.perfilFailed)
this.positionConsolidated = position
if (this.positionConsolidated) {
this.investments = true
}
this.isEverthingFailed = !this.investments && this.perfilFailed
})
)
}
1. Questions
There is no enough code here to see exactly what is happening. But at first glance it looks like a race condition from my point of view:
You have to realise how Subject works.
const sub = new Subject();
sub.next(1);
// If you subscribe here like:
sub.subscribe(a => console.log(a))
// Nothing will happen because Subject does not preserve the old values
But if you do this:
const sub = new Subject();
// If you subscribe here like:
sub.subscribe(a => console.log(a))
sub.next(1);
// You will get a print in a console since you emitted the value after you subscribed on the stream.
In your case it looks like that you are subscribing to a subject after subject already emitted values.
Also:
this.perfilClient$.subscribe(res => {
You are subscribing to this.perfilClient$, but i dont see where you are emitting values on that stream?
So if you cannot sync things that you subscribe to the Subject before it emits the value, you can use ReplaySubject or BehaviourSubject
2. Questions You have just a few observables, its not much. But there are are some antipatterns such as:
The main thing in RXJs is to avoid side effects as much as possible.