functioncreateStore(reducer, preloadedState, enhancer) { if (typeof enhancer !== 'undefined') { if (typeof enhancer !== 'function') { thrownewError('Expected the enhancer to be a function.') } // enhancer后面再看 return enhancer(createStore)(reducer, preloadedState) }
// 这里创建了createStore函数的内部变量 // 用于后续创建闭包 // 实际保存state的地方 let currentReducer = reducer let currentState = preloadedState let currentListeners = [] let nextListeners = currentListeners let isDispatching = false
// 拿到当前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 }
// 订阅执行dispatch之后的回调函数 functionsubscribe(listener) { if (typeof listener !== '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#subscribelistener for more details.' ) }
// 这里又是一个闭包 // 每订阅一次就会产生一个这样的标记 let isSubscribed = true
returnfunctionunsubscribe() { if (!isSubscribed) { return }
if (isDispatching) { thrownewError( 'You may not unsubscribe from a store listener while the reducer is executing. ' + 'See https://redux.js.org/api-reference/store#subscribelistener for more details.' ) }
// 这里执行所有的回调listeners const listeners = (currentListeners = nextListeners) for (let i = 0; i < listeners.length; i++) { const listener = listeners[i] listener() }
return action }
// 这个暂时不管 functionreplaceReducer(nextReducer) { if (typeof nextReducer !== 'function') { thrownewError('Expected the nextReducer to be a function.') }
currentReducer = nextReducer
// This action has a similiar effect to ActionTypes.INIT. // Any reducers that existed in both the new and old rootReducer // will receive the previous state. This effectively populates // the new state tree with any relevant data from the old one. dispatch({ type: ActionTypes.REPLACE }) }
// 这个暂时不管 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()) } }
// When a store is created, an "INIT" action is dispatched so that every // reducer returns their initial state. This effectively populates // the initial state tree. dispatch({ type: ActionTypes.INIT })
// 运行结果 // middleware2 before dispatch // middleware1 before dispatch // ruby cool language // middleware1 after dispatch // middleware2 before dispatch
functionapplyMiddleware(...middlewares) { // 此处的createStore入参为上面提到的createStore returncreateStore => (...args) => { const store = createStore(...args) // 这里的dispatch形成了一个闭包 let dispatch = () => { thrownewError( 'Dispatching while constructing your middleware is not allowed. ' + 'Other middleware would not be applied to this dispatch.' ) }