ngRx 官方示例分析 - 3. reducers
上一篇:ngRx 官方示例分析 - 2. Action 管理
这里我们讨论 reducer.
如果你注意的话,会看到在不同的 Action 定义文件中,导出的 Action 类型名称都是 Actions ,在导入的时候,同时导入同名的类型就是问题了。这里首先使用了 import as 语法进行重命名。
import * as book from '../actions/book';
import * as collection from '../actions/collection';
这样我们就可以区分来自不同定义文件的 Actions 了。
对于每个 reducer 来说,状态虽然一直在变,但是所管理的状态的基本的结构是不变的,我们通过接口进行约束。
比如 Book,我们定义如下状态的接口。注意这个接口的名称没有 Book,是统一的 State,在导入这个定义的时候,使用 import as 语法进行重命名。
export interface State {
ids: string[];
entities: { [id: string]: Book };
selectedBookId: string | null;
};
Book 的初始状态当然需要实现这个接口的。
export const initialState: State = {
ids: [],
entities: {},
selectedBookId: null,
};
辅助函数
在 Reducer 中提供了辅助函数,以便于数据访问。
/**
* Because the data structure is defined within the reducer it is optimal to
* locate our selector functions at this level. If store is to be thought of
* as a database, and reducers the tables, selectors can be considered the
* queries into said database. Remember to keep your selectors small and
* focused so they can be combined and composed to fit each particular
* use-case.
*/ export const getEntities = (state: State) => state.entities; export const getIds = (state: State) => state.ids; export const getSelectedId = (state: State) => state.selectedBookId; export const getSelected = createSelector(getEntities, getSelectedId, (entities, selectedId) => {
return entities[selectedId];
}); export const getAll = createSelector(getEntities, getIds, (entities, ids) => {
return ids.map(id => entities[id]);
});
这个 createSelector 函数来自 reselect 。
Simple “selector” library for Redux inspired by getters in NuclearJS, subscriptions in re-frame and this proposal from speedskater.
- Selectors can compute derived data, allowing Redux to store the minimal possible state.
- Selectors are efficient. A selector is not recomputed unless one of its arguments change.
- Selectors are composable. They can be used as input to other selectors.
需要注意的是,Book 有 4 个 Action 的定义,
- Search
- SearchComplete
- Load
- Select
在 Book 的处理中并没有 SearchAction 的处理。它需要我们发出一个异步的请求,在 @ngrx 中,它需要 @ngrx/effect 来处理,
源代码
下面是 Book 的 reducer 的全部代码。/app/reducers/books.ts
import { createSelector } from 'reselect';
import { Book } from '../models/book';
import * as book from '../actions/book';
import * as collection from '../actions/collection';
export interface State {
ids: string[];
entities: { [id: string]: Book };
selectedBookId: string | null;
};
export const initialState: State = {
ids: [],
entities: {},
selectedBookId: null,
};
export function reducer(state = initialState, action: book.Actions | collection.Actions): State {
switch (action.type) {
case book.ActionTypes.SEARCH_COMPLETE:
case collection.ActionTypes.LOAD_SUCCESS: {
const books = action.payload;
const newBooks = books.filter(book => !state.entities[book.id]);
const newBookIds = newBooks.map(book => book.id);
const newBookEntities = newBooks.reduce((entities: { [id: string]: Book }, book: Book) => {
return Object.assign(entities, {
[book.id]: book
});
}, {});
return {
ids: [ ...state.ids, ...newBookIds ],
entities: Object.assign({}, state.entities, newBookEntities),
selectedBookId: state.selectedBookId
};
}
case book.ActionTypes.LOAD: {
const book = action.payload;
if (state.ids.indexOf(book.id) > -1) {
return state;
}
return {
ids: [ ...state.ids, book.id ],
entities: Object.assign({}, state.entities, {
[book.id]: book
}),
selectedBookId: state.selectedBookId
};
}
case book.ActionTypes.SELECT: {
return {
ids: state.ids,
entities: state.entities,
selectedBookId: action.payload
};
}
default: {
return state;
}
}
}
/**
* Because the data structure is defined within the reducer it is optimal to
* locate our selector functions at this level. If store is to be thought of
* as a database, and reducers the tables, selectors can be considered the
* queries into said database. Remember to keep your selectors small and
* focused so they can be combined and composed to fit each particular
* use-case.
*/
export const getEntities = (state: State) => state.entities;
export const getIds = (state: State) => state.ids;
export const getSelectedId = (state: State) => state.selectedBookId;
export const getSelected = createSelector(getEntities, getSelectedId, (entities, selectedId) => {
return entities[selectedId];
});
export const getAll = createSelector(getEntities, getIds, (entities, ids) => {
return ids.map(id => entities[id]);
});
import * as collection from '../actions/collection';
export interface State {
loaded: boolean;
loading: boolean;
ids: string[];
};
const initialState: State = {
loaded: false,
loading: false,
ids: []
};
export function reducer(state = initialState, action: collection.Actions): State {
switch (action.type) {
case collection.ActionTypes.LOAD: {
return Object.assign({}, state, {
loading: true
});
}
case collection.ActionTypes.LOAD_SUCCESS: {
const books = action.payload;
return {
loaded: true,
loading: false,
ids: books.map(book => book.id)
};
}
case collection.ActionTypes.ADD_BOOK_SUCCESS:
case collection.ActionTypes.REMOVE_BOOK_FAIL: {
const book = action.payload;
if (state.ids.indexOf(book.id) > -1) {
return state;
}
return Object.assign({}, state, {
ids: [ ...state.ids, book.id ]
});
}
case collection.ActionTypes.REMOVE_BOOK_SUCCESS:
case collection.ActionTypes.ADD_BOOK_FAIL: {
const book = action.payload;
return Object.assign({}, state, {
ids: state.ids.filter(id => id !== book.id)
});
}
default: {
return state;
}
}
}
export const getLoaded = (state: State) => state.loaded;
export const getLoading = (state: State) => state.loading;
export const getIds = (state: State) => state.ids;
import * as layout from '../actions/layout';
export interface State {
showSidenav: boolean;
}
const initialState: State = {
showSidenav: false,
};
export function reducer(state = initialState, action: layout.Actions): State {
switch (action.type) {
case layout.ActionTypes.CLOSE_SIDENAV:
return {
showSidenav: false
};
case layout.ActionTypes.OPEN_SIDENAV:
return {
showSidenav: true
};
default:
return state;
}
}
export const getShowSidenav = (state: State) => state.showSidenav;
/app/reducers/search.ts
import * as book from '../actions/book';
export interface State {
ids: string[];
loading: boolean;
query: string;
};
const initialState: State = {
ids: [],
loading: false,
query: ''
};
export function reducer(state = initialState, action: book.Actions): State {
switch (action.type) {
case book.ActionTypes.SEARCH: {
const query = action.payload;
if (query === '') {
return {
ids: [],
loading: false,
query
};
}
return Object.assign({}, state, {
query,
loading: true
});
}
case book.ActionTypes.SEARCH_COMPLETE: {
const books = action.payload;
return {
ids: books.map(book => book.id),
loading: false,
query: state.query
};
}
default: {
return state;
}
}
}
export const getIds = (state: State) => state.ids;
export const getQuery = (state: State) => state.query;
export const getLoading = (state: State) => state.loading;
管理 Recuders
组合器
compose 函数用来将一组函数组合成单个单个函数。
/**
* The compose function is one of our most handy tools. In basic terms, you give
* it any number of functions and it returns a function. This new function
* takes a value and chains it through every composed function, returning
* the output.
*
* More: https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch5.html*/
import { compose } from '@ngrx/core/compose';
ngrx-store-freeze
@ngrx/store meta reducer that prevents state from being mutated. When mutation occurs, an exception will be thrown. This is useful during development mode to ensure that no part of the app accidentally mutates the state. Ported from redux-freeze
/**
* storeFreeze prevents state from being mutated. When mutation occurs, an
* exception will be thrown. This is useful during development mode to
* ensure that none of the reducers accidentally mutates the state.
*/
import { storeFreeze } from 'ngrx-store-freeze';
combineReducers
随着应用变得复杂,需要对 reducer 函数 进行拆分,拆分后的每一块独立负责管理 state 的一部分。
combineReducers 辅助函数的作用是,把一个由多个不同 reducer 函数作为 value 的 object,合并成一个最终的 reducer 函数,然后就可以对这个 reducer 调用 createStore。
合并后的 reducer 可以调用各个子 reducer,并把它们的结果合并成一个 state 对象。state 对象的结构由传入的多个 reducer 的 key 决定。
通过为传入对象的 reducer 命名不同来控制 state key 的命名。例如,你可以调用 combineReducers({todos: myTodosReducer, counter: myCounterReducer }) 将 state 结构变为 { todos, counter }。
通常的做法是命名 reducer,然后 state 再去分割那些信息,因此你可以使用 ES6 的简写方法:combineReducers({ counter, todos })。这与 combineReducers({ counter: counter, todos: todos })一样。
http://www.redux.org.cn/docs/api/combineReducers.html
/**
* combineReducers is another useful metareducer that takes a map of reducer
* functions and creates a new reducer that stores the gathers the values
* of each reducer and stores them using the reducer's key. Think of it
* almost like a database, where every reducer is a table in the db.
*
* More: https://egghead.io/lessons/javascript-redux-implementing-combinereducers-from-scratch*/
import { combineReducers } from '@ngrx/store';
组合 Recuders
这是函数式编程中的方法,为了方便,被放到了 Redux 里。 当需要把多个 store 增强器 依次执行的时候,需要用到它。
http://www.redux.org.cn/docs/api/compose.html
将各个状态和 reducer 组合为单个的 reducer。
/**
* As mentioned, we treat each reducer like a table in a database. This means
* our top level state interface is just a map of keys to inner state types.
*/
export interface State {
search: fromSearch.State;
books: fromBooks.State;
collection: fromCollection.State;
layout: fromLayout.State;
router: fromRouter.RouterState;
} /**
* Because metareducers take a reducer function and return a new reducer,
* we can use our compose helper to chain them together. Here we are
* using combineReducers to make our top level reducer, and then
* wrapping that in storeLogger. Remember that compose applies
* the result from right to left.
*/
const reducers = {
search: fromSearch.reducer,
books: fromBooks.reducer,
collection: fromCollection.reducer,
layout: fromLayout.reducer,
router: fromRouter.routerReducer,
}; const developmentReducer: ActionReducer<State> = compose(storeFreeze, combineReducers)(reducers);
const productionReducer: ActionReducer<State> = combineReducers(reducers);
在 compose 的时候,已经传递了当前的 reducers .
最后导出了 reducer 函数
export function reducer(state: any, action: any) {
if (environment.production) {
return productionReducer(state, action);
} else {
return developmentReducer(state, action);
}
}
注册 reducer
在 /app/app.module.ts 中,注册到 store 中。注意 './reducers' 实际上导入的是 /app/reducers/index.ts。
import { reducer } from './reducers';
/**
* StoreModule.provideStore is imported once in the root module, accepting a reducer
* function or object map of reducer functions. If passed an object of
* reducers, combineReducers will be run creating your application
* meta-reducer. This returns all providers for an @ngrx/store
* based application.
*/
StoreModule.provideStore(reducer),
一些辅助函数
获取当前图书列表的辅助函数。
export const getBooksState = (state: State) => state.books;
源码
import { createSelector } from 'reselect';
import { ActionReducer } from '@ngrx/store';
import * as fromRouter from '@ngrx/router-store';
import { environment } from '../../environments/environment';
/**
* The compose function is one of our most handy tools. In basic terms, you give
* it any number of functions and it returns a function. This new function
* takes a value and chains it through every composed function, returning
* the output.
*
* More: https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch5.html
*/
import { compose } from '@ngrx/core/compose';
/**
* storeFreeze prevents state from being mutated. When mutation occurs, an
* exception will be thrown. This is useful during development mode to
* ensure that none of the reducers accidentally mutates the state.
*/
import { storeFreeze } from 'ngrx-store-freeze';
/**
* combineReducers is another useful metareducer that takes a map of reducer
* functions and creates a new reducer that stores the gathers the values
* of each reducer and stores them using the reducer's key. Think of it
* almost like a database, where every reducer is a table in the db.
*
* More: https://egghead.io/lessons/javascript-redux-implementing-combinereducers-from-scratch
*/
import { combineReducers } from '@ngrx/store';
/**
* Every reducer module's default export is the reducer function itself. In
* addition, each module should export a type or interface that describes
* the state of the reducer plus any selector functions. The `* as`
* notation packages up all of the exports into a single object.
*/
import * as fromSearch from './search';
import * as fromBooks from './books';
import * as fromCollection from './collection';
import * as fromLayout from './layout';
/**
* As mentioned, we treat each reducer like a table in a database. This means
* our top level state interface is just a map of keys to inner state types.
*/
export interface State {
search: fromSearch.State;
books: fromBooks.State;
collection: fromCollection.State;
layout: fromLayout.State;
router: fromRouter.RouterState;
}
/**
* Because metareducers take a reducer function and return a new reducer,
* we can use our compose helper to chain them together. Here we are
* using combineReducers to make our top level reducer, and then
* wrapping that in storeLogger. Remember that compose applies
* the result from right to left.
*/
const reducers = {
search: fromSearch.reducer,
books: fromBooks.reducer,
collection: fromCollection.reducer,
layout: fromLayout.reducer,
router: fromRouter.routerReducer,
};
const developmentReducer: ActionReducer<State> = compose(storeFreeze, combineReducers)(reducers);
const productionReducer: ActionReducer<State> = combineReducers(reducers);
export function reducer(state: any, action: any) {
if (environment.production) {
return productionReducer(state, action);
} else {
return developmentReducer(state, action);
}
}
/**
* A selector function is a map function factory. We pass it parameters and it
* returns a function that maps from the larger state tree into a smaller
* piece of state. This selector simply selects the `books` state.
*
* Selectors are used with the `select` operator.
*
* ```ts
* class MyComponent {
* constructor(state$: Observable<State>) {
* this.booksState$ = state$.select(getBooksState);
* }
* }
* ```
*/
export const getBooksState = (state: State) => state.books;
/**
* Every reducer module exports selector functions, however child reducers
* have no knowledge of the overall state tree. To make them useable, we
* need to make new selectors that wrap them.
*
* The createSelector function from the reselect library creates
* very efficient selectors that are memoized and only recompute when arguments change.
* The created selectors can also be composed together to select different
* pieces of state.
*/
export const getBookEntities = createSelector(getBooksState, fromBooks.getEntities);
export const getBookIds = createSelector(getBooksState, fromBooks.getIds);
export const getSelectedBookId = createSelector(getBooksState, fromBooks.getSelectedId);
export const getSelectedBook = createSelector(getBooksState, fromBooks.getSelected);
/**
* Just like with the books selectors, we also have to compose the search
* reducer's and collection reducer's selectors.
*/
export const getSearchState = (state: State) => state.search;
export const getSearchBookIds = createSelector(getSearchState, fromSearch.getIds);
export const getSearchQuery = createSelector(getSearchState, fromSearch.getQuery);
export const getSearchLoading = createSelector(getSearchState, fromSearch.getLoading);
/**
* Some selector functions create joins across parts of state. This selector
* composes the search result IDs to return an array of books in the store.
*/
export const getSearchResults = createSelector(getBookEntities, getSearchBookIds, (books, searchIds) => {
return searchIds.map(id => books[id]);
});
export const getCollectionState = (state: State) => state.collection;
export const getCollectionLoaded = createSelector(getCollectionState, fromCollection.getLoaded);
export const getCollectionLoading = createSelector(getCollectionState, fromCollection.getLoading);
export const getCollectionBookIds = createSelector(getCollectionState, fromCollection.getIds);
export const getBookCollection = createSelector(getBookEntities, getCollectionBookIds, (entities, ids) => {
return ids.map(id => entities[id]);
});
export const isSelectedBookInCollection = createSelector(getCollectionBookIds, getSelectedBookId, (ids, selected) => {
return ids.indexOf(selected) > -1;
});
/**
* Layout Reducers
*/
export const getLayoutState = (state: State) => state.layout;
export const getShowSidenav = createSelector(getLayoutState, fromLayout.getShowSidenav);
总结
通过 Reducer 建立对 store 状态的维护,示例中还提供了一些辅助函数来帮助简化数据访问。
参考资源:
ngRx 官方示例分析 - 3. reducers的更多相关文章
- ngRx 官方示例分析 - 2. Action 管理
我们从 Action 名称开始. 解决 Action 名称冲突问题 在 ngRx 中,不同的 Action 需要一个 Action Type 进行区分,一般来说,这个 Action Type 是一个字 ...
- ngRx 官方示例分析 - 1. 介绍
ngRx 的官方示例演示了在具体的场景中,如何使用 ngRx 管理应用的状态. 示例介绍 示例允许用户通过查询 google 的 book API 来查询图书,并保存自己的精选书籍列表. 菜单有两 ...
- ngRx 官方示例分析 - 4.pages
Page 中通过构造函数注入 Store,基于 Store 进行数据操作. 注意 Component 使用了 changeDetection: ChangeDetectionStrategy.OnPu ...
- ngRx 官方示例分析 - 6 - Effect
@ngrx/effect 前面我们提到,在 Book 的 reducer 中,并没有 Search 这个 Action 的处理,由于它需要发出一个异步的请求,等到请求返回前端,我们需要根据返回的结果来 ...
- ngRx 官方示例分析 - 5. components
组件通过标准的 Input 和 Output 进行操作,并不直接访问 store. /app/components/book-authors.ts import { Component, Input ...
- RocketMQ源码分析之从官方示例窥探:RocketMQ事务消息实现基本思想
摘要: RocketMQ源码分析之从官方示例窥探RocketMQ事务消息实现基本思想. 在阅读本文前,若您对RocketMQ技术感兴趣,请加入RocketMQ技术交流群 RocketMQ4.3.0版本 ...
- Halcon斑点分析官方示例讲解
官方示例中有许多很好的例子可以帮助大家理解和学习Halcon,下面举几个经典的斑点分析例子讲解一下 Crystals 图中显示了在高层大气中采集到的晶体样本的图像.任务是分析对象以确定特定形状的频率. ...
- DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版------------- ...
- DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...
随机推荐
- APP开发选择什么框架好? 请看这里!
背景 App的开发一般都需要满足Android和iOS两个系统环境,也就意味着一个App需要定制两套实现方案,造成开发成本和维护成本都很高.为了解决这个问题,最好的办法就是实现一套代码跨端运行,所以H ...
- MySQL 数据类型和约束(外键是重点🙄)
数据类型 1. 数字(默认都是由符号,宽度表示的是显示宽度,与存储无关).tinyint 括号里指定宽度 七位2进制数最大数就是2**7 -1=127 最小是-128 验证: create tabel ...
- 面向对象 初级版 (Preview) 未完
概述: 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数里,日后使用无需重复编写,直接调用韩顺即可. 面向对象: 对函数进行分类和封装,让开发'更快更强' 面向对象和面向过程的通 ...
- ArcGis连接oracle失败:ORA-6413:连接未打开
问题: 通过ARCMap 添加Oracle数据库连接时提示,ORA-6413:连接未打开. 运行环境: ArcGis 10.2 Oracle 10g 解决方法: 通过上网查找解决方法,网友说" ...
- Java学习笔记14---this作为返回值时返回的是什么
有时会遇到this作为返回值的情况,那么此时返回的到底是什么呢? 返回的是调用this所处方法的那个对象的引用,读起来有点绕口哈,有没有想起小学语文分析句子成份的试题,哈哈. 一点点分析的话,主干是& ...
- OwinHost.exe用法
简介 OwinHost.exe是微软提供的自宿主host,如果自己不想写owin的host,可以用这个. 官方对OwinHost的描述为:Provides a stand-alone executab ...
- 设计模式学习(四): 1.简单工厂 (附C#实现)
New 这是一个典型的情况, 我们需要在运行时来实例化一些具体的类. 在需要修改或者扩展的时候我们就需要改这段代码. 一个程序中可能会多次出现类似的代码, 这使得维护和更新非常困难而且容易出错. 通过 ...
- [转载]常见slave 延迟原因以及解决方法
一 序言在运维线上M-M 架构的MySQL数据库时,接收的比较多关于主备延时的报警: 点击(此处)折叠或打开 check_ins_slave_lag (err_cnt:1)critical-slav ...
- JAVA实现跳一跳辅助程序之虎啸龙吟
前序: 今天有幸,看到2位博主的文章,在此表示感谢.自己也动手实现了一下. 实现原理 请参考博主 https://www.cnblogs.com/dongkuo/p/8285162.html 另感谢博 ...
- String常用的方法
l String: 字符串类,字符串是常量:它们的值在创建之后不能更改 l 方法 boolean equals(Object obj) 判断两个字符串中的内容是否相同 boolean equalsIg ...