RxJS5订阅和观察者的区别 [英] Difference between RxJS5 subscription and observer

查看:42
本文介绍了RxJS5订阅和观察者的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些与 Rx 订阅/观察者相关的问题/答案,但它们可能适用于旧版本的 Rx,也不适用于 RxJS,后者可能符合不同的 API.

I see some question/answers relating to Rx Subscriptions/Observers but they may be for older versions of Rx and also not for RxJS, which may conform to a different API.

我的印象是订阅/订阅者和观察者都是一样的.如果您查看文档,它们位于不同的相邻部分,但似乎完全相同:

I was under the impression that subscriptions/subscribers and observers were all the same. If you look at the docs, they are in different adjacent sections, but seem to be exactly the same:

观察者:http://reactivex.io/rxjs/manual/overview.html#observer

订阅:http://reactivex.io/rxjs/manual/overview.html#subscription

到底有什么区别?有人可以举一个例子来说明两者之间的实际区别吗?

what the heck is the difference? Can someone given an example with a practical difference between the two?

推荐答案

Observer 是 Observable 传递的值的消费者.

An Observer is a consumer of values delivered by an Observable.

所以基本上观察者接收流发出的值.

So basically the observer receives the values emitted by a stream.

订阅是一个对象,它代表一个一次性资源,通常是一个 Observable 的执行.

A Subscription is an object that represents a disposable resource, usually the execution of an Observable.

订阅基本上只是某个观察者当前接收数据的事实",如果您取消订阅订阅,流和观察者都将仍然存在,只是不再连接.

A subscription is basically just a "fact" that a certain observer currently receives data, if you unsubscribe a subscription, both the stream and the observer will still exist, they are just not connected any more.

混合了伪代码的真实世界比喻:报纸

A real-world metaphor mixed with pseudo-code: Newspaper

Stream:这将是报纸的生产链(涉及出版公司创建内容和印刷公司印刷报纸>)

Stream: This would be the production-chain of the newspaper (involing the publishing company creating the content and the printing house printing the paper)

const newsPaper$ = Observable.interval(DAILY)
    .switchMapTo(date => publishingCompany.createContent(date))
    .switchMapTo(content => printingHouse.printPaper(content))
    .publish()
    .refCount();

观察者:这将是读者/收件人,那个穿着浴袍的人每天早上在他的前院拿起报纸阅读.>

Observer: This would be the reader/recipient, that guy with a bathrobe that picks up the newspaper in his front-yard every morning to read it.

const bathrobeGuy = {
    next: newsPaper => readPaper(newsPaper),
    error: errorMsg => complainAbout(errorMsg), // the bathrobe guy will be so angry, the he unsubscribes the paper
    complete: () => subscribeToDifferentNewsPaper()
}

订阅:这是报纸订阅 - 送货员每天早上将报纸扔到每个前院.

Subscription: This is the news-paper-subscription - the delivery-boy throwing the newspaper into each front-yard every morning.

// this will activate the "delivery boy"
const paperSubscription = newsPaper$.subscribe(bathrobeGuy);

退订:当浴袍男决定不再需要报纸时,他可以退订报纸,送货员将不再递送任何报纸.然而观察者(浴袍人)和报纸制作仍然存在,但他们已经不再有任何关系.

Unsubscribing: When the bathrobe-guy decides to not want the paper any more, he can unsubscribe the paper and the delivery-boy will not deliver any paper any more. However the observer(the bathrobe-guy) and the newspaper-production still exist, but they have simply no relationship any more.

paperSubscription.unsubscribe();

这篇关于RxJS5订阅和观察者的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆