提交 6ced4d4d 编写于 作者: Q qiang

feat: uni.offAccelerometerChange

上级 deca0508
......@@ -73,6 +73,7 @@ const device = [
'getNetworkType',
'onNetworkStatusChange',
'onAccelerometerChange',
'offAccelerometerChange',
'startAccelerometer',
'stopAccelerometer',
'onCompassChange',
......@@ -162,12 +163,12 @@ const ui = [
'createSelectorQuery',
'createIntersectionObserver',
'createMediaQueryObserver',
'getMenuButtonBoundingClientRect',
'showTopWindow',
'showLeftWindow',
'showRightWindow',
'hideTopWindow',
'hideLeftWindow',
'getMenuButtonBoundingClientRect',
'showTopWindow',
'showLeftWindow',
'showRightWindow',
'hideTopWindow',
'hideLeftWindow',
'hideRightWindow',
]
......@@ -224,7 +225,7 @@ const third = [
]
const ad = [
'createRewardedVideoAd',
'createRewardedVideoAd',
'createFullScreenVideoAd'
]
......
......@@ -106,18 +106,25 @@ function createKeepAliveApiCallback (apiName, callback) {
const callbackId = invokeCallbackId++
const invokeCallbackName = 'api.' + apiName + '.' + callbackId
const invokeCallback = function (res, extras) {
callback(res, extras)
}
invokeCallbacks[callbackId] = {
name: invokeCallbackName,
keepAlive: true,
callback: invokeCallback
callback
}
return callbackId
}
function getKeepAliveApiCallback (apiName, callback) {
for (const key in invokeCallbacks) {
const item = invokeCallbacks[key]
if (item.name.startsWith('api.' + apiName.replace(/^off/, 'on')) && item.callback === callback) {
delete invokeCallbacks[key]
return Number(key)
}
}
return 'fail'
}
function createApiCallback (apiName, params = {}, extras = {}) {
if (!isPlainObject(params)) {
return {
......@@ -294,7 +301,7 @@ export function wrapper (name, invokeMethod, extras = {}) {
}
} else if (isCallbackApi(name)) {
if (validateParams(name, args, -1)) {
return invokeMethod(createKeepAliveApiCallback(name, args[0]))
return invokeMethod((name.startsWith('off') ? getKeepAliveApiCallback : createKeepAliveApiCallback)(name, args[0]))
}
} else {
let argsObj = {}
......@@ -322,4 +329,4 @@ export function wrapper (name, invokeMethod, extras = {}) {
}
}
}
}
}
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
})
}
......@@ -17,9 +17,7 @@ export function onWindowResize (callbackId) {
callbacks.push(callbackId)
}
export function offWindowResize (callbackId) {
// TODO 目前 on 和 off 即使传入同一个 function,获取到的 callbackId 也不会一致,导致不能 off 掉指定
// 后续修复
export function offWindowResize (callbackId) {
// 此处和微信平台一致查询不到去掉最后一个
callbacks.splice(callbacks.indexOf(callbackId), 1)
}
}
......@@ -3,53 +3,61 @@ import {
} from '../constants'
import {
getLastWebview
} from '../util'
import {
publish
invoke
} from '../../bridge'
let watchAccelerationId = false
let isWatchAcceleration = false
let listener
const clearWatchAcceleration = () => {
if (watchAccelerationId) {
plus.accelerometer.clearWatch(watchAccelerationId)
watchAccelerationId = false
}
}
const callbackIds = []
export function enableAccelerometer ({
enable
}) {
if (enable) { // 启用监听
clearWatchAcceleration()
watchAccelerationId = plus.accelerometer.watchAcceleration((res) => {
publish('onAccelerometerChange', {
export function startAccelerometer (options, callbackId) {
listener = listener || plus.accelerometer.watchAcceleration((res) => {
callbackIds.forEach(callbackId => {
invoke(callbackId, {
x: res.xAxis,
y: res.yAxis,
z: res.zAxis,
errMsg: 'enableAccelerometer:ok'
z: res.zAxis
})
}, (e) => {
publish('onAccelerometerChange', {
errMsg: 'enableAccelerometer:fail'
})
}, {
frequency: DEVICE_FREQUENCY
})
if (!isWatchAcceleration) {
isWatchAcceleration = true
const webview = getLastWebview()
if (webview) {
webview.addEventListener('close', clearWatchAcceleration)
}
}, err => {
listener = null
invoke(callbackId, {
errMsg: `startAccelerometer:fail ${err.message}`
})
}, {
frequency: DEVICE_FREQUENCY
})
setTimeout(() => {
invoke(callbackId, {
errMsg: 'startAccelerometer:ok'
})
}, DEVICE_FREQUENCY)
}
export function stopAccelerometer () {
if (listener) {
plus.accelerometer.clearWatch(listener)
listener = null
}
return {}
}
export function onAccelerometerChange (callbackId) {
if (!callbackIds.length) {
startAccelerometer()
}
callbackIds.push(callbackId)
}
export function offAccelerometerChange (callbackId) {
// 暂不支持移除所有监听
if (callbackId) {
const index = callbackIds.indexOf(callbackId)
if (index >= 0) {
callbackIds.splice(index, 1)
}
} else {
clearWatchAcceleration()
}
return {
errMsg: 'enableAccelerometer:ok'
if (!callbackIds.length) {
stopAccelerometer()
}
}
import {
publish
} from '../../bridge'
let listener
export function enableAccelerometer ({
enable
}) {
return enable ? startAccelerometer() : stopAccelerometer()
}
const callbackIds = []
/**
* 开始监听加速度数据
*/
function startAccelerometer () {
if (window.DeviceMotionEvent) {
export function startAccelerometer (options, callbackId) {
const {
invokeCallbackHandler: invoke
} = UniServiceJSBridge
if (!window.DeviceMotionEvent) {
return {
errMsg: 'startAccelerometer:fail'
}
}
function addEventListener () {
listener = function (event) {
const acceleration = event.acceleration || event.accelerationIncludingGravity
publish('onAccelerometerChange', {
x: acceleration.x || 0,
y: acceleration.y || 0,
z: acceleration.z || 0,
errMsg: 'onAccelerometerChange:ok'
callbackIds.forEach(callbackId => {
invoke(callbackId, {
x: acceleration.x || 0,
y: acceleration.y || 0,
z: acceleration.z || 0
})
})
}
window.addEventListener('devicemotion', listener, false)
return {}
} else {
throw new Error('device nonsupport devicemotion')
}
if (!listener) {
if (DeviceMotionEvent.requestPermission) {
DeviceMotionEvent.requestPermission().then((res) => {
if (res === 'granted') {
addEventListener()
invoke(callbackId, {
errMsg: 'startAccelerometer:ok'
})
} else {
invoke(callbackId, {
errMsg: `startAccelerometer:fail ${res}`
})
}
}).catch(error => {
invoke(callbackId, {
errMsg: `startAccelerometer:fail ${error}`
})
})
return
}
addEventListener()
}
return {}
}
/**
* 停止监听加速度数据
*/
function stopAccelerometer () {
export function stopAccelerometer () {
if (listener) {
window.removeEventListener('devicemotion', listener, false)
listener = null
}
return {}
}
}
export function onAccelerometerChange (callbackId) {
if (!callbackIds.length) {
startAccelerometer()
}
callbackIds.push(callbackId)
}
export function offAccelerometerChange (callbackId) {
// 暂不支持移除所有监听
if (callbackId) {
const index = callbackIds.indexOf(callbackId)
if (index >= 0) {
callbackIds.splice(index, 1)
}
}
if (!callbackIds.length) {
stopAccelerometer()
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册