Subject 与 Observable 的区别

原文链接:http://javascript.tutorialhorizon.com/2017/03/23/rxjs-subject-vs-observable/

In order to under­stand the dif­fer­ence between a Sub­ject and an Observ­able, you need to be aware of two dis­tinct con­cepts
– A data pro­ducer
– A data consumer

An observ­able, by def­i­n­i­tion is a data pro­ducer. Albeit a spe­cial kind that can pro­duce data over time.

A Sub­ject on the other hand can act as both — a data pro­ducer and a data consumer.

This implies two things.
1. A sub­ject can be sub­scribed to, just like an observ­able.
2. A sub­ject can sub­scribe to other observables.

That being said, there is one crit­i­cal dif­fer­ence between a sub­ject and an observable.

All sub­scribers to a sub­ject share the same exe­cu­tion of the sub­ject. i.e. when a sub­ject pro­duces data, all of its sub­scribers will receive the same data. This behav­ior is dif­fer­ent from observ­ables, where each sub­scrip­tion causes an inde­pen­dent exe­cu­tion of the observable.

Lets see some examples

var subject = new Rx.Subject();

// Here the subject is acting like a data producer
// because it is being subscribed to
subject.subscribe(v => console.log('consumer A: ' + v));
subject.subscribe(v => console.log('consumer B: ' + v));

// Create a source of the data, which in our case is an observable
var observable = Rx.Observable.from([0, 1]);

// Here the same subject acs as a data consumer because it
// can subscribe to another observable
source.subscribe(observable);

/* Prints */
// Consumer A: 0
// Consumer B: 0
// Consumer A: 1
// Consumer B: 1

There are a few impor­tant things to notice about this exam­ple.
1. I had to setup my sub­scrip­tions before my sub­ject sub­scribed to the source.
2. Both the con­sumers logged the same value for v.

The fact that our con­sumers log the same data implies that the broad­casted data is shared with all the consumers/subscribers. This is unlike observ­ables, where each consumer/subscriber causes the observ­able func­tion to be re-executed.

Lets see another exam­ple to clar­ify this.

Exam­ple 1

Vanilla observ­able, inde­pen­dent execution

var observable = Rx.Observable.create(function(source) {
  source.next(Math.random());
});

observable.subscribe(v => console.log('consumer A: ' + v));
observable.subscribe(v => console.log('consumer B: ' + v));

/* Prints DIFFERENT values for both consumers */
// consumer A: 0.25707833297857885
// consumer B: 0.8304769607422662
Exam­ple 2

Observ­able wrapped in a sub­ject, caus­ing shared execution

var observable = Rx.Observable.create(function(source) {
  source.next(Math.random());
});

var subject = new Rx.Subject();

subject.subscribe(v => console.log('consumer A: ' + v));
subject.subscribe(v => console.log('consumer B: ' + v));

observable.subscribe(subject);

/* Prints SAME values for both consumers */
// consumer A: 0.8495447073368834
// consumer B: 0.8495447073368834

You can also man­u­ally broad­cast data from a sub­ject to it observers using the next() func­tion as shown below.

var subject = new Rx.Subject();

subject.subscribe(v => console.log('consumer A: ' + v));

subject.next('hello world');

/* Prints */
// consumer A: hello world

I hope this post effec­tively takes you a step closer towards get­ting com­fort­able with using Sub­jects and Observ­ables in your projects with confidence.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值