为什么使用 RxJS .asObservable() getter/factory 函数模式? [英] Why use the RxJS .asObservable() getter/factory function pattern?

查看:19
本文介绍了为什么使用 RxJS .asObservable() getter/factory 函数模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 RxJS 的许多代码库中,我似乎遇到了通过 getter 或普通 Observables 作为 Observables 公开私有 Subjects 的模式代码>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?

asObservable() 包裹在 getter/factory 函数中

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.此外,创建的 Observablehot 并且可以多次订阅.为什么要创建多个 Observable 的匿名实例,(每个访问/订阅一个),而不是只有一个 Observable,在所有观察者订阅的一个类/服务中访问?
  • 这种getter/factory function 模式有什么不明显的优势吗?
  • 可能是因为垃圾收集或测试/模拟的优势?
  • My undestanding is that .asObservable() creates a new Observable every time that subscribes to the Subject. Also the created Observable is hot and can be subscribed multiple times. Why would one create multiple anonymous instances of Observable, (one for each access/subscription), instead of having just one Observable, 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 对象而不是一个承诺;并且,它会使主题对意外和破坏性的使用开放.因此,在公开一个 Subject 时,您可能希望先将其转换为 Observable.

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.

这种 getter/factory 函数模式有什么不明显的优势吗?
不,完全不是,因为您正在创建一个新的 Observable,以该 Subject 作为源,以将其从使用 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()?

这篇关于为什么使用 RxJS .asObservable() getter/factory 函数模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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