Difference between RxJS5 subscription and observer

 ̄綄美尐妖づ 提交于 2019-12-12 03:58:55

问题


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:

Observer: http://reactivex.io/rxjs/manual/overview.html#observer

Subscription: 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?


回答1:


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

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

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: 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();



回答2:


An Observer is an object with a set of callbacks that are executed when you subscribe to an Observable. In other words, when you call subscribe you pass an object of type Observer. Even when you only pass a callback, internally rxjs is creating an Observer with your callback as the next property. Other properties are error and complete.

A Subscription is the return type of the call subscribe, and its only purpose is to be able to call subscription.unsubscribe() in order to not listen to that subscription anymore. The Observer functions (next, error, complete) will no longer be called.

var myObserver = {
  next: (val) => {},
  error: (err) => {},
  complete: () => {}
};

var mySubscription: Subscription = myObservable.subscribe(myObserver);

// then, if later you want to unsubscribe:

mySubscription.unsubscribe()


来源:https://stackoverflow.com/questions/41949355/difference-between-rxjs5-subscription-and-observer

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