// 如果有增强器但是增强器不是函数 抛出异常 if (typeof enhancer !== 'undefined') { if (typeof enhancer !== 'function') { thrownewError('Expected the enhancer to be a function.') }
// 这个是获取到最新state内容 functiongetState() { if (isDispatching) { thrownewError( 'You may not call store.getState() while the reducer is executing. ' + 'The reducer has already received the state as an argument. ' + 'Pass it down from the top reducer instead of reading it from the store.' ) }
return currentState } // 这个subscribe是为了给currentListener增加监听事件 下面的dispatch函数中 在执行完dispatch操作后 会遍历这个监听队列依次订阅的监听事件 functionsubscribe(listener) { if (typeof listener !== 'function') { thrownewError('Expected the listener to be a function.') } // 保证不能再dispatch的过程中触发监听事件 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.' ) }
let isSubscribed = true
ensureCanMutateNextListeners() // 监听事件入栈 nextListeners.push(listener) // 这种写法还是比较常见且妙的 添加监听事件后 通过闭包返回一个移除当前监听事件的方法 // 用法如下 // const store = createStore(...) // const subscribe = store.subscribe(() => {}) // 移除只需要执行subscribe()就行了 returnfunctionunsubscribe() { // 如果该监听被取消 if (!isSubscribed) { return } // 无法再dispatch过程中移除监听事件 if (isDispatching) { thrownewError( 'You may not unsubscribe from a store listener while the reducer is executing. ' + 'See https://redux.js.org/api-reference/store#subscribe(listener) for more details.' ) } // 监听取消的标志 isSubscribed = false
// 每个redux middleware都是一个对dispatch的一个增强 // 是redux的dispatch方法 也是页面修改树状态的唯一途径 functiondispatch(action) { // 首先判断action是不是一个纯对象 即通过{}或者new Object()创建 if (!isPlainObject(action)) { thrownewError( 'Actions must be plain objects. ' + 'Use custom middleware for async actions.' ) } // 保证每个action必须有type属性 if (typeof action.type === 'undefined') { thrownewError( 'Actions may not have an undefined "type" property. ' + 'Have you misspelled a constant?' ) } // 一个action结束才能调用下一个 if (isDispatching) { thrownewError('Reducers may not dispatch actions.') }
// 遍历触发subscribe添加的监听 const listeners = (currentListeners = nextListeners) for (let i = 0; i < listeners.length; i++) { const listener = listeners[i] listener() }
return action }
// 替换当前reducer 这个只在热更新上用过 functionreplaceReducer(nextReducer) { if (typeof nextReducer !== 'function') { thrownewError('Expected the nextReducer to be a function.') }
// 这个待看 暂时不知道是干嘛的 functionobservable() { const outerSubscribe = subscribe return { /** * The minimal observable subscription method. * @param {Object} observer Any object that can be used as an observer. * The observer object should have a `next` method. * @returns {subscription} An object with an `unsubscribe` method that can * be used to unsubscribe the observable from the store, and prevent further * emission of values from the observable. */ subscribe(observer) { if (typeof observer !== 'object' || observer === null) { thrownewTypeError('Expected the observer to be an object.') }
functionobserveState() { if (observer.next) { observer.next(getState()) } }
// 看到这段代码 我觉得猜测是对的 是不是有点像上面的loggerEnhancer的结构 重写dispatch方法 // 所以得出结论 与其说增强器是对store的增强不如说是对dispatch的增强 exportdefaultfunctionapplyMiddleware(...middlewares) { returncreateStore =>(...args) => { // 这里的...args是 reducers、preloadedState、enhancer const store = createStore(...args) letdispatch = () => { thrownewError( `Dispatching while constructing your middleware is not allowed. ` + `Other middleware would not be applied to this dispatch.` ) }