ngrx - createSelector vs Observable.combineLatest

Kes*_*vid 5 observable redux ngrx angular combinelatest

我刚刚遇到了自定义选择器,@ngrx我不能对这个功能感到惊讶.

继其使用情况booksselectedUser,我不能给一个真正的好理由使用自定义选择,例如:

export const selectVisibleBooks = createSelector(selectUser, selectAllBooks, (selectedUser: User, allBooks: Books[]) => {
    return allBooks.filter((book: Book) => book.userId === selectedUser.id);
});
Run Code Online (Sandbox Code Playgroud)

而不是像:

export const selectVisibleBooks = Observable.combineLatest(selectUser, selectAllBooks, (selectedUser: User, allBooks: Books[]) => {
    return allBooks.filter((book: Book) => book.userId === selectedUser.id);
});
Run Code Online (Sandbox Code Playgroud)

我试图说服自己的记忆化createSelector是关键的部分,但据我了解,它不能执行这些性能提升到非原始值,因此它不会真正节省非原始切片任何计算,通过使用Rx' s distinctUntilChanged操作员combineLatest可以解决.

那么我错过了什么,我为什么要使用@ngrx/selector

提前感谢任何见解.

byg*_*ace 6

也许它不仅仅是记忆,但我没有在源代码中看到任何突出的东西。文档中宣传的所有内容都是记忆化和一种重置它的方法,您基本上也可以使用不同的运算符来完成。我会说使用它的原因是它很方便。至少在简单的情况下,这比将不同的运算符绑定到combineLatest.

另一个好处是它允许您集中与状态内部结构相关的逻辑。store.select(x => foo.bar.baz)您可以为它创建一个选择器并执行 ,而不是到处做store.select(selectBaz)。您可以将选择器组合到。通过这种方式,您应该只需要设置逻辑以在一个地方遍历状态树。如果您必须更改状态的结构,这将非常有用,因为您只需在一个地方进行更改,而无需查找每个选择器。不过,每个人可能都不喜欢添加更多样板。但是作为必须对状态进行重大重构的人,我只使用选择器。

createSelector是非常基本的,所以你只能将它用于基本类型的操作。在您检索只需要过滤子集的对象列表的情况下,它是不够的。下面是一个例子:

const selectParentVmById = (id: string) => createSelector<RootState, Parent, Child[], ParentVm>(
    selectParentById(id),
    selectChildren(),
    (parent: Parent, children: Child[]) => (<ParentVm>{
        ...parent,
        children: children.filter(child => parent.children.includes(child.id))
    })
);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,选择器selectParentVmById将在selectChildren()发出不同的数组时发出,如果其中的任何元素发生更改,就会发生这种情况。如果更改的元素是父级的子级之一,这很好。如果不是,那么你会得到不必要的流失,因为记忆是在整个列表上完成的,而不是过滤后的列表(或者更确切地说是其中的元素)。我有很多这样的场景,并且已经开始只createSelector用于简单的选择器,并将它们与combineLatest我自己的记忆结合起来。

这不是一般不使用它的理由,您只需要知道它的局限性。

额外学分

你的问题不是关于这个,但自从我提出这个问题后,我想我会给出完整的解决方案。我开始使用一个名为的自定义运算符distinctElements(),它的作用类似于distinctUntilChanged()但适用于列表中的元素而不是列表本身。

这是运算符:

import { Observable } from 'rxjs/Observable';
import { startWith, pairwise, filter, map } from 'rxjs/operators';

export const distinctElements = () => <T extends Array<V>, V>(source: Observable<T>) => {
    return source.pipe(
        startWith(<T>null),
        pairwise(),
        filter(([a, b]) => a == null || a.length !== b.length || a.some(x => !b.includes(x))),
        map(([a, b]) => b)
    )
};
Run Code Online (Sandbox Code Playgroud)

以下是重构以使用它的上述代码:

const selectParentVmById = (store: Store<RootState>, id: string): ParentVm => {
    return store.select(selectParentById(id)).pipe(
        distinctUntilChanged(),
        switchMap((parent) => store.select(selectChildren()).pipe(
            map((children) => children.filter(child => parent.children.includes(child.id))),
            distinctElements(),
            map((children) => <ParentVm> { ...parent, children })
        ))
    );
}
Run Code Online (Sandbox Code Playgroud)

需要更多的代码,但它减少了浪费的工作。您可以shareReplay(1)根据您的情况添加一个。