在 ngrx 选择器中使用管道

use pipe in ngrx selector

我想要一个 memoized selector,它在投影函数中包含一个 rxjs 转换。像这样尝试获取 FunctionProps[] 数组的每组 FunctionProps.CategoryFunctionProps 数组,该数组由 select 记忆 select 或 selectFunctionProperties 编辑:

   export const selectGroupedFunctionProperties = createSelector(
    selectFunctionProperties,
    (f) => from(f).pipe(
        groupBy((v: FunctionProps) => v.Category),
        mergeMap(group => group.pipe(toArray())),
        toArray()
    )
);

如您所见,我使用了 'from' 从源 FunctionProps[] 数组中获取可通过管道传输的可观察对象。这当然不是一个好主意,因为 selectGroupedFunctionProperties select 或现在是消费组件中可观察的可观察对象。

有没有一种方法可以将管道存储在内存中 select 或者不使用组件 this.store.select(selectFunctionProperties).pipe(...) 中的管道?

我希望能够组合 selectors 并将 rxjs 代码排除在组件之外。

否则在 selector 中使用管道是否是一个有效的模式 (所以我无法避免在 select 或 纯函数 中为带有类型脚本的数组实现 groupBy)?

编辑 我更改了 select 或者直接使用管道运算符:

export const selectGroupedFunctionProperties = pipe(
    select(selectFunctionProperties),
    concatMap(functions => functions),
    groupBy(v => v.Category),
    mergeMap(group => group.pipe(toArray())),
    toArray(),
);

在组件中,我收到带有管道而不是 select:

的 Functionprops[][] 数组
groupedFunctions$ = this.store.pipe(selectGroupedFunctionProperties);

您可以选择自行导出管道(无需 createSelector):

export const selectGroupedFunctionProperties = pipe(
  select(selectFunctionProperties),
  groupBy((v: FunctionProps) => v.Category),
  mergeMap(group => group.pipe(toArray()))
  // I'm not quite sure, but I don't think you need the 'toArray'-operator
);

为此也查看文档:https://ngrx.io/guide/store/selectors#breaking-down-the-basics