Reputation: 5058
I have stream that is triggered by next()
method of Rx BehaviorSubject
. How do I share the pipedData$
stream between subscribers?
I tried to use share
operator here to avoid heavy recalculations inside the map
operator but no luck - every subscriber produces recalculations.
here's stackblitz for this issue
Also is it possible to get number of subscribers from the source?
import { interval, BehaviorSubject } from 'rxjs';
import { take, map, tap, share, debounceTime } from 'rxjs/operators';
const data$ = new BehaviorSubject(null);
interval(1000).pipe(
tap(x => console.log('emit:')),
take(3)
).subscribe((x)=>{
data$.next(x)
});
const pipedData$ = data$.pipe(
debounceTime(30),
share(),
map(x => Math.random()),
);
console.log("--=-=-=-=--=-=----=-=-=-=-==-")
pipedData$.subscribe(x => console.log(x));
pipedData$.subscribe(x => console.log(x));
pipedData$.subscribe(x => console.log(x));
Upvotes: 0
Views: 71
Reputation: 13539
Looks like you need to move share
below map
to get the desired behaviour.
Upvotes: 2