All files vdata.js

69.23% Statements 18/26
50% Branches 7/14
50% Functions 1/2
70.83% Lines 17/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68            17x                             17x 17x 17x 17x   28x 28x     28x       17x   10x 10x     10x       17x 17x                 5x 1x                 17x      
import AsyncDataMixin from './AsyncDataMixin'
import createHandler from './createHandler'
import createStore from './createStore'
import get from 'lodash/get'
import isFunction from 'lodash/isFunction'
 
const hasVdata = (vm) => !!get(vm, '$options.vdata')
 
/**
 * vdata plugin
 */
export default {
  createConfig (fn) {
    return (V) => fn(V)
  },
  /**
   * @param {object} Vue
   * @param {object} options - options are passed into `createStore`
   * @param {number} [options.queueConcurrency=2]
   */
  install (Vue, options) {
    const vdataOptions = (isFunction(options)) ? options(Vue) : options
    const store = createStore(vdataOptions)
    const concurrency = vdataOptions.queueConcurrency || 2
    Object.defineProperty(Vue, '$store', {
      get () {
        const parentStore = get(this, '$parent.$store')
        Iif (parentStore) {
          return parentStore
        } else {
          return store
        }
      }
    })
    Object.defineProperty(Vue.prototype, '$store', {
      get () {
        const parentStore = get(this, '$parent.$store')
        Iif (parentStore) {
          return parentStore
        } else {
          return store
        }
      }
    })
    const vmHandler = createHandler(Vue, {concurrency})
    Vue.mixin({
      methods: {
        $vdata (message) {
          if (hasVdata(this)) {
            this._vdataHandler.run(message)
          }
        }
      },
      created () {
        if (hasVdata(this)) {
          this._vdataHandler = vmHandler.add(this)
        }
      },
      beforeDestroy () {
        if (hasVdata(this)) {
          this._vdataHandler.destroy()
        }
      }
    })
    Vue.mixin(AsyncDataMixin)
  }
}