Skip to content

Redux源码简读 #13

Description

@jappp

最近闲来无事,对 Redux 的设计思想和工作原理比较感兴趣,由于自身对于 Redux 的了解称不上多透彻,所以决定阅读源码加深理解。关于 Redux 的其它我就不详细介绍了,Redux的核心概念主要就是三点:

  1. Store 全局单一数据源,只读
  2. Action 动作,对变化的描述
  3. Reducer 纯函数,负责对变化进行分发和处理,返回最新的数据给 Store

image

在 Redux 的整个工作流程中,数据流是严格单向的。修改数据的唯一途径就是派发 Action,这种严格单向数据保证了在大型多人协作项目中数据的可控性和可预测性。

这是 Redux 的源码文件目录:
image

我们的目标主要是 createStore.js 和 applyMiddleWare.js 文件。createStore.js 定义了 createStore 方法,也就是我们在使用 Redux 时候最先调用的方法,是 Redux 最核心的API。另外 applyMiddleWare.js 是中间件模块,这个模块比较独立。

其它的例如 bindActionCreators,combineReducers,compose 都是用于定义工具性质的方法,具体情况如果有使用时再介绍。

createStore 源码

functioncreateStore(reducer,preloadedState,enhancer){if(typeofpreloadedState==='function'&&typeofenhancer==='undefined'){enhancer=preloadedState;preloadedState=undefined;}if(typeofenhancer!=='undefined'){returnenhancer(createStore)(reducer,preloadedState);}letcurrentReducer=reducer;letcurrentState=preloadedState;letcurrentListeners=[];letnextListeners=currentListeners;letisDispatching=falsefunctionensureCanMutateNextListeners(){if(nextListeners===currentListeners){nextListeners=currentListeners.slice();}}functiongetState(){returncurrentState;}functionsubscribe(listener){if(typeoflistener!=='function'){thrownewError('Expected the listener to be a function.')}if(isDispatching){thrownewError('You may not call store.subscribe() while the reducer is executing. '+'If you would like to be notified after the store has been updated, subscribe from a '+'component and invoke store.getState() in the callback to access the latest state. '+'See https://redux.js.org/api-reference/store#subscribe(listener) for more details.')}letisSubscribed=true;ensureCanMutateNextListeners();nextListeners.push(listener);returnfunctionunsubscribe(){if(!isSubscribed){return;}isSubscribed=false;ensureCanMutateNextListeners();constindex=nextListeners.indexOf(listener);nextListeners.splice(index,1);};}functiondispatch(action){if(!isPlainObject(action)){thrownewError('Actions must be plain objects. '+'Use custom middleware for async actions.')}if(typeofaction.type==='undefined'){thrownewError('Actions may not have an undefined "type" property. '+'Have you misspelled a constant?')}if(isDispatching){thrownewError('Reducers may not dispatch actions.')}try{isDispatching=truecurrentState=currentReducer(currentState,action)}finally{isDispatching=false}constlisteners=(currentListeners=nextListeners);for(leti=0;i<listeners.length;i++){constlistener=listeners[i];listener();}returnaction;}functionreplaceReducer(nextReducer){currentReducer=nextReducer;dispatch({type: ActionTypes.REPLACE});returnstore;}dispatch({type: ActionTypes.INIT});functionobservable(){// observable 方法的实现}return{
dispatch,
subscribe,
getState,
replaceReducer,[$$observable]: observable}}

我们一步一步来解析该方法的内部逻辑

1. 调用 createStore 方法传入三个参数 reducer,初始状态内容,中间件

functioncreateStore(reducer,preloadedState,enhancer)

2. 前置校验

  • 处理没有传入初始状态内容的时候的情况
  • 若 enhancer 不为空,则用 enhancer 包装 createStore
if(typeofpreloadedState==='function'&&typeofenhancer==='undefined'){// 此时第二个参数会被认为是 enhancer(中间件)enhancer=preloadedState;preloadedState=undefined;}// 当 enhancer 不为空时,便会将原来的 createStore 作为参数传入到 enhancer 中if(typeofenhancer!=='undefined'){returnenhancer(createStore)(reducer,preloadedState);}

3. 定义内部变量

// 记录当前的 reducer,因为 replaceReducer 会修改 reducer 的内容letcurrentReducer=reducer;// 记录当前的 stateletcurrentState=preloadedState;// 声明 listeners 数组,这个数组用于记录在 subscribe 中订阅的事件letcurrentListeners=[];// nextListeners 是 currentListeners 的快照letnextListeners=currentListeners;// 该变量用于记录当前是否正在进行 dispatchletisDispatching=false

4. 定义 ensureCanMutateNextListeners 方法,用于确保 currentListeners 和 nextListeners 不会指向同一个引用,为何需要 nextListeners 是 currentListeners 的副本,我们后续再解释。

functionensureCanMutateNextListeners(){if(nextListeners===currentListeners){nextListeners=currentListeners.slice();}}

5. 定义 getState 方法,该方法用于获取当前的状态

functiongetState(){returncurrentState;}

6. 定义 subscribe 方法,用于订阅监听函数

  • 前置校验
// 校验 listener 的类型if(typeoflistener!=='function'){thrownewError('Expected the listener to be a function.')}// 禁止在 reducer 中调用 subscribeif(isDispatching){thrownewError('You may not call store.subscribe() while the reducer is executing. '+'If you would like to be notified after the store has been updated, subscribe from a '+'component and invoke store.getState() in the callback to access the latest state. '+'See https://redux.js.org/api-reference/store#subscribe(listener) for more details.')}
  • 调用 ensureCanMutateNextListeners 函数
// 确保 nextListeners 与 currentListeners 不指向同一个引用ensureCanMutateNextListeners();
  • 收集监听函数,将入参的 listener 保存到 nextListeners 数组中
nextListeners.push(listener);
  • 返回一个取消订阅当前 listener 的方法
// 该变量用于防止调用多次 unsubscribe 函数letisSubscribed=true;returnfunctionunsubscribe(){if(!isSubscribed){return;}isSubscribed=false;ensureCanMutateNextListeners();constindex=nextListeners.indexOf(listener);// 将当前的 listener 从 nextListeners 数组中删除 nextListeners.splice(index,1);};

7. 定义 dispatch 方法,用于派发Action调用Reducer,触发订阅。

  • 前置校验
// 校验 action 的数据格式是否合法if(!isPlainObject(action)){thrownewError('Actions must be plain objects. '+'Use custom middleware for async actions.')}// 约束 action 中必须有 type 属性作为 action 的唯一标识 if(typeofaction.type==='undefined'){thrownewError('Actions may not have an undefined "type" property. '+'Have you misspelled a constant?')}
  • 上锁,主要是避免在 reducer 函数中重复调用 dispatch
// 若当前已经位于 dispatch 的流程中,则不允许再度发起 dispatch(禁止套娃)if(isDispatching){thrownewError('Reducers may not dispatch actions.')}try{// 执行 reducer 前,先"上锁",标记当前已经存在 dispatch 执行流程isDispatching=true// 调用 reducer,计算新的 state currentState=currentReducer(currentState,action)}finally{// 执行结束后,把"锁"打开,允许再次进行 dispatch isDispatching=false}
  • 触发订阅
// currentListeners 被赋值为 nextListeners,// 因此最终被执行的 listeners 和当前的 nextListeners 指向同一个引用constlisteners=(currentListeners=nextListeners);for(leti=0;i<listeners.length;i++){constlistener=listeners[i];listener();}returnaction;

8. 定义 replaceReducer 方法,该方法用于替换 Reducer

functionreplaceReducer(nextReducer){currentReducer=nextReducer;dispatch({type: ActionTypes.REPLACE});returnstore;}

9. 执行一次 dispatch,完成状态初始化

// 初始化 state,当派发一个 type 为 ActionTypes.INIT 的 action,每个 reducer 都会返回它的初始值dispatch({type: ActionTypes.INIT});

10. 定义 observer 方法,在 redux 内部使用,开发者一般不会直接接触,可忽略

functionobservable(){// observable 方法的实现}

11. 将上述定义的方法放进 store 对象返回

// 将定义的方法包裹在 store 对象里返回return{
dispatch,
subscribe,
getState,
replaceReducer,[$$observable]: observable}

通过分析完 createStore 的整个工作逻辑,不难看出 Redux 主流程中最重要的是 dispatch 和 subscribe 方法,其中 dispatch 将之前提到的 action,reducer 和 store 串联起来,subscribe 方法则实现了 redux 特有的 ”发布-订阅”模式。

在实际的开发过程中,我们只有在需要监听状态变化时,才会调用 subscribe。
subscribe 接收一个 Function 类型的 listener 作为参数,返回的正好是这个 listener 的解绑函数。当dispatch action 发生时,Redux 会在 reducer 执行完毕后,将 listeners 数组中的监听函数逐个执行。

我们之前有提到 currentListeners 和 nextListeners 确保不会指向同一个引用,
在之前的源码解析中,我们看到 subscribe 中的订阅过程和 dispatch 中的发布过程,使用的都是 nextListeners 数组

// 订阅过程nextListeners.push(listener);// 触发订阅constlisteners=(currentListeners=nextListeners);for(leti=0;i<listeners.length;i++){constlistener=listeners[i];listener();}

正是因为所有变更都是在 nextListeners 数组中操作的,我们才需要一个 currentListeners 来确保执行过程中的稳定性。

我们知道 subscribe 返回的是一个解绑函数,如果依次订阅三个函数A,B,C,
例如

[listenerA,listenerB,listenerC]

在函数B中解绑函数A,如果不存在 currentListeners,函数A解绑之后会直接从 nextListeners 数组中消失,

[listenerB,listenerC]

此时for循环已经继续执行就会报错了。

所以为了确保这种情况不出现,Redux 内部使用 ensureCanMutateNextListeners 函数将 nextListeners 和 当前正在执行的 listeners 区分开来,以确保监听函数在执行过程中的稳定性

本来是准备一篇写完两个核心模块的源码的,后面发现太长了,所以就拆分成两块,applyMiddleware 中间件模块下一篇再总结吧。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions