Skip to content

Commit

Permalink
perf: 优化 API 兼容性的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyucoding committed Apr 24, 2019
1 parent 8c2cdc5 commit 8b36237
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 23 deletions.
13 changes: 12 additions & 1 deletion src/core/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ import * as extraApi from './extra'

import * as api from 'uni-platform/service/api/index.js'

import protocols from 'uni-platform/service/api/protocols'
import { protocols, todos, canIUses } from 'uni-platform/service/api/protocols'

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

canIUses.forEach(canIUseApi => {
const apiName = protocols[canIUseApi] && protocols[canIUseApi].name ? protocols[canIUseApi].name : canIUseApi
if (!__GLOBAL__.canIUse(apiName)) {
protocols[canIUseApi] = false
}
})

let uni = {}

Expand Down
2 changes: 1 addition & 1 deletion src/core/runtime/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isContextApi
} from '../helpers/promise'

import protocols from 'uni-platform/service/api/protocols'
import { protocols } from 'uni-platform/service/api/protocols'

const CALLBACKS = ['success', 'fail', 'cancel', 'complete']

Expand Down
4 changes: 3 additions & 1 deletion src/platforms/app-plus/service/api/protocols.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default {}
export const protocols = {}
export const todos = []
export const canIUses = []
18 changes: 11 additions & 7 deletions src/platforms/mp-alipay/service/api/protocols.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 不支持的 API 列表
const TODOS = [
const todos = [
'saveImageToPhotosAlbum',
'getRecorderManager',
'getBackgroundAudioManager',
Expand All @@ -24,7 +24,6 @@ const TODOS = [
'hideTabBarRedDot',
'setBackgroundColor',
'setBackgroundTextStyle',
'startPullDownRefresh',
'createIntersectionObserver',
'authorize',
'openSetting',
Expand All @@ -45,6 +44,11 @@ const TODOS = [
'offWindowResize'
]

// 存在兼容性的 API 列表
const canIUses = [
'startPullDownRefresh'
]

function _handleNetworkInfo (result) {
switch (result.networkType) {
case 'NOTREACHABLE':
Expand Down Expand Up @@ -359,8 +363,8 @@ const protocols = { // 需要做转换的 API 列表
}
}

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

export default protocols
export {
protocols,
todos,
canIUses
}
15 changes: 9 additions & 6 deletions src/platforms/mp-baidu/service/api/protocols.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 不支持的 API 列表
const TODOS = [
const todos = [
'hideKeyboard',
'onGyroscopeChange',
'startGyroscope',
Expand Down Expand Up @@ -32,6 +32,9 @@ const TODOS = [
'offWindowResize'
]

// 存在兼容性的 API 列表
const canIUses = []

function createTodoMethod (contextName, methodName) {
return function unsupported () {
console.error(`__PLATFORM_TITLE__ ${contextName}暂不支持${methodName}`)
Expand Down Expand Up @@ -93,8 +96,8 @@ const protocols = {
}
}

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

export default protocols
export {
protocols,
todos,
canIUses
}
15 changes: 9 additions & 6 deletions src/platforms/mp-toutiao/service/api/protocols.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 不支持的 API 列表
const TODOS = [
const todos = [
'hideKeyboard',
'onSocketOpen',
'onSocketError',
Expand Down Expand Up @@ -81,6 +81,9 @@ const TODOS = [
'offWindowResize'
]

// 存在兼容性的 API 列表
const canIUses = []

// 需要做转换的 API 列表
const protocols = {
chooseImage: {
Expand Down Expand Up @@ -154,8 +157,8 @@ const protocols = {
}
}

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

export default protocols
export {
protocols,
todos,
canIUses
}
4 changes: 3 additions & 1 deletion src/platforms/mp-weixin/service/api/protocols.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default {}
export const protocols = {}
export const todos = []
export const canIUses = []

0 comments on commit 8b36237

Please sign in to comment.