Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the semantics of different RxJS subjects?

Tags:

Documentation for the topic is sparse and it's hard to discover an "entry-point" there.

like image 228
Ivan Kleshnin Avatar asked Jan 18 '16 08:01

Ivan Kleshnin


People also ask

What are different types of subject in RxJS?

But rxjs offers different types of Subjects, namely: BehaviorSubject, ReplaySubject and AsyncSubject.

What are the different types of subjects in Angular?

Subject – No initial value or replay available. BehaviouralSubject – requires an initial value and emits current values to new subscribers. AsyncSubject – Emits latest values to subscribers on completion of the async task. ReplaySubject – replays a specified number of last values to new subscribers.

What are RxJS subject Observable and EventEmitter?

The EventEmitter class is essentially an RxJS observable stream which powers component and directive outputs – extends the RxJS subject class. The subject then, in turn, extends both the RxJS observer and observable class. Thus we are able to operate on it and subscribe to it just like any other RxJS stream.

What is subject and BehaviorSubject in RxJS?

A BehaviorSubject holds one value (so we actually need to initialize a default value). When it is subscribed it emits that value immediately. A Subject on the other hand, does not hold a value.


1 Answers

Semantics differ according to the type of subjects. I will divide them in two kinds : vanilla (Rx.Subject), and special-purpose subjects (the other three). The special-purpose subjects share part of the semantics of the vanilla subject with a few caveats due to their specialization (for instance, completion/reconnection behaviour).

Vanilla Rx.Subject semantics

  1. Key features

    • subjects implement the observer, observable interface (and the disposable interface as they have a dispose handler on their prototype). That means, among other things, they have:
      • observer interface : onNext, onError, onComplete method
      • observable interface : subscribe method
    • you can cast a subject to an observer or to an observable, hiding the implementation of the extra interfaces (cf. .asObserver(), and .asObservable()) if need be
    • the subject being an observable, you can subscribe several observers to it. That observable will then broadcast its data to all its observers. Internally the subject maintains an array of observers.
    • the subject being an observer, you can subscribe it to any observable
    • the observer and the observable which compose the subject being two distinct entities, you can use them independently of the other if that's your use case.
    • disposeing a subject will unsubscribe all observers and release resources.
    • Subjects do not take a scheduler but rather assume that all serialization and grammatical correctness are handled by the caller of the subject.
    • The default behaviour of subjects is to emit synchronously their values to the observers, starting with the first subscribed observer to the last. In most cases, order will not matter, in others it will.

I quote a key aspect of Rxjs contract and grammar :

This grammar allows observable sequences to send any amount (0 or more) of onNext messages to the subscribed observer instance, optionally followed by a single success (onCompleted) or failure (onError) message.

  • a vanilla subject (created with new Rx.Subject()) implements that grammar : when onCompleted has been called once, all subsequent calls to onNext are ignored. Second call of onCompleted on the same observer is also ignored. If an observer subscribes to the observable side of the subject, its onComplete callback will immediately be called (http://jsfiddle.net/cLf6Lqsn/1/).

    1. Creation

      • new Rx.Subject()

    Returns a subject which connects its observer to its observable (jsfiddle). This example is taken from the official documentation and portrays how to use subjects as proxies. The subject is subscribed to a source (observer side), and is also listened on by observers (observable side). Any call to onNext (observer side) results in the observable side calling onNext with the same value for each of its observers.

    • Rx.Subject.create(observer, observable)

    Creates a subject from the specified observer and observable. Those two are not necessarily connected. A good example can be seen in the implementation of Rx.Dom.fromWebSocket which returns a subject used to send and receive data from a socket. The observer side of the subject sends data to the socket. The observable side is used to listen on incoming data from the socket. Also, a subject created this way does NOT have a dispose method.

Specialized Rx.Subject semantics

  • This reactivex.io documentation covers pretty well most of the semantics of the specialized subjects.
  • The other interesting points to mention concern behavior past completion.
  • Sample code illustrating the behaviour are here for async, behavior, replay

Hopefully I did not get too much wrong. I'll be happy to be corrected. Last note, this should be valid for RxJS v4.

For a detailed explanation of the behaviour of cold/hot observables, one can refer to : Hot and Cold observables : are there 'hot' and 'cold' operators?

like image 100
user3743222 Avatar answered Sep 18 '22 22:09

user3743222