提交 ec066665 编写于 作者: fxy060608's avatar fxy060608

refactor accelerometer

上级 e0039c51
...@@ -159,6 +159,15 @@ function createApiCallback (apiName, params = {}, extras = {}) { ...@@ -159,6 +159,15 @@ function createApiCallback (apiName, params = {}, extras = {}) {
const invokeCallback = function (res) { const invokeCallback = function (res) {
res.errMsg = res.errMsg || apiName + ':ok' res.errMsg = res.errMsg || apiName + ':ok'
// 部分 api 可能返回的 errMsg 的 api 名称部分不一致,格式化为正确的
if (res.errMsg.indexOf(':ok') !== -1) {
res.errMsg = apiName + ':ok'
} else if (res.errMsg.indexOf(':cancel') !== -1) {
res.errMsg = apiName + ':cancel'
} else if (res.errMsg.indexOf(':fail') !== -1) {
res.errMsg = apiName + ':fail'
}
const errMsg = res.errMsg const errMsg = res.errMsg
if (errMsg.indexOf(apiName + ':ok') === 0) { if (errMsg.indexOf(apiName + ':ok') === 0) {
......
...@@ -187,9 +187,9 @@ const third = [ ...@@ -187,9 +187,9 @@ const third = [
'unsubscribePush', 'unsubscribePush',
'onPush', 'onPush',
'offPush', 'offPush',
'requireNativePlugin', 'requireNativePlugin',
'upx2px' 'upx2px'
] ]
const apis = [ const apis = [
...base, ...base,
...@@ -201,10 +201,10 @@ const apis = [ ...@@ -201,10 +201,10 @@ const apis = [
...device, ...device,
...keyboard, ...keyboard,
...ui, ...ui,
...event, ...event,
...file, ...file,
...canvas, ...canvas,
...third ...third
] ]
export default apis module.exports = apis
export const upx2px = [{ export const upx2px = [{
name: 'upx', name: 'upx',
type: Number, type: [Number, String],
required: true required: true
}] }]
import {
invoke
} from 'uni-core/service/bridge'
import {
onMethod,
invokeMethod
} from '../../platform'
const callbacks = []
onMethod('onAccelerometerChange', function (res) {
callbacks.forEach(callbackId => {
invoke(callbackId, res)
})
})
let isEnable = false
/**
* 监听加速度
* @param {*} callbackId
*/
export function onAccelerometerChange (callbackId) {
// TODO 当没有 start 时,添加 on 需要主动 start?
callbacks.push(callbackId)
if (!isEnable) {
startAccelerometer()
}
}
export function startAccelerometer ({
interval // TODO
} = {}) {
if (isEnable) {
return
}
isEnable = true
return invokeMethod('enableAccelerometer', {
enable: true
})
}
export function stopAccelerometer () {
isEnable = false
return invokeMethod('enableAccelerometer', {
enable: false
})
}
...@@ -4,4 +4,4 @@ export function unpack (args) { ...@@ -4,4 +4,4 @@ export function unpack (args) {
export function invoke (...args) { export function invoke (...args) {
return UniServiceJSBridge.invokeCallbackHandler(...args) return UniServiceJSBridge.invokeCallbackHandler(...args)
} }
const callbacks = [] import {
var listener publish
/** } from '../../bridge'
* 监听加速度
* @param {*} callbackId let listener
*/
export function onAccelerometerChange (callbackId) { export function enableAccelerometer ({
callbacks.push(callbackId) enable
if (!listener) { }) {
startAccelerometer() return enable ? startAccelerometer() : stopAccelerometer()
} }
}
/** /**
* 开始监听加速度数据 * 开始监听加速度数据
*/ */
export function startAccelerometer () { function startAccelerometer () {
const { if (window.DeviceMotionEvent) {
invokeCallbackHandler: invoke listener = function (event) {
} = UniServiceJSBridge const acceleration = event.acceleration || event.accelerationIncludingGravity
if (window.DeviceMotionEvent) { publish('onAccelerometerChange', {
listener = function (event) { x: acceleration.x || 0,
callbacks.forEach(callbackId => { y: acceleration.y || 0,
const acceleration = event.acceleration || event.accelerationIncludingGravity z: acceleration.z || 0,
invoke(callbackId, { errMsg: 'onAccelerometerChange:ok'
x: acceleration.x || 0, })
y: acceleration.y || 0, }
z: acceleration.z || 0, window.addEventListener('devicemotion', listener, false)
errMsg: 'onAccelerometerChange:ok' return {}
}) } else {
}) throw new Error('device nonsupport devicemotion')
} }
window.addEventListener('devicemotion', listener, false) }
return {} /**
} else { * 停止监听加速度数据
throw new Error('device nonsupport devicemotion') */
} function stopAccelerometer () {
} if (listener) {
/** window.removeEventListener('devicemotion', listener, false)
* 停止监听加速度数据 listener = null
*/ }
export function stopAccelerometer () { return {}
if (listener) {
window.removeEventListener('devicemotion', listener, false)
listener = null
}
return {}
} }
/**
* 触发 service 层,与 onMethod 对应
*/
export function publish (name, res) {
return UniServiceJSBridge.emit('api.' + name, res)
}
export function publishHandler (event, args, pageId) { export function publishHandler (event, args, pageId) {
// h5 平台直接调用UniViewJSBridge // h5 平台直接调用UniViewJSBridge
global.UniViewJSBridge.subscribeHandler(event, args, pageId) global.UniViewJSBridge.subscribeHandler(event, args, pageId)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册