RXJS: Subscribe adding a new loop on the observable scan operator

Viewed 101

I have recently started with RxJS but have not been able to find information with regards to the following:

When scanning an Observable every subscription adds a new iteration to the scanning operation.

It looks like a bug or a bad implementation of the code.

The below link demonstrates this:

https://codesandbox.io/s/fancy-bird-0m81p

I have 3 subscriptions, which means 3 extra loops on the scan function.

Comment out a subscription and see the change in the console window.

Is this expected, is it a bug or is it bad implementation?

If it is expected please link to an explanation if possible. Also is it possible to stop this from happening?

Thanks ahead!

1 Answers

Just to add more from the comments from @JB Nizet How share work is similar to the following code example

getShareObservable(){
 const share = new Subject()
 Store$.pipe(
  scan((acc, curr) => {
    console.log("Scanning...");
    return Object.assign({}, acc, curr);
  }, initialState).subscribe(share)
  return share
}

const shareObservable=getShareObservable()

then your scan won't run multiple times

Related