组合主题而无需等待每个主题

发布于 2025-02-13 03:10:57 字数 446 浏览 1 评论 0原文

我有4个主题在用户输入上散发值

this.combine$ = zip(
  this.filterInputStore.selectedCurrenciesHandler,
  this.filterInputStore.searchInputHandler,
  this.filterInputStore.selectedTypesHandler,
  this.filterInputStore.selectedPrivacyOptionsHandler
).pipe(tap(() => { doSomething() }));

我的目标是触发dosomething(),一旦其中一个发出值。

问题:所有RXJS运算符(例如zip或CombineLatest)都需要等到所有受试者发出。如果我想过滤列表并想要结果,这是非常不切实际的,即使我只使用一个过滤器。 有RXJS的最佳实践吗?

I Have 4 Subjects that emit values on user Input

this.combine$ = zip(
  this.filterInputStore.selectedCurrenciesHandler,
  this.filterInputStore.searchInputHandler,
  this.filterInputStore.selectedTypesHandler,
  this.filterInputStore.selectedPrivacyOptionsHandler
).pipe(tap(() => { doSomething() }));

My Goal is to trigger the doSomething(), as soon as one of them emits a value.

Problem: All rxjs operators like zip or combineLatest require to wait until all subjects in it emit. This is extremely impractical if say i want to filter a list and want results even if i only use one filter.
Is there a rxjs best practise for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独难免 2025-02-20 03:10:57

我相信合并race都可以使用。我更喜欢race,因为该方法表明您正在等待第一个可观察到的发射(并赢得比赛!)。另外,race只会触发一次,因此您的dosomething不会多次执行。

I believe both merge or race would work. I prefer race because the method indicate that you're waiting for the first observable to emit (and win the race!). Also, race will trigger only ONCE, so your doSomething won't get executed multiple times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文