All files createHandler.js

88.46% Statements 23/26
40% Branches 2/5
80% Functions 8/10
87.5% Lines 21/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        17x     17x 17x   1x   17x 17x             1x 1x   1x   1x 1x 1x       1x   1x     1x 1x   1x   1x 1x             1x 1x        
import Queue from '@r14c/async-utils/Queue'
import flattenMixinTree from './flattenMixinTree'
import includes from 'lodash/includes'
 
let storeSubs = []
 
export default (Vue, options = {}) => {
  const concurrency = options.concurrency
  const queue = Queue.create({
    concurrency,
    next: () => new Promise((resolve) => Vue.nextTick(() => resolve()))
  })
  let handlers = {}
  return {
    /**
     * register handlers that will run on datastore events
     *
     * @param {Vue.Component} vm
     */
    add (vm) {
      const storeId = vm.$store.storeId
      Eif (!includes(storeSubs, storeId)) {
        // only sub to `Store#all` once
        vm.$store.on('all', (message) => {
          // enqueue a task to handle the vdata listeners for a particular vm
          queue.push(() => {
            Object.values(handlers).forEach((vmHandler) => {
              vmHandler.run(message)
            })
          })
        })
        storeSubs.push(storeId)
      }
      const listeners = flattenMixinTree(vm.$options.mixins)
        .filter((mixin) => !!mixin.vdata)
        .map((mixin) => mixin.vdata)
      Eif (vm.$options.vdata) {
        listeners.push(vm.$options.vdata)
      }
      const handler = {
        run (message) {
          listeners.forEach((fn) => {
            fn.call(vm, message)
          })
        },
        destroy () {
          delete handlers[vm._uid]
        }
      }
      handlers[vm._uid] = handler
      return handler
    }
  }
}