redux-actions源码解读

一.什么是redux-actionsredux

   redux-actions是一个简化action和reducer建立的一个封装库,里面有5个js文件,ide

   createAction.js函数

   handleAction.js工具

   handleActions.jsspa

   index.js3d

   ownKeys.jscode

二.怎么使用?下面将从源码一一解释每一个文件的的用处对象

  1.createAction.jsblog

    从名字就能够看出,这是用来建立action的.其源码以下:继承

      

 1 /**
 2  * 参数不是function调用此函数
 3  */
 4 function identity(t) {
 5   return t;
 6 }
 7 
 8 /**
 9  * 建立action
10  * @param type  action的类型
11  * @param actionCreator 须要建立的action,函数
12  * @param metaCreator   action的属性
13  * @returns {Function}
14  */
15 export default function createAction(type, actionCreator, metaCreator) {
16     /**
17     * finalActionCreator最终建立的action,
18     * 判断传进来的参数是否是function,true返回这个函数,false调用identity函数
19     */
20   const finalActionCreator = typeof actionCreator === 'function'
21     ? actionCreator
22     : identity;
23   /**
24    * 返回一个匿名函数
25    */
26   return (...args) => {
27     /**
28      *建立的action,只有两个属性
29      */
30     const action = {
31       type,
32       payload: finalActionCreator(...args)
33     };
34     /**
35      * 若是给匿名函数传递参数的长度为1个,或者第一个参数元素的类型为Error,那么这么action的error属性为true
36      */
37     if (args.length === 1 && args[0] instanceof Error) {
38       // Handle FSA errors where the payload is an Error object. Set error.
39       action.error = true;
40     }
41     /**
42      * 传递到action里面的函数
43      */
44     if (typeof metaCreator === 'function') {
45       action.meta = metaCreator(...args);
46     }
47 
48     return action;
49   };
50 }
View Code

 

 

   2.handleAction.js

    操做action,源码以下:

    

 1 import { isError } from 'flux-standard-action';
 2 /**
 3  * 判断是否是function
 4  */
 5 function isFunction(val) {
 6   return typeof val === 'function';
 7 }
 8 /**
 9  * 处理action
10  * @param type action类型
11  * @param 全部的reducers
12  * @returns {Function}
13  */
14 export default function handleAction(type, reducers) {
15   return (state, action) => {
16     // If action type does not match, return previous state
17     if (action.type !== type) return state;
18 
19     const handlerKey = isError(action) ? 'throw' : 'next';
20 
21     // If function is passed instead of map, use as reducer
22     if (isFunction(reducers)) {
23       reducers.next = reducers.throw = reducers;
24     }
25 
26     // Otherwise, assume an action map was passed
27     const reducer = reducers[handlerKey];
28 
29     return isFunction(reducer)
30       ? reducer(state, action)
31       : state;
32   };
33 }
View Code

 

    reduce-reducers源码:

1 export default function reduceReducers(...reducers) {
2   return (previous, current) =>
3     reducers.reduce(
4       (p, r) => r(p, current),
5       previous
6     );
7 }
View Code

  3.handleActions.js 

    将全部的action集中在一块儿处理

    

 1 import handleAction from './handleAction';
 2 import ownKeys from './ownKeys';
 3 import reduceReducers from 'reduce-reducers';
 4 
 5 /**
 6  *
 7  * @param handlers 全部的action
 8  * @param defaultState 初始的state
 9  * @returns {Function}  返回reducer
10  */
11 export default function handleActions(handlers, defaultState) {
12   const reducers = ownKeys(handlers).map(type => {
13     return handleAction(type, handlers[type]);
14   });
15   /**
16    * 处理事后的reduce
17    */
18   const reducer = reduceReducers(...reducers)
19 
20   return typeof defaultState !== 'undefined'
21     ? (state = defaultState, action) => reducer(state, action)
22     : reducer;
23 }
View Code

 

  4.ownKeys.js

    用于判断对象属性的工具

   

 1 export default function ownKeys(object) {
 2   /**
 3    * Reflect.ownKeys相似于Object.keys(),返回对象中可枚举的属性
 4    */
 5   if (typeof Reflect !== 'undefined' && typeof Reflect.ownKeys === 'function') {
 6     return Reflect.ownKeys(object);
 7   }
 8   /**
 9    * 返回对象本身(非原型继承的属性)的属性名称,包括函数。
10    * Object.keys只适用于可枚举的属性,而Object.getOwnPropertyNames返回对象本身的所有属性名称。
11    */
12   let keys = Object.getOwnPropertyNames(object);
13   /**
14    * getOwnPropertySymbols
15    * 返回Symbol类型的属性
16    */
17   if (typeof Object.getOwnPropertySymbols === 'function') {
18     keys = keys.concat(Object.getOwnPropertySymbols(object));
19   }
20 
21   return keys;
22 }
View Code

  5.index.js

    输出全部的函数

  

1 import createAction from './createAction';
2 import handleAction from './handleAction';
3 import handleActions from './handleActions';
4 
5 export {
6   createAction,
7   handleAction,
8   handleActions
9 };
View Code
相关文章
相关标签/搜索