1

I have short question. I have this code in a service:

newChartConfig = new Subject()
newChartConfigAction$ = this.newChartConfig.asObservable()

And use it in this a component like this:

 this.areaChartCacheService.newChartConfigAction$.pipe(
      map((x: any) => {
        this.yrange = x.yrange;
        this.xrange = x.xrange;
      })
    ).subscribe(
      () => console.log('complete LiineChart')
    )

But so my question is:

is it necessary to do asObservable?

Because Subject is already observable.

Thank you

Because if I remove asObservable it still works

And after it is used like this:

 async getObjectFromStore(id) {
    const db = await openDB<CaDB>('ca-state', 1)

    await db.get('ca-widgets', id).then(
      result => {
        this.newChartConfig.next(result)
        this.newChartParams.next(result.params)
      }
    );
  }
  • 1
    [This](https://medium.com/@benlesh/on-the-subject-of-subjects-in-rxjs-2b08b7198b93#:~:text=Probably%20a%20more%20important%20distinction,also%20implement%20an%20Observer%20interface.) explains the difference pretty well I think –  Mar 01 '21 at 12:25
  • 1
    So the conclusion is that asObservable is not necessarily? That is my question – savantCodeEngineer Mar 01 '21 at 12:33
  • ? But so asObservable can be removed – savantCodeEngineer Mar 01 '21 at 12:44
  • 1
    One of my teammembers asked Ben Lesh in a life conference via twitter questions what's the sense asObservable is. The main use-case is library apis. Use asObservable whenever you want to limit the interaction to your subjects 100%. In your own code base you can use the typescript compiler to avoid interacting by simply using the type Observable instead of eg Subject. const foo: Observable = new Subject(); – Jonathan Stellwag Mar 01 '21 at 14:16
  • 1
    @JonathanStellwag i think that you should submit your comment as an answer. – Християн Христов Mar 01 '21 at 15:35

0 Answers0