Skip to content

Commit

Permalink
fix: 修复 Accelerometer 事件绑定问题
Browse files Browse the repository at this point in the history
  • Loading branch information
iChengbo authored and zhiqingchen committed Mar 23, 2021
1 parent 60a0bb0 commit 3a4226e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
50 changes: 26 additions & 24 deletions packages/taro-rn/src/lib/accelerometer.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,49 @@
import { Accelerometer } from 'expo-sensors'

const accCase: any = {}
const accCase: any = {
callbacks: [],
}
const intervalMap: any = {
game: 29,
game: 20,
ui: 60,
normal: 200
}

function offAccelerometerChange (fnc?: () => any): void {
function offAccelerometerChange(fnc?: () => any): void {
if (fnc && typeof fnc === 'function') {
if (accCase.callback === fnc) {
accCase.callback = null
} else {
console.warn('offAccelerometerChange failed')
}
accCase.callbacks = accCase.callbacks.filter((cb: (...args: any[]) => any) => cb !== fnc)
} else if (fnc === undefined) {
accCase.callbacks = []
} else {
Accelerometer.removeAllListeners()
accCase.listener = null
accCase.callback = null
console.warn('offAccelerometerChange failed')
}
}

function onAccelerometerChange (fnc: Taro.onAccelerometerChange.Callback): void {
accCase.callback = fnc
function onAccelerometerChange(fnc: Taro.onAccelerometerChange.Callback): void {
accCase.callbacks.push(fnc)
}

/**
* 开始监听加速度数据。
* @param {Object} opts
* @param {string} [opts.interval='normal'] 监听加速度数据回调函数的执行频率
*/
function startAccelerometer (opts: Taro.startAccelerometer.Option = {}): Promise<Taro.General.CallbackResult> {
function startAccelerometer(opts: Taro.startAccelerometer.Option = {}): Promise<Taro.General.CallbackResult> {
const { interval = 'normal', success, fail, complete } = opts
accCase.interval = interval
const res = { errMsg: 'startAccelerometer:ok' }
try {
accCase.listener = Accelerometer.addListener(accCase.callback)
success && success(res)
complete && complete(res)
accCase.listener = Accelerometer.addListener((e: Taro.onAccelerometerChange.Result) => {
accCase.callbacks.forEach((cb: (...args: any[]) => any) => {
cb?.(e)
});
})
success?.(res)
complete?.(res)
} catch (error) {
res.errMsg = 'startAccelerometer:fail'
fail && fail(res)
complete && complete(res)
fail?.(res)
complete?.(res)
return Promise.reject(res)
}
Accelerometer.setUpdateInterval(intervalMap[interval])
Expand All @@ -52,19 +54,19 @@ function startAccelerometer (opts: Taro.startAccelerometer.Option = {}): Promise
* 停止监听加速度数据
* @param opts
*/
function stopAccelerometer (opts: Taro.stopAccelerometer.Option = {}): Promise<Taro.General.CallbackResult> {
function stopAccelerometer(opts: Taro.stopAccelerometer.Option = {}): Promise<Taro.General.CallbackResult> {
const { success, fail, complete } = opts
const res = { errMsg: 'stopAccelerometer:ok' }
try {
accCase.listener.remove()
accCase.listener = null
success && success(res)
complete && complete(res)
success?.(res)
complete?.(res)
return Promise.resolve(res)
} catch (error) {
res.errMsg = 'stopAccelerometer:fail'
fail && fail(res)
complete && complete(res)
fail?.(res)
complete?.(res)
return Promise.reject(res)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-rn/src/lib/gyroscope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function onGyroscopeChange(fnc: Taro.onGyroscopeChange.Callback): void {
* 取消监听陀螺仪数据变化事件
* @param opts
*/
function offGyroscopeChange(fnc?: Taro.onGyroscopeChange.Callback) {
function offGyroscopeChange(fnc?: () => any) {
if (fnc && typeof fnc === 'function') {
gyroCase.callbacks = gyroCase.callbacks.filter((cb: Taro.onGyroscopeChange.Callback) => {
return cb !== fnc
Expand Down

0 comments on commit 3a4226e

Please sign in to comment.