Redux源码分析之基本概念

Redux源码分析之createStore

Redux源码分析之bindActionCreators

Redux源码分析之combineReducers

Redux源码分析之compose

Redux源码分析之applyMiddleware

接着前面的,我们继续,打开createStore.js, 直接看最后, createStore返回的就是一个带着5个方法的对象。

  return {
dispatch,
subscribe,
getState,
replaceReducer,
[$$observable]: observable
}

同样的,我先删除一些不需要的代码,简化成如下, 注意看备注。(注:这里先无视中间件和enhancer,后篇再说)

export const ActionTypes = {
INIT: '@@redux/INIT'
}
export default function createStore(reducer, preloadedState, enhancer) {
// 初始化参数
let currentReducer = reducer // 整个reducer
let currentState = preloadedState //当前的state, getState返回的值就是他,
let currentListeners = [] // 当前的订阅,搭配 nextListeners
let nextListeners = currentListeners //下一次的订阅 ,搭配currentListeners
let isDispatching = false //是否处于 dispatch action 状态中 // 内部方法
function ensureCanMutateNextListeners() { } // 确保currentListeners 和 nextListeners 是不同的引用
function getState() { } // 获得当前的状态,返回的就是currentState
function subscribe(listener) { } //订阅监听,返回一个函数,执行该函数,取消监听
function dispatch(action) { } // dispacth action
function replaceReducer(nextReducer) { } // 替换 reducer
function observable() { } //不知道哈哈 //初始化state
dispatch({ type: ActionTypes.INIT }) //返回方法
return {
dispatch,
subscribe,
getState,
replaceReducer,
[$$observable]: observable
}
}

 

ensureCanMutateNextListeners

这个方法主要用在 subscribe里面,

  • 在每次订阅和取消订阅的时候,会让 nextListeners 和 currentListeners 不是同一个引用,
  • 在每次 dispatch的时候,当 reducer执行完毕,订阅执行前,让 nextListeners 和 currentListeners 是一个引用
  function ensureCanMutateNextListeners() {
if (nextListeners === currentListeners) {
nextListeners = currentListeners.slice()
}
}

  为什么这么设计,在subscribe方法上有很详细的注解,我的理解是假如订阅在执行过程中,这里说的是订阅执行过程,不是reducer执行过程

有新加的订阅添加的时候,新的订阅是不会被执行的,因为是一份拷贝

有新的订阅删除的时候,被删除的还是会执行的。

       简单说,就是新的删除和添加,下次生效。

 
getState
就是返回利用闭包存的currentState
  /**
* Reads the state tree managed by the store.
*
* @returns {any} The current state tree of your application.
*/
function getState() {
return currentState
}

 

 subscribe

添加订阅

  • 每次添加前,如果 nextListeners 和 currentListeners 是一个引用,重新复制一个
  • 并存入 nextListeners
  • 返回一个函数,执行该函数取消订阅,
  function subscribe(listener) {
if (typeof listener !== 'function') {
throw new Error('Expected listener to be a function.')
} let isSubscribed = true ensureCanMutateNextListeners() //复制新的
nextListeners.push(listener) return function unsubscribe() {
if (!isSubscribed) {
return
} isSubscribed = false ensureCanMutateNextListeners() // 复制新的
const index = nextListeners.indexOf(listener)
nextListeners.splice(index, 1) // 从nextListeners里面删除,下次dispatch会生效
}
}

dispatch

派发一个action,让reducer更新数据,下面都有注释了,为啥可说的。
  •  如果上一次派发没完毕,接着派发是会出异常的,对于单线程的js来说倒是没啥大问题
  function dispatch(action) {
if (!isPlainObject(action)) { // action 必须是对象
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
} if (typeof action.type === 'undefined') { // 必须有type属性
throw new Error(
'Actions may not have an undefined "type" property. ' +
'Have you misspelled a constant?'
)
} if (isDispatching) { // 正在派发,抛出异常
throw new Error('Reducers may not dispatch actions.')
} try {
isDispatching = true // 标记,正在派发
currentState = currentReducer(currentState, action)
} finally {
isDispatching = false //标记派发完毕
} const listeners = currentListeners = nextListeners // 让nextListeners生效
for (let i = 0; i < listeners.length; i++) { // 挨个执行订阅
const listener = listeners[i]
listener()
} return action // 返回action
}

  

replaceReducer
 function replaceReducer(nextReducer) {
if (typeof nextReducer !== 'function') { // 不是函数,抛出异常
throw new Error('Expected the nextReducer to be a function.')
} currentReducer = nextReducer // 替换reducer
dispatch({ type: ActionTypes.INIT }) // 重新初始化
}

  

observable 还没啥研究,暂不说了。
最后的代码为, 
  • 初始化 state
  • 返回相关方法
  dispatch({ type: ActionTypes.INIT })

  return {
dispatch,
subscribe,
getState,
replaceReducer,
[$$observable]: observable
}

  这里说一下 dispatch({ type: ActionTypes.INIT }) 是怎么达到初始化state的,

  我们再回头看一下disptach中的一段代码

    try {
isDispatching = true
currentState = currentReducer(currentState, action)
} finally {
isDispatching = false
}

      这里先讲非合成的reducer,带合成的后面说。

createStore的第一个参数为 reducer,第二个为初始化state的默认值,

  • 如果你传入了第二个参数,currentState就等于你传入的值,而执行一次 dispatch的时候,系统定义的 type 为@@redux/INIT的action,你肯定是没有定义的额,看下面代码,就会直接返回state,   那么执行 currentReducer(currentState, action) 得到的结果还是 currentState
  • 如果你没有传入第二个参数,在reducer的第一个参数指定了默认值,那么reducer处理type为 @@redux/INIT的action的时候,返回的就是reducer第一个参数 state的默认值,然后被赋值给了currentState
  • 如果没有传入第二个参数,同时reducer的state也没指定值,那么,你的dispatch一般都会报错,因为你的state从开始就是undefined
  • 如果recuder函数和createStore都设置了默认了,那么reducer的默认值是不会生效的
let todoReducer = function (state = todoList, action) {
switch (action.type) {
case 'add':
return [...state, action.todo]
case 'delete':
return state.filter(todo => todo.id !== action.id)
default:
return state
}
}

  这里相对特别的是 合成recuder,后面再说。

 

到此为止,你只用redux的createStore方法,就能完成数据控制了,combineReducers,bindActionCreators,applyMiddleware,compose 都只是对redux的增强。

再回头看看我们第一篇提到的代码:(云淡风轻)

  • 初始化的state在recuder赋值,和在createStore赋值是等价的,都赋值的话,createStore的赋值会生效。 (createStore用的是显示赋值, reducer:默认参数) 
/* 简单示例 */
let { createStore } = self.Redux //默认state
let todoList = []
// reducer
let todoReducer = function (state, action) {
switch (action.type) {
case 'add':
return [...state, action.todo]
case 'delete':
return state.filter(todo => todo.id !== action.id)
default:
return state
}
} //创建store
let store = createStore(todoReducer,todoList) //订阅
function subscribe1Fn() {
console.log(store.getState())
}
let sub = store.subscribe(subscribe1Fn) store.dispatch({
type: 'add',
todo: {
id: 1,
content: '学习redux'
}
}) store.dispatch({
type: 'add',
todo: {
id: 2,
content: '吃饭睡觉'
}
}) store.dispatch({
type: 'delete',
id: 2
}) // 取消订阅
sub() console.log('取消订阅后:')
store.dispatch({
type: 'add',
todo: {
id: 3,
content: '打游戏'
}
})

   

Redux源码分析之createStore的更多相关文章

  1. Redux源码分析之applyMiddleware

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  2. Redux源码分析之基本概念

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  3. Redux源码分析之bindActionCreators

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  4. Redux源码分析之combineReducers

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  5. Redux源码分析之compose

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  6. redux源码图解:createStore 和 applyMiddleware

    在研究 redux-saga时,发现自己对 redux middleware 不是太了解,因此,便决定先深入解读一下 redux 源码.跟大多数人一样,发现 redux源码 真的很精简,目录结构如下: ...

  7. redux源码学习笔记 - createStore

    本篇是学习redux源码的一些记录,学习的redux版本是^4.0.1. 在页面开发时,需要管理很多状态(state),比如服务器响应,缓存数据,UI状态等等···当页面的庞大时,状态就会变的混乱.r ...

  8. 史上最全的 Redux 源码分析

    前言 用 React + Redux 已经一段时间了,记得刚开始用Redux 的时候感觉非常绕,总搞不起里面的关系,如果大家用一段时间Redux又看了它的源码话,对你的理解会有很大的帮助.看完后,在回 ...

  9. 正式学习React(四) ----Redux源码分析

    今天看了下Redux的源码,竟然出奇的简单,好吧.简单翻译做下笔记: 喜欢的同学自己可以去github上看:点这里 createStore.js import isPlainObject from ' ...

随机推荐

  1. 总结·CSS3中定位模型之position属性的使用方法

    一.position元素介绍 position属性规定了元素的定位类型,通过定位,可准确地定义元素相对于其正常位置而应该出现的位置,或者是相对于父元素.另一元素和浏览器窗口等的位置. position ...

  2. 对js运算符“||”和“&&”的总结

    首先出个题: 如图: 假设对成长速度显示规定如下: 成长速度为5显示1个箭头: 成长速度为10显示2个箭头: 成长速度为12显示3个箭头: 成长速度为15显示4个箭头: 其他都显示都显示0各箭头. 用 ...

  3. gdb常用命令及使用gdb调试多进程多线程程序

    一.常用普通调试命令 1.简单介绍GDB 介绍: gdb是Linux环境下的代码调试⼯具.使⽤:需要在源代码⽣成的时候加上 -g 选项.开始使⽤: gdb binFile退出: ctrl + d 或 ...

  4. js循环处理后台返回的json数组

    <script type="text/javascript"> function gongdan_search(elm){ var dangqian_value=$(e ...

  5. js中的数字格式变成货币类型的格式

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  6. EventBus In eShop -- 解析微软微服务架构Demo(四)

    引言 大家好像对分析源码厌倦了,说实在我也会厌倦,不过不看是无法分析其后面的东西,从易到难是一个必要的过程. 今天说下EventBus,前几天园里的大神已经把其解刨,我今天就借着大神的肩膀,分析下在e ...

  7. [leetcode-508-Most Frequent Subtree Sum]

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  8. MyBatis-sql映射文件

    Sql映射文件 MyBatis真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL映射的XML文件是相当的简单.当然如果你将它们和对等功能的JDBC代码来比较,你会发现映射文件节省了 ...

  9. [图形学] Chp14 GLU曲面裁剪函数程序示例及样条表示遗留问题

    样条表示这章已经看完,最后的GLU曲面裁剪函数,打算按书中的示例实现一下,其中遇到了几个问题. 先介绍一下GLU曲面裁剪函数的使用方法. 1 裁剪函数是成对出现的: gluBeginTrim和gluE ...

  10. Student implements java.io.Serializable

    package JBJADV003; public class Student implements java.io.Serializable { private String name; priva ...