Skip to content

Commit

Permalink
fix(usevibrate interval mode): fix useVibrate interval mode error
Browse files Browse the repository at this point in the history
  • Loading branch information
innocces committed Sep 1, 2021
1 parent 22ac04c commit f99082f
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions packages/hooks/src/useVibrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,38 @@ function useVibrate(
};
}, [timer, interval]);

const vibrateAction = useCallback<VibrateAction>((long) => {
return new Promise((resolve, reject) => {
try {
const vibrateMethod = long ? vibrateLong : vibrateShort;
vibrateMethod({
success: (res) => {
const computedGap =
(gap || DEFAULTGAP) + (long ? LONGINTERVAL : SHORTINTERVAL);
if (interval) {
timer.current = setTimeout(() => {
vibrateAction(long);
}, computedGap);
}
resolve(res);
},
fail: reject,
}).catch(reject);
} catch (e) {
reject(e);
}
});
}, []);

const stopVibrateAction = useCallback(() => {
timer.current && clearTimeout(timer.current);
}, [timer]);

const vibrateAction = useCallback<VibrateAction>(
(long) => {
return new Promise((resolve, reject) => {
try {
const vibrateMethod = long ? vibrateLong : vibrateShort;
vibrateMethod({
success: (res) => {
const computedGap =
(gap || DEFAULTGAP) + (long ? LONGINTERVAL : SHORTINTERVAL);
if (interval) {
// whether timer exist, clear first to fix multi vibrate
stopVibrateAction();
timer.current = setTimeout(() => {
vibrateAction(long);
}, computedGap);
}
resolve(res);
},
fail: reject,
}).catch(reject);
} catch (e) {
reject(e);
}
});
},
[stopVibrateAction],
);

return [vibrateAction, stopVibrateAction];
}

Expand Down

2 comments on commit f99082f

@vercel
Copy link

@vercel vercel bot commented on f99082f Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

taro-hooks – ./

taro-hooks-innocces.vercel.app
taro-hooks-git-main-innocces.vercel.app
taro-hooks-theta.vercel.app

@vercel
Copy link

@vercel vercel bot commented on f99082f Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

taro-hooks-h5 – ./

taro-hooks-h5-git-main-innocces.vercel.app
taro-hooks-h5-innocces.vercel.app
taro-hooks-h5-green.vercel.app

Please sign in to comment.