Subject vs BehaviorSubject vs ReplaySubject in Angular

。_饼干妹妹 提交于 2019-11-27 02:31:37

It really comes down to behavior and semantics. With a

  • Subject - a subscriber will only get published values that were emitted after the subscription. Ask yourself, is that what you want? Does the subscriber need to know anything about previous values? If not, then you can use this, otherwise choose one of the others. For example, with component-to-component communication. Say you have a component that publishes events for other components on a button click. You can use a service with a subject to communicate.

  • BehaviorSubject - the last value is cached. An subscriber will get the latest value upon initial subscription. The semantics for this subject is to represent a value that changes over time. For example a logged in user. The initial user might be an anonymous user. But once a user logs in, then the new value is the authenticated user state.

    The BehaviorSubject is initialized with an initial value. This is sometimes important to coding preference. Say for instance you initialize it with a null. Then in your subscription, you need to do a null check. Maybe OK, or maybe annoying.

  • ReplaySubject - it can cache up to a specified number of emissions. Any subscribers will get all the cached values upon subscription. When would you need this behavior? Honestly I have not had need for such behavior, except for the following case:

    If you initialize a ReplaySubject with a buffer size of 1, then it actually behaves just like a BehaviorSubject. The last value is always cached, so it acts like a value changing over time. With this, there is no need for a null check like in the case of the BehaviorSubject initialized with a null, because no value is ever emitted to the subscriber until the first publishing.

So it really comes down the behavior you are expecting, as for which one to use. Most the time you will probably want to use a BehaviorSubject because what you really want to represent is that "value over time" semantic. But I personally don't see anything wrong with the substitution of ReplaySubject initialized with 1.

What you want to avoid is using the vanilla Subject when what you really need is some caching behavior. Take for example you are writing a routing guard or a resolve. You fetch some data in that guard and set it in a service Subject. Then in the routed component you subscribe to the service subject to try to get that value that was emitted in the guard. OOPs. where's the value? It was already emitted, DUH. Use a "caching" subject!

See also:

A handy summary of the different observable types, non intuitive naming i know lol.

  • Subject - A subscriber will only get published values thereon-after the subscription is made.
  • BehaviorSubject - New subscribers get the last published value OR initial value immediately upon subscription.
  • ReplaySubject - New subscribers get the last 1-n published value(s) immediately upon subscription (only if previously emitted).

From: Randall Koutnik book “Build Reactive Websites with RxJS.” :

A Subject is an object that’s a turbocharged observable. At its core, a Subject acts much like a regular observable, but each subscription is hooked into the same source. Subjects also are observers and have next, error, and done methods to send data to all subscribers at once. Because subjects are observers, they can be passed directly into a subscribe call, and all the events from the original observable will be sent through the subject to its subscribers.

We can use the ReplaySubject to track history. A ReplaySubject records the last n events and palys them back to every new subscriber. For example in chat application. We can use it for tracking the record of previous chat history.

A BehaviorSubject is a simplified version of the ReplaySubject. The ReplaySubject stored an arbitrary number of events, the BehaviorSubject only records the value of the latest event. Whenever a BehaviorSubject records a new subscription, it emits the latest value to the subscriber as well as any new values that are passed in. The BehaviorSubject is useful when dealing with single units of state, such as configuration options.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!