index.js 2.6 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 9 10

import {
  upx2px
11
} from './upx2px'
fxy060608's avatar
fxy060608 已提交
12

fxy060608's avatar
fxy060608 已提交
13 14
import wrapper from './wrapper'

fxy060608's avatar
fxy060608 已提交
15
import todoApi from './todo'
fxy060608's avatar
fxy060608 已提交
16 17

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

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

fxy060608's avatar
fxy060608 已提交
21 22
import * as api from 'uni-platform/service/api/index.js'

fxy060608's avatar
fxy060608 已提交
23 24 25 26 27 28 29 30 31
import {
  protocols,
  todos,
  canIUses
} from 'uni-platform/service/api/protocols'

import createApp from './wrapper/create-app'
import createPage from './wrapper/create-page'
import createComponent from './wrapper/create-component'
32 33 34 35 36 37

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

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

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

fxy060608's avatar
fxy060608 已提交
47
if (typeof Proxy !== 'undefined' && __PLATFORM__ !== 'app-plus') {
fxy060608's avatar
fxy060608 已提交
48 49 50 51 52 53 54
  uni = new Proxy({}, {
    get (target, name) {
      if (name === 'upx2px') {
        return upx2px
      }
      if (api[name]) {
        return promisify(name, api[name])
fxy060608's avatar
fxy060608 已提交
55 56 57 58 59 60 61 62
      }
      if (__PLATFORM__ !== 'app-plus') {
        if (extraApi[name]) {
          return promisify(name, extraApi[name])
        }
        if (todoApi[name]) {
          return promisify(name, todoApi[name])
        }
fxy060608's avatar
fxy060608 已提交
63
      }
64 65 66
      if (eventApi[name]) {
        return eventApi[name]
      }
fxy060608's avatar
fxy060608 已提交
67
      if (!hasOwn(__GLOBAL__, name) && !hasOwn(protocols, name)) {
fxy060608's avatar
fxy060608 已提交
68 69
        return
      }
fxy060608's avatar
fxy060608 已提交
70
      return promisify(name, wrapper(name, __GLOBAL__[name]))
fxy060608's avatar
fxy060608 已提交
71 72 73 74 75
    }
  })
} else {
  uni.upx2px = upx2px

fxy060608's avatar
fxy060608 已提交
76 77 78 79 80 81 82
  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 已提交
83
  }
fxy060608's avatar
fxy060608 已提交
84

85 86 87 88
  Object.keys(eventApi).forEach(name => {
    uni[name] = eventApi[name]
  })

fxy060608's avatar
fxy060608 已提交
89 90 91 92 93
  Object.keys(api).forEach(name => {
    uni[name] = promisify(name, api[name])
  })

  Object.keys(__GLOBAL__).forEach(name => {
fxy060608's avatar
fxy060608 已提交
94 95
    if (hasOwn(__GLOBAL__, name) || hasOwn(protocols, name)) {
      uni[name] = promisify(name, wrapper(name, __GLOBAL__[name]))
fxy060608's avatar
fxy060608 已提交
96 97 98
    }
  })
}
99 100

if (__PLATFORM__ === 'app-plus') {
fxy060608's avatar
fxy060608 已提交
101 102
  if (typeof global !== 'undefined') {
    global.uni = uni
103 104 105
    global.UniEmitter = eventApi
  }
}
fxy060608's avatar
fxy060608 已提交
106

107 108 109 110 111 112 113 114 115
__GLOBAL__.createApp = createApp
__GLOBAL__.createPage = createPage
__GLOBAL__.createComponent = createComponent

export {
  createApp,
  createPage,
  createComponent
}
116

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