ngrx store.select 方法如何传递存储上下文?

How does ngrx store.select method pass the store context?

在这个ngrx example component的第27行,你会看到

this.books$ = store.select(fromRoot.getSearchResults);

选择器函数getSearchResults由其他选择器函数组成,innermost selector functionsgetSearchStategetBoosState

我的问题是

select 是在 @ngrx/core and is bound as a method of the Store.

中定义的 RxJS 运算符

Store 是一个可观察对象,它在接收到一个动作并通过其组合的化简器传递该动作后发出状态。所以 select 运算符接收状态作为 Store observable 发出的值。

如果您对状态本身的保存位置感到好奇,Store 使用 State observable as its source. And the State observable 是一个 RxJS BehaviorSubject

getSearchResults selector is composed using reselect. The createSelector 函数创建一个选择器,将其接收状态传递给 getBookEntitiesgetSearchBookIds 选择器 - 然后其结果为 combined/joined.

reselect 用于创建选择器,因为它的选择器是记忆化的。即选择器结果为cached/memoized,只有当输入发生变化时,选择器才重新运行。这可以显着提高效率,因为每次 store observable 发出状态时,选择器都会重新运行。

所以流程是:

  • State observable 发出状态。
  • State observable 是 Store 的来源,因此它也发出状态。
  • getSearchResults 用于 Store 上的 select 运算符。
  • getSearchResults 接收状态并将其传递给组合的 getBookEntitiesgetSearchBookIds 选择器。
  • getBookEntities 接收状态并使用 getBooksState 选择书籍状态,将其传递给 fromBooks.getEntities
  • getSearchBookIds 接收状态并使用 getSearchState 选择书籍状态,将其传递给 fromBooks.getEntities
  • 然后将书籍和搜索 ID 合并,结果就是选择器返回的结果。