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

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 29

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

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

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

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

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

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
  if (typeof global !== 'undefined') {
fxy060608's avatar
fxy060608 已提交
102
    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