问题描述
在许多使用RxJS的代码库中,我似乎遇到了通过 getter
或普通 getObservable()
函数.我的问题不是为什么使用 .asObservable()
,而是为什么它看起来如此普遍地封装在getter/factory函数中?
In a lot of codebases using RxJS I seem to come across the pattern of exposing private Subjects
as Observables
via a getter
or normal getObservable()
function. My question is not why .asObservable()
is used, but instead why it seems so commonly wrapped in a getter/factory function?
封装在getter/factory函数中的asObservable()
private readonly _engineInfo$ = new Subject<EngineInfo>();
get engineInfo$() { return this._engineInfo$.asObservable(); }
asObservable()作为实例变量
private readonly _engineInfo$ = new Subject<EngineInfo>();
public engineInfo$ = this._engineInfo$.asObservable();
问题
- 我不了解的是,每次订阅
Subject
时,.asObservable()
都会创建一个新的Observable
.创建的Observable
也是 hot ,可以多次订阅.为什么要创建多个Observable
,(每个访问/订阅一个)的匿名实例,而不是只创建一个Observable
,所有观察员都订阅了一项/一项服务吗? - 此
getter/factory function
模式是否有明显优势? - 可能是由于垃圾收集或测试/模拟的优势吗?
- My undestanding is that
.asObservable()
creates a newObservable
every time that subscribes to theSubject
. Also the createdObservable
is hot and can be subscribed multiple times. Why would one create multiple anonymous instances ofObservable
, (one for each access/subscription), instead of having just oneObservable
, accessed at one class/service, that all observers subscribe to? - Is there a non-obvious advantage to this
getter/factory function
pattern? - Could it be because of garbage collection or testing/mocking advantages?
到目前为止,我在所有服务/类中都使用了第二个示例中的实例变量设置,并且一切似乎都可以正常工作,并且具有多个观察者.
So far I'm using the instance variable setup from the second example in all services/classes and everything seems to works as expected, also with multiple observers.
推荐答案
何时使用Subject.prototype.asObservable()
此操作的目的是防止对象的观察者侧"泄漏出API.基本上是为了防止在您不希望人们能够接着"到结果可观察对象中时进行抽象泄漏.
When to use Subject.prototype.asObservable()
The purpose of this is to prevent leaking the "observer side" of the Subject out of an API. Basically to prevent a leaky abstraction when you don't want people to be able to "next" into the resulting observable.
您永远不想将Subject实例返回到调用上下文.这样做有点类似于返回Deferred对象而不是Promise.并且,这会使主体对意料之外的破坏性使用持开放态度.这样,在公开主题时,您可能需要先将其转换为可观察对象.
You never want to return a Subject instance to the calling context. Doing so would be somewhat akin to returning a Deferred object rather than a promise; and, it would leave the Subject open to unanticipated and corrupting usage. As such, when exposing a Subject, you'll probably want to convert it to an Observable first.
要执行此操作,我们可以使用 Rx.Observable.prototype.asObservable()
实例方法.
To get this working we can use the Rx.Observable.prototype.asObservable()
instance method.
主题本身是热/可共享的,它充当源Observable和许多观察者之间的桥梁/代理人,使多个观察者可以共享相同的Observable执行.
The subject itself is hot/sharable and it acts as a bridge/proxy between the source Observable and many observers, making it possible for multiple observers to share the same Observable execution.
此吸气剂/工厂功能模式是否有明显优势?
不会,一点也不,因为您正在创建一个新的以该Subject为源的Observable,以将其隐藏在使用Observable的代码中.
Is there a non-obvious advantage to this getter/factory function pattern?
Nope, not at all since you are Creating a new Observable with this Subject as the source to conceal it from code that uses the Observable.
这篇关于为什么要使用RxJS .asObservable()getter/factory函数模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!