Skip to content

Commit

Permalink
fix: 不允许 Gyroscope 及 Accelerometer 重复 start
Browse files Browse the repository at this point in the history
  • Loading branch information
iChengbo authored and zhiqingchen committed Mar 23, 2021
1 parent 3a4226e commit 9c78a97
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/taro-rn/src/lib/accelerometer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const intervalMap: any = {
normal: 200
}

function offAccelerometerChange(fnc?: () => any): void {
function offAccelerometerChange(fnc?: Taro.onAccelerometerChange.Callback): void {
if (fnc && typeof fnc === 'function') {
accCase.callbacks = accCase.callbacks.filter((cb: (...args: any[]) => any) => cb !== fnc)
} else if (fnc === undefined) {
Expand All @@ -33,6 +33,10 @@ function startAccelerometer(opts: Taro.startAccelerometer.Option = {}): Promise<
accCase.interval = interval
const res = { errMsg: 'startAccelerometer:ok' }
try {
// 适配微信小程序行为:重复 start 失败
if (accCase.listener) {
throw new Error('startAccelerometer:fail')
}
accCase.listener = Accelerometer.addListener((e: Taro.onAccelerometerChange.Result) => {
accCase.callbacks.forEach((cb: (...args: any[]) => any) => {
cb?.(e)
Expand Down
6 changes: 5 additions & 1 deletion packages/taro-rn/src/lib/gyroscope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ function startGyroscope(opts: Taro.startGyroscope.Option = {}): Promise<Taro.Gen
gyroCase.interval = interval
const res = { errMsg: 'startGyroscope:ok' }
try {
// 适配微信小程序行为:重复 start 失败
if (gyroCase.listener) {
throw new Error('startGyroscope:fail')
}
gyroCase.listener = Gyroscope.addListener(e => {
gyroCase.callbacks.forEach((cb: Taro.onGyroscopeChange.Callback) => {
cb?.(e)
Expand Down Expand Up @@ -69,7 +73,7 @@ function onGyroscopeChange(fnc: Taro.onGyroscopeChange.Callback): void {
* 取消监听陀螺仪数据变化事件
* @param opts
*/
function offGyroscopeChange(fnc?: () => any) {
function offGyroscopeChange(fnc?: Taro.onGyroscopeChange.Callback) {
if (fnc && typeof fnc === 'function') {
gyroCase.callbacks = gyroCase.callbacks.filter((cb: Taro.onGyroscopeChange.Callback) => {
return cb !== fnc
Expand Down

0 comments on commit 9c78a97

Please sign in to comment.