简体   繁体   English

append 新的可观察量到可观察量数组?

[英]append new observables to array of observables?

I am trying to avoid the following:我试图避免以下情况:

switchMap(([action, state]) =>
  from(TodosDB.getAll()).pipe(
    map(
      (todos) => onQueryTodoDone({ items: todos }),
      catchError((err) => of(onQueryTodoFail({ error: err })))
    )
  )
),

to something more linear like like we do with combineLatestFrom in ngrx.到更线性的东西,就像我们在 ngrx 中使用 combineLatestFrom 一样。 So far I tried to do the below.到目前为止,我尝试执行以下操作。 But the promise does not seem to work.但是promise好像不行。

withLatestFrom(
  from(TodosDB.getAll())
),

and

withLatestFrom(
  from(TodosDB.getAll()).pipe(
    map((todos) => onQueryTodoDone({ items: todos }))
  )
)

Any ideas how to deal with this scenario without nesting pipe map in a switchMap?任何想法如何处理这种情况而无需在 switchMap 中嵌套 pipe map?

PS: this might be obvious to you but I don't know much and I looked up on the inte.net and found withLatestFrom but not sure what I am missing. PS:这对您来说可能很明显,但我知道的不多,我在 inte.net 上查找并找到了 withLatestFrom 但不确定我遗漏了什么。

EDIT: this is the best I got so far:编辑:这是迄今为止我得到的最好的:

switchMap(([action, state]) =>
  forkJoin([of(action), of(state), TodosDB.getAll()])
),
map(
  ([action, state, todos]) => onQueryTodoDone({ items: todos }),
  catchError((err) => of(onQueryTodoFail({ error: err })))
),

The above is better but I have no idea if that can cause issues later.以上更好,但我不知道这是否会在以后引起问题。 But hopefully I was able to communicate the idea.但希望我能够传达这个想法。 Which is append a promise result to the piped observable keep it's original structure这是 append 一个 promise 结果管道观察保持其原始结构

[Observable<Actions>,Observable<State>,Observable<FromPromise>]

Joining a few dots here and taking a bit of a guess I would say the problem stems from TodosDB.getAll returning a Promise .在这里加入几个点并进行一些猜测,我会说问题源于TodosDB.getAll返回Promise

With a promise based function you want to lazily evaluate it, as such a function is executed immediately when it is called, unlike an observable based function which requires a subscription.对于基于 promise 的 function,您希望延迟评估它,因此 function 在调用时会立即执行,这与需要订阅的基于可观察对象的 function 不同。

This is why the switchMap based solutions work, because the body of the switchMap is not evaluated until the source emits.这就是基于switchMap的解决方案起作用的原因,因为switchMap的主体在源发出之前不会被评估。

In your shortened version using withLatestFrom , there is no lazy evaluation, the getAll call is probably evaluated once and once only when the effect is set up.在您使用withLatestFrom的缩短版本中,没有延迟评估, getAll调用可能只在设置效果时评估一次。

You can use the defer operator to convert your promise based function to one that behaves more appropriately with the rxjs observable based operators.您可以使用defer运算符将基于 promise 的 function 转换为更适合基于 rxjs 可观察运算符的运算符。

withLatestFrom(defer(() => this.getAll())),  // use of defer
map(([[action, state], todos]) => [action, state, todos]),  // refine the shape of the data to your requirements
...

Stackblitz: https://stackblitz.com/edit/angular-ivy-qkwqyw?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html Stackblitz: https://stackblitz.com/edit/angular-ivy-qkwqyw?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html

Note: The concatLatestFrom ngrx operator also looks like it may be of use but I couldn't get it to work how I wanted.注意: concatLatestFrom ngrx 运算符看起来也可能有用,但我无法让它按我想要的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM