index.js 1.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
import {
  hasOwn
} from 'uni-shared'

import {
  promisify
} from 'uni-core/helpers/promise'

9 10 11 12 13 14 15 16 17 18 19 20
import {
  initSubNVue
} from '../sub-nvue'

import {
  initPostMessage
} from '../post-message'

import {
  initTitleNView
} from '../title-nview'

fxy060608's avatar
fxy060608 已提交
21 22
import * as apis from './api'

23 24 25 26 27 28 29 30 31 32
export default function initUni (uni, nvue, plus, BroadcastChannel) {
  const {
    getSubNVueById,
    getCurrentSubNVue
  } = initSubNVue(nvue, plus, BroadcastChannel)

  const scopedApis = Object.assign({
    getSubNVueById,
    getCurrentSubNVue,
    requireNativePlugin: nvue.requireModule
fxy060608's avatar
fxy060608 已提交
33
  }, initTitleNView(nvue), initPostMessage(nvue))
34

fxy060608's avatar
fxy060608 已提交
35 36 37 38 39 40
  if (typeof Proxy !== 'undefined') {
    return new Proxy({}, {
      get (target, name) {
        if (apis[name]) {
          return apis[name]
        }
41 42
        if (scopedApis[name]) {
          return scopedApis[name]
fxy060608's avatar
fxy060608 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56
        }
        if (!hasOwn(uni, name)) {
          return
        }
        return promisify(name, uni[name])
      }
    })
  }
  const ret = {
    requireNativePlugin: nvue.requireModule
  }
  Object.keys(apis).forEach(name => {
    ret[name] = apis[name]
  })
57 58 59
  Object.keys(scopedApis).forEach(name => {
    ret[name] = scopedApis[name]
  })
fxy060608's avatar
fxy060608 已提交
60 61 62 63 64
  Object.keys(uni).forEach(name => {
    ret[name] = promisify(name, uni[name])
  })
  return ret
}