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

feat: uni.offAccelerometerChange

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