RXJS可观察的发射顺序

发布于 2025-01-17 11:36:35 字数 319 浏览 4 评论 0原文

例如,我如何订阅三个可观察量,并在其中一个发出新值时发出取决于它们的顺序,例如 forkJoin 但发出,但它们的顺序很重要,所以如果 ob3 首先发出,我希望 obv1 的值为 null,也为 obv2 并且只有发出的 obv3 才有价值,

例如

forkJoin(obs1,obs2,ob3)
.subscribe([ob1v,ob2v,ob3v]=>{

 if (obv1v){ 'do somthing'}
 if (obv2v){ 'do somthing'}
 if (obv3v){ 'do somthing'}
})

谢谢

how can i subscribe for example three observables, and to emit when one of them emit new value depend on the order they are, like forkJoin but emit but the order of them is important so if ob3 emit first, i want the value of obv1 to be null, also obv2
and only obv3 that was emitted will have value

for example

forkJoin(obs1,obs2,ob3)
.subscribe([ob1v,ob2v,ob3v]=>{

 if (obv1v){ 'do somthing'}
 if (obv2v){ 'do somthing'}
 if (obv3v){ 'do somthing'}
})

thanks

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

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

发布评论

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

评论(2

芸娘子的小脾气 2025-01-24 11:36:36

也许带有初始值的 combineLatest 适合您。每当任何源发出时,combineLatest 都会发出每个可观察源的最新值。但是,在所有源都发出至少一个值之前,它不会第一次发出。

我们可以使用 startWith 通过提供初始值来克服这个问题每个来源。

combineLatest([
  obs1.pipe(startWith(null)),
  obs2.pipe(startWith(null)),
  obs3.pipe(startWith(null)),
])
  .pipe(
    skip(1) // prevent emitting initial [null, null, null] value
  )
  .subscribe(([v1, v2, v3]) => {
    // do something here
  });

您可以在此 StackBlitz 中查看输出。

Maybe combineLatest with an initial value would work for you. combineLatest will emit the latest value from each source observable whenever any of its sources emit. However, it doesn't emit for the first time until all sources have emitted at least one value.

We can use startWith to overcome this by providing an initial value for each source.

combineLatest([
  obs1.pipe(startWith(null)),
  obs2.pipe(startWith(null)),
  obs3.pipe(startWith(null)),
])
  .pipe(
    skip(1) // prevent emitting initial [null, null, null] value
  )
  .subscribe(([v1, v2, v3]) => {
    // do something here
  });

You can see the output in this StackBlitz.

‖放下 2025-01-24 11:36:36

看来你想对​​每个可观察到的东西做不同的事情。也许你不应该把它们分组?如果你想对它们进行分组并对每个它们执行不同的副作用,你可以执行类似于 BizzyBob anwer 的操作,但不要在订阅中使用 if 语句,而是使用 tap() 运算符。像这样的事情:


combineLatest([
  obs1.pipe(tap(() =>  'do somthing'),
  obs2.pipe(tap(() =>  'do somthing')),
  obs3.pipe(tap(() =>  'do somthing')),
])
  .subscribe(([v1, v2, v3]) => {
  });

好的做法是不要使用订阅方法,而是将此流设置为组件中的某个属性,然后在模板中使用异步管道。

It seems that you want to do different thing for every observable. Maybe you shouldn't gorup them? If you want to group them and do different side effect for every one of them you can do something similar to BizzyBob anwer but instead of having if statements in subscribe use tap() operator for every stream. Something like this:


combineLatest([
  obs1.pipe(tap(() =>  'do somthing'),
  obs2.pipe(tap(() =>  'do somthing')),
  obs3.pipe(tap(() =>  'do somthing')),
])
  .subscribe(([v1, v2, v3]) => {
  });

Good practise is not to use subscribe method but instead set this stream to some property in component and than use async pipe in the template.

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