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

fxy060608's avatar
fxy060608 已提交
5 6
import {
  promisify
fxy060608's avatar
fxy060608 已提交
7
} from '../helpers/promise'
fxy060608's avatar
fxy060608 已提交
8

fxy060608's avatar
fxy060608 已提交
9
import * as baseApi from './base'
fxy060608's avatar
fxy060608 已提交
10

fxy060608's avatar
fxy060608 已提交
11 12
import wrapper from './wrapper'

fxy060608's avatar
fxy060608 已提交
13
import todoApi from './todo'
fxy060608's avatar
fxy060608 已提交
14 15

import * as extraApi from './extra'
fxy060608's avatar
fxy060608 已提交
16

fxy060608's avatar
fxy060608 已提交
17
import * as eventApi from './event-bus'
18

fxy060608's avatar
fxy060608 已提交
19
import * as api from 'uni-platform/runtime/api/index.js'
fxy060608's avatar
fxy060608 已提交
20

fxy060608's avatar
fxy060608 已提交
21 22 23 24
import {
  protocols,
  todos,
  canIUses
fxy060608's avatar
fxy060608 已提交
25
} from 'uni-platform/runtime/api/protocols'
fxy060608's avatar
fxy060608 已提交
26 27 28

import createApp from './wrapper/create-app'
import createPage from './wrapper/create-page'
fxy060608's avatar
fxy060608 已提交
29 30
import createComponent from './wrapper/create-component'
import createSubpackageApp from './wrapper/create-subpackage-app'
31 32 33 34 35 36

todos.forEach(todoApi => {
  protocols[todoApi] = false
})

canIUses.forEach(canIUseApi => {
fxy060608's avatar
fxy060608 已提交
37 38
  const apiName = protocols[canIUseApi] && protocols[canIUseApi].name ? protocols[canIUseApi].name
    : canIUseApi
39 40 41 42
  if (!__GLOBAL__.canIUse(apiName)) {
    protocols[canIUseApi] = false
  }
})
fxy060608's avatar
fxy060608 已提交
43

fxy060608's avatar
fxy060608 已提交
44 45
let uni = {}

fxy060608's avatar
fxy060608 已提交
46
if (typeof Proxy !== 'undefined' && __PLATFORM__ !== 'app-plus') {
fxy060608's avatar
fxy060608 已提交
47 48
  uni = new Proxy({}, {
    get (target, name) {
fxy060608's avatar
fxy060608 已提交
49
      if (hasOwn(target, name)) {
50 51
        return target[name]
      }
fxy060608's avatar
fxy060608 已提交
52 53
      if (baseApi[name]) {
        return baseApi[name]
fxy060608's avatar
fxy060608 已提交
54 55 56
      }
      if (api[name]) {
        return promisify(name, api[name])
fxy060608's avatar
fxy060608 已提交
57 58 59 60 61 62 63 64
      }
      if (__PLATFORM__ !== 'app-plus') {
        if (extraApi[name]) {
          return promisify(name, extraApi[name])
        }
        if (todoApi[name]) {
          return promisify(name, todoApi[name])
        }
fxy060608's avatar
fxy060608 已提交
65
      }
66 67 68
      if (eventApi[name]) {
        return eventApi[name]
      }
fxy060608's avatar
fxy060608 已提交
69
      if (!hasOwn(__GLOBAL__, name) && !hasOwn(protocols, name)) {
fxy060608's avatar
fxy060608 已提交
70 71
        return
      }
fxy060608's avatar
fxy060608 已提交
72
      return promisify(name, wrapper(name, __GLOBAL__[name]))
73 74
    },
    set (target, name, value) {
fxy060608's avatar
fxy060608 已提交
75
      target[name] = value
fxy060608's avatar
fxy060608 已提交
76
      return true
fxy060608's avatar
fxy060608 已提交
77 78
    }
  })
79
} else {
fxy060608's avatar
fxy060608 已提交
80 81 82
  Object.keys(baseApi).forEach(name => {
    uni[name] = baseApi[name]
  })
fxy060608's avatar
fxy060608 已提交
83

fxy060608's avatar
fxy060608 已提交
84 85 86 87 88 89 90
  if (__PLATFORM__ !== 'app-plus') {
    Object.keys(todoApi).forEach(name => {
      uni[name] = promisify(name, todoApi[name])
    })
    Object.keys(extraApi).forEach(name => {
      uni[name] = promisify(name, todoApi[name])
    })
fxy060608's avatar
fxy060608 已提交
91
  }
fxy060608's avatar
fxy060608 已提交
92

93 94 95 96
  Object.keys(eventApi).forEach(name => {
    uni[name] = eventApi[name]
  })

fxy060608's avatar
fxy060608 已提交
97 98 99 100 101
  Object.keys(api).forEach(name => {
    uni[name] = promisify(name, api[name])
  })

  Object.keys(__GLOBAL__).forEach(name => {
fxy060608's avatar
fxy060608 已提交
102 103
    if (hasOwn(__GLOBAL__, name) || hasOwn(protocols, name)) {
      uni[name] = promisify(name, wrapper(name, __GLOBAL__[name]))
fxy060608's avatar
fxy060608 已提交
104 105 106
    }
  })
}
107 108

if (__PLATFORM__ === 'app-plus') {
fxy060608's avatar
fxy060608 已提交
109
  if (typeof global !== 'undefined') {
fxy060608's avatar
fxy060608 已提交
110
    global.uni = uni
111 112 113
    global.UniEmitter = eventApi
  }
}
fxy060608's avatar
fxy060608 已提交
114

115 116 117
__GLOBAL__.createApp = createApp
__GLOBAL__.createPage = createPage
__GLOBAL__.createComponent = createComponent
fxy060608's avatar
fxy060608 已提交
118
__GLOBAL__.createSubpackageApp = createSubpackageApp
119 120 121 122

export {
  createApp,
  createPage,
fxy060608's avatar
fxy060608 已提交
123 124
  createComponent,
  createSubpackageApp
125
}
126

fxy060608's avatar
fxy060608 已提交
127
export default uni