All files / src/instance components.ts

60.87% Statements 14/23
16.67% Branches 2/12
75% Functions 3/4
60.87% Lines 14/23

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  1x 1x 1x 1x   2x 2x 2x               2x           2x           1x   22x 22x 22x                                  
import { isPlainObject } from '../util/isPlainObject'
import { noop } from '../util/noop'
 
export function initComponents(vm, components): {
  [componentName: string] : WechatMiniprogram.Component.RelationOption
} {
  return Object.keys(components).reduce((acc, key) => {
    const { module: componentName, type = 'child', observer = noop } = getData(components[key])
    const linkCb = function () {
      if (typeof observer === 'string') {
        return this[observer]()
      } else if (typeof observer === 'function') {
        return observer()
      }
    }
    const option: WechatMiniprogram.Component.RelationOption = {
      type,
      linked: linkCb,
      linkChanged: linkCb,
      unlinked: linkCb,
    }
    return {
      ...acc,
      [componentName]: option,
    }
  }, {})
}
 
export function getData (comp) {
  if (typeof comp === 'function') {
    Econst ret = comp()
    return {
      ['module']: ret.module,
      type: ret.type,
      observer: ret.observer,
    }
  } else if (typeof comp === 'string') {
    return {
      ['module']: comp,
    }
  } else if (isPlainObject(comp)) {
    return {
      ['module']: comp.module,
      type: comp.type,
      observer: comp.observer,
    }
  }
  return {}
}