I attempted to utilize the rxjs share operator in two distinct manners.
- Implementing it in the component
Component:
constructor() {
const obs1 = interval(1000).pipe(
tap(x => console.log('processing in comp1')),
map(x => x * x),
take(1),
share()
);
obs1.subscribe(x=>console.log('testShare1'))
obs1.subscribe(x=>console.log('testShare2'))
}
Outcome:
processing in comp1
testShare1
testShare2
It functioned as anticipated.
- Invoking it from an external service.
Service:
export class TestShareService {
constructor() { }
testShare() {
const obs1 = interval(1000).pipe(
tap(x => console.log('processing in service ')),
map(x => x * x),
take(1),
share()
);
return obs1;
}
}
Component:
constructor(private shareSrv: TestShareService) {
shareSrv.testShare().subscribe(x=>console.log('testShare1 using service'));
shareSrv.testShare().subscribe(x=>console.log('testShare2 using service'));
}
outcome:
processing in service
testShare1 using service
processing in service
testShare2 using service
It seems that the rxjs share operator does not have an impact when the observable is situated in an external service. Is this accurate? Or could I be making a mistake?
Appreciate your help.