美文网首页
redux中间件源码分析

redux中间件源码分析

作者: king_184f | 来源:发表于2019-07-26 11:47 被阅读0次

    废话不多说,直接上干货:

    首先说下middleware怎么用
     store = createStore(reducer, applyMiddleware(middleware, middleware2));

    下面先来看createStore里面干了什么,直接上源码

    export default function createStore(reducer, preloadedState, enhancer) {

      if (

        (typeof preloadedState === 'function' && typeof enhancer === 'function') ||

        (typeof enhancer === 'function' && typeof arguments[3] === 'function')

      ) {

        throw new Error(

          'It looks like you are passing several store enhancers to ' +

            'createStore(). This is not supported. Instead, compose them ' +

            'together to a single function.'

        )

      }

      if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') {

        enhancer = preloadedState

        preloadedState = undefined

      }

      if (typeof enhancer !== 'undefined') {

        if (typeof enhancer !== 'function') {

          throw new Error('Expected the enhancer to be a function.')

        }

        return enhancer(createStore)(reducer, preloadedState)

      }

      if (typeof reducer !== 'function') {

        throw new Error('Expected the reducer to be a function.')

      }

      let currentReducer = reducer

      let currentState = preloadedState

      let currentListeners = []

      let nextListeners = currentListeners

      let isDispatching = false

      return {

        dispatch,

        subscribe,

        getState,

        replaceReducer,

        [$$observable]: observable

      }

    }

    相关文章

      网友评论

          本文标题:redux中间件源码分析

          本文链接:https://www.haomeiwen.com/subject/ftzddxtx.html