为什么不是所有的流都会在 combineLatest 中发出值?

huangapple go评论64阅读模式
英文:

Why not all streams are emit value in combineLatest?

问题

这里有一个combineLatest操作符:

this.result$ = combineLatest([
      this.data$.pipe(tap(() => console.log('data'))),
      this.sourceWithImage$.pipe(tap(() => console.log('image'))),
      this.sourceWithFields$.pipe(tap(() => console.log('fields'))),
    ]).pipe(
      map(([data, image, fields]) => {
        return { data, image, fields };
      })
    );

为什么不是所有的流都发出值?只有data发出值。在这个链接中有一个 Stackblitz 示例。

我已经尝试使用startWith来发出默认值,但我不需要它。

英文:

There is a combineLatest operator:

this.result$ = combineLatest([
      this.data$.pipe(tap(() => console.log('data'))),
      this.sourceWithImage$.pipe(tap(() => console.log('image'))),
      this.sourceWithFields$.pipe(tap(() => console.log('fields'))),
    ]).pipe(
      map(([data, image, fields]) => {
        return { data, image, fields };
      })
    );

Whydo not all streams emit values? Only data. Stackblitz exmaple by link

I have tried to emit default values using startWith, but I dont need it

答案1

得分: 0

以下是代码的中文翻译部分:

this.sourceWithImage$this.sourceWithFields$两者中,您在switchMap中都有一个空的of()调用。of()操作符创建一个可观察对象,会发出传递给它的参数。由于您没有传递任何参数,它会立即创建一个可观察对象,而不发出任何值,这导致随后的delay()map()操作没有效果。

要解决这个问题,您可以修改of()调用以传递一个值,如下所示:

this.sourceWithImage$ = this.data$.pipe(
  switchMap(() =>
    of(null).pipe( // 传递一个值给of()
      delay(5000),
      map(() => new Date().valueOf())
    )
  )
);

this.sourceWithFields$ = this.data$.pipe(
  switchMap(() =>
    of(null).pipe( // 传递一个值给of()
      delay(3000),
      map(() => new Date().valueOf())
    )
  )
);

此处更新的Stackblitz

英文:

The issue is with the rest of the code you didn't post:

    this.sourceWithImage$ = this.data$.pipe(
      switchMap(() =>
        of().pipe(
          delay(5000),
          map(() => new Date().valueOf())
        )
      )
    );

    this.sourceWithFields$ = this.data$.pipe(
      switchMap(() =>
        of().pipe(
          delay(3000),
          map(() => new Date().valueOf())
        )
      )
    );

In both this.sourceWithImage$ and this.sourceWithFields$, you have an empty of() call in the switchMap. The of() operator creates an observable that emits the arguments passed to it. Since you didn't pass any arguments, it creates an observable that completes immediately without emitting any values. This causes the subsequent delay() and map() operators to have no effect.

To fix this issue, you can modify the of() calls to pass a value like this:

this.sourceWithImage$ = this.data$.pipe(
  switchMap(() =>
    of(null).pipe( // Pass a value to of()
      delay(5000),
      map(() => new Date().valueOf())
    )
  )
);

this.sourceWithFields$ = this.data$.pipe(
  switchMap(() =>
    of(null).pipe( // Pass a value to of()
      delay(3000),
      map(() => new Date().valueOf())
    )
  )
);

Updated Stackblitz here.

huangapple
  • 本文由 发表于 2023年5月7日 00:46:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定