是否有理由反对getLatestFrom OperatorFunction来实现ngrx效果?

问题描述

要以ngrx效果访问状态,我们可以像这样使用withLatestFrom(摘自文档https://ngrx.io/guide/effects):

this.actions$.pipe(
        ofType(CollectionApiActions.addBookSuccess),concatMap(action => of(action).pipe(
          withLatestFrom(this.store.pipe(select(fromBooks.getCollectionBookIds)))
        )),tap(([action,bookCollection]) => {
          if (bookCollection.length === 1) {
            window.alert('Congrats on adding your first book!');
          } else {
            window.alert('You have added book number ' + bookCollection.length);
          }
        })
      )

与我的应用程序中的许多效果一样,该效果实际上并没有从我们使用action获得的列表中访问withLatestFrom参数,而仅对bookCollection感兴趣。所以我想知道,为什么不简单地这样做呢?

function getLatestFrom<T,R>(observable: Observable<T>) {
    return concatMap((action: R) => of(action).pipe(
        withLatestFrom(observable),map(([,value]) => value)
    ))
}

并将上面的代码段替换为

this.actions$.pipe(
        ofType(CollectionApiActions.addBookSuccess),getLatestFrom(this.store.pipe(select(fromBooks.getCollectionBookIds))),tap(bookCollection => {
          if (bookCollection.length === 1) {
            window.alert('Congrats on adding your first book!');
          } else {
            window.alert('You have added book number ' + bookCollection.length);
          }
        })
      )

这对我来说看起来更干净,但是我在任何地方都找不到这样的东西,所以我想知道是否有理由反对这样做?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)