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

fxy060608's avatar
fxy060608 已提交
83 84 85 86 87 88 89
  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 已提交
90
  }
fxy060608's avatar
fxy060608 已提交
91

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

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

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

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

114 115 116 117 118 119 120 121 122
__GLOBAL__.createApp = createApp
__GLOBAL__.createPage = createPage
__GLOBAL__.createComponent = createComponent

export {
  createApp,
  createPage,
  createComponent
}
123

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