Skip to content

Commit

Permalink
fix#71 fix touch event listener and throttle touch move callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuyz0112 committed Jun 17, 2019
1 parent 52ec5d4 commit d43aa97
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
28 changes: 20 additions & 8 deletions src/record/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getWindowWidth,
isBlocked,
isAncestorRemoved,
isTouchEvent,
} from '../utils';
import {
mutationCallBack,
Expand Down Expand Up @@ -271,7 +272,7 @@ function initMutationObserver(
return observer;
}

function initMousemoveObserver(cb: mousemoveCallBack): listenerHandler {
function initMoveObserver(cb: mousemoveCallBack): listenerHandler {
let positions: mousePosition[] = [];
let timeBaseline: number | null;
const wrappedCb = throttle(() => {
Expand All @@ -285,9 +286,12 @@ function initMousemoveObserver(cb: mousemoveCallBack): listenerHandler {
positions = [];
timeBaseline = null;
}, 500);
const updatePosition = throttle<MouseEvent>(
const updatePosition = throttle<MouseEvent | TouchEvent>(
evt => {
const { clientX, clientY, target } = evt;
const { target } = evt;
const { clientX, clientY } = isTouchEvent(evt)
? evt.changedTouches[0]
: evt;
if (!timeBaseline) {
timeBaseline = Date.now();
}
Expand All @@ -304,7 +308,13 @@ function initMousemoveObserver(cb: mousemoveCallBack): listenerHandler {
trailing: false,
},
);
return on('mousemove', updatePosition);
const handlers = [
on('mousemove', updatePosition),
on('touchmove', updatePosition),
];
return () => {
handlers.forEach(h => h());
};
}

function initMouseInteractionObserver(
Expand All @@ -313,12 +323,14 @@ function initMouseInteractionObserver(
): listenerHandler {
const handlers: listenerHandler[] = [];
const getHandler = (eventKey: keyof typeof MouseInteractions) => {
return (event: MouseEvent) => {
return (event: MouseEvent | TouchEvent) => {
if (isBlocked(event.target as Node, blockClass)) {
return;
}
const id = mirror.getId(event.target as INode);
const { clientX, clientY } = event;
const { clientX, clientY } = isTouchEvent(event)
? event.changedTouches[0]
: event;
cb({
type: MouseInteractions[eventKey],
id,
Expand All @@ -328,7 +340,7 @@ function initMouseInteractionObserver(
};
};
Object.keys(MouseInteractions)
.filter(key => Number.isNaN(Number(key)))
.filter(key => Number.isNaN(Number(key)) && !key.endsWith('_Departed'))
.forEach((eventKey: keyof typeof MouseInteractions) => {
const eventName = eventKey.toLowerCase();
const handler = getHandler(eventKey);
Expand Down Expand Up @@ -499,7 +511,7 @@ export default function initObservers(o: observerParam): listenerHandler {
o.inlineStylesheet,
o.maskAllInputs,
);
const mousemoveHandler = initMousemoveObserver(o.mousemoveCb);
const mousemoveHandler = initMoveObserver(o.mousemoveCb);
const mouseInteractionHandler = initMouseInteractionObserver(
o.mouseInteractionCb,
o.blockClass,
Expand Down
2 changes: 2 additions & 0 deletions src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ export class Replayer {
}
break;
case MouseInteractions.Click:
case MouseInteractions.TouchStart:
case MouseInteractions.TouchEnd:
/**
* Click has no visual impact when replaying and may
* trigger navigation when apply to an <a> link.
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export enum MouseInteractions {
Focus,
Blur,
TouchStart,
TouchMove,
TouchMove_Departed, // we will start a separate observer for touch move event
TouchEnd,
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@ export function isAncestorRemoved(target: INode): boolean {
}
return isAncestorRemoved((target.parentNode as unknown) as INode);
}

export function isTouchEvent(
event: MouseEvent | TouchEvent,
): event is TouchEvent {
return Boolean((event as TouchEvent).changedTouches);
}

0 comments on commit d43aa97

Please sign in to comment.