5

What's the difference between Subject and AnonymousSubject in RxJS 5? I've searched the internet but didn't find any info about AnonymousSubject.

I've found an example on the web

Subject.create(observer, observable);

Looking into rxjs source code I saw that this creates and AnonymousSubject. Can you also come up with an example when is good to use AnonymousSubject?

Doua Beri
  • 10,612
  • 18
  • 89
  • 138
  • There is a use case in the RxJS4 documentation: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/subjects/subject.md#example-1 – cartant Feb 10 '17 at 07:52

2 Answers2

9

The AnonymousSubject doesn't subscribe itself to the source Observable. It just connects the source and destination.

I don't know what a typical use-case for AnonymousSubject looks like but it's used inside Subject.lift().

Also see: RxJs Subject.subscribe method not working as expected

Community
  • 1
  • 1
martin
  • 93,354
  • 25
  • 191
  • 226
1

You can use AnonymousSubject to define an operator that returns a hot observable. Usually the Observable will be a Subject too, for example a BehaviorSubject.

On the Observer side, the onNext method will "do something" with the value it receives and push to the destination Subject. For example an the observer could accept a boolean value for "is the network connection available", try connecting to a server when the network connection becomes available, and push true to a BehaviorSubject if the attempt to connect succeeds. This AnonymousSubject would tell you upon subscription whether you are connected to the server.

Paolo Bonzini
  • 1,900
  • 15
  • 25