Skip to content

Commit

Permalink
bypass more empty case
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed Dec 4, 2024
1 parent 8efb033 commit 726e475
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/reshow-hooks/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.18.2",
"version": "0.18.3",
"name": "reshow-hooks",
"repository": {
"type": "git",
Expand Down
37 changes: 22 additions & 15 deletions packages/reshow-hooks/src/useSwipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ const defaultCallback = (/**@type DirectionType*/ _bDirection) => {};
* @property {function(DirectionType):void} callback=defaultCallback
* @property {number} startTime
* @property {boolean} bTracking
* @property {number} startPosX
* @property {number} startPosY
* @property {number} endPosX
* @property {number} endPosY
* @property {number?} startPosX
* @property {number?} startPosY
* @property {number?} endPosX
* @property {number?} endPosY
*/

const resetSwipState = {
startTime: 0,
bTracking: false,
startPosX: 0,
startPosY: 0,
endPosX: 0,
endPosY: 0,
startPosX: null,
startPosY: null,
endPosX: null,
endPosY: null,
};

/**
Expand Down Expand Up @@ -115,13 +115,24 @@ export const useSwipe = ({
let direction;
const now = new Date().getTime();
const deltaTime = now - startTime;
const deltaX = endPosX - startPosX;
const deltaY = endPosY - startPosY;
lastState.current = {
...lastState.current,
...resetSwipState,
};

/* work out what the movement was */
if (deltaTime > thresholdTime) {
if (
deltaTime > thresholdTime ||
null == endPosX ||
null == endPosY ||
null == startPosX ||
null == startPosY
) {
/* gesture too slow */
return;
} else {
const deltaX = endPosX - startPosX;
const deltaY = endPosY - startPosY;
if (
deltaX > thresholdDistance &&
Math.abs(deltaY) < thresholdDistance
Expand All @@ -145,10 +156,6 @@ export const useSwipe = ({
} else {
direction = null;
}
lastState.current = {
...lastState.current,
...resetSwipState,
};
if (null != direction) {
callback(direction);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/reshow-hooks/types/useSwipe.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export type UseSwipeState = {
callback: (arg0: DirectionType) => void;
startTime: number;
bTracking: boolean;
startPosX: number;
startPosY: number;
endPosX: number;
endPosY: number;
startPosX: number | null;
startPosY: number | null;
endPosX: number | null;
endPosY: number | null;
};

0 comments on commit 726e475

Please sign in to comment.