zoukankan      html  css  js  c++  java
  • VueX源码分析(3)

    VueX源码分析(3)

    还剩余

    • /module
    • /plugins
    • store.js

    /plugins/devtool.js

    const devtoolHook =
      typeof window !== 'undefined' &&
      window.__VUE_DEVTOOLS_GLOBAL_HOOK__
    
    export default function devtoolPlugin (store) {
      if (!devtoolHook) return
    
      store._devtoolHook = devtoolHook
    
      devtoolHook.emit('vuex:init', store)
    
      devtoolHook.on('vuex:travel-to-state', targetState => {
        store.replaceState(targetState)
      })
    
      store.subscribe((mutation, state) => {
        devtoolHook.emit('vuex:mutation', mutation, state)
      })
    }
    
    
    • 浏览器开发者工具支持监控VueX

    /plugins/logger.js

    repeat

    function repeat (str, times) {
      return (new Array(times + 1)).join(str)
    }
    

    解析:

    • 字符串重复几次repeat('0', 5)会返回'00000'

    pad

    function pad (num, maxLength) {
      return repeat('0', maxLength - num.toString().length) + num
    }
    

    解析:

    • 给数字补零,如pad(5, 5) 会返回'000005'

    createLogger

    import { deepCopy } from '../util'
    
    export default function createLogger ({
      collapsed = true,
      filter = (mutation, stateBefore, stateAfter) => true,
      transformer = state => state,
      mutationTransformer = mut => mut,
      logger = console
    } = {}) {
      return store => {
        let prevState = deepCopy(store.state)
    
        store.subscribe((mutation, state) => {
          if (typeof logger === 'undefined') {
            return
          }
          const nextState = deepCopy(state)
    
          if (filter(mutation, prevState, nextState)) {
            const time = new Date()
            const formattedTime = ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
            const formattedMutation = mutationTransformer(mutation)
            const message = `mutation ${mutation.type}${formattedTime}`
            const startMessage = collapsed
              ? logger.groupCollapsed
              : logger.group
    
            // render
            try {
              startMessage.call(logger, message)
            } catch (e) {
              console.log(message)
            }
    
            logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
            logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
            logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))
    
            try {
              logger.groupEnd()
            } catch (e) {
              logger.log('—— log end ——')
            }
          }
    
          prevState = nextState
        })
      }
    }
    
    • console的一些报错处理模板
    • store.subscribe监控的是commit,每次commit都会执行一次检测
  • 相关阅读:
    POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)
    UVaLive 5031 Graph and Queries (Treap)
    Uva 11996 Jewel Magic (Splay)
    HYSBZ
    POJ 3580 SuperMemo (Splay 区间更新、翻转、循环右移,插入,删除,查询)
    HDU 1890 Robotic Sort (Splay 区间翻转)
    【转】ACM中java的使用
    HDU 4267 A Simple Problem with Integers (树状数组)
    POJ 1195 Mobile phones (二维树状数组)
    HDU 4417 Super Mario (树状数组/线段树)
  • 原文地址:https://www.cnblogs.com/lantuoxie/p/9353777.html
Copyright © 2011-2022 走看看