redux-actions源碼解讀


一.什么是redux-actions

   redux-actions是一個簡化action和reducer創建的一個封裝庫,里面有5個js文件,

   createAction.js

   handleAction.js

   handleActions.js

   index.js

   ownKeys.js

二.怎么使用?下面將從源碼一一解釋每個文件的的用處

  1.createAction.js

    從名字就可以看出,這是用來創建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

 

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM