Skip to content

Commit

Permalink
fix(rstream-gestures): remove duplicate MOVE events
Browse files Browse the repository at this point in the history
- add check if target element is `document.body`
- if so don't add temp mousemove listener
  • Loading branch information
postspectacular committed Jan 10, 2020
1 parent 9a3e216 commit 0c8da9b
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions packages/rstream-gestures/src/gesture-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ const endEvents = new Set(["mouseup", "touchend", "touchcancel"]);

const baseEvents = <const>["mousemove", "mousedown", "touchstart", "wheel"];

const tempEvents = <const>[
"touchend",
"touchcancel",
"touchmove",
"mouseup",
"mousemove"
];

const eventGestureTypeMap: IObjectOf<GestureType> = {
touchstart: GestureType.START,
touchmove: GestureType.DRAG,
Expand Down Expand Up @@ -105,6 +97,15 @@ export const gestureStream = (
let numTouches = 0;
let tempStreams: Stream<UIEvent>[] | undefined;

const isBody = el === document.body;
const tempEvents: UIEventID[] = [
"touchend",
"touchcancel",
"touchmove",
"mouseup"
];
!isBody && tempEvents.push("mousemove");

opts.preventContextMenu &&
el.addEventListener("contextmenu", (e) => e.preventDefault());

Expand All @@ -126,16 +127,17 @@ export const gestureStream = (
const b = el.getBoundingClientRect();

const getPos = (e: Touch | MouseEvent | WheelEvent) => {
const pos = [e.clientX, e.clientY];
let x = e.clientX;
let y = e.clientY;
if (opts.local) {
pos[0] -= b.left;
pos[1] -= b.top;
x -= b.left;
y -= b.top;
}
if (opts.scale) {
pos[0] *= dpr;
pos[1] *= dpr;
x *= dpr;
y *= dpr;
}
return pos;
return [x | 0, y | 0];
};

if (startEvents.has(etype)) {
Expand Down Expand Up @@ -165,7 +167,7 @@ export const gestureStream = (
eventSource(document.body, id, opts, "-temp")
);
stream.addAll(tempStreams);
stream.removeID("mousemove");
!isBody && stream.removeID("mousemove");
// console.log("add temp", [
// ...map((s) => s.id, stream.sources.keys())
// ]);
Expand All @@ -181,7 +183,7 @@ export const gestureStream = (
}
if (numTouches === 0) {
stream.removeAll(tempStreams!);
stream.add(eventSource(el, "mousemove", opts));
!isBody && stream.add(eventSource(el, "mousemove", opts));
tempStreams = undefined;
// console.log("remove temp", [
// ...map((s) => s.id, stream.sources.keys())
Expand Down

0 comments on commit 0c8da9b

Please sign in to comment.