Skip to content

Commit 5053890

Browse files
committed
fix: fixes [pmndrs#149](pmndrs#149) when setting state
1 parent dafceb1 commit 5053890

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/recognizers/DragRecognizer.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,28 @@ const FILTER_REPEATED_EVENTS_DELAY = 200
1212

1313
export default class DragRecognizer extends CoordinatesRecognizer<'drag'> {
1414
ingKey = 'dragging' as IngKey
15-
isTouch = false
1615
wasTouch = false
1716

1817
constructor(controller: Controller, args: any[]) {
1918
super('drag', controller, args)
2019
}
2120

21+
private isEventTypeTouch = (type?: string) => !!type && type.indexOf('touch') === 0
22+
2223
private dragShouldStart = (event: UseGestureEvent) => {
2324
const { touches } = getGenericEventData(event)
24-
25+
const { _lastEventType } = this.state
2526
/**
2627
* This tries to filter out mouse events triggered by touch screens
2728
* */
28-
29-
this.isTouch = !!touches
30-
3129
// If the previous gesture was touch-based, and the current one is mouse based,
3230
// this means that we might be dealing with mouse simulated events if they're close to
3331
// each other. We're only doing this check when we're not using pointer events.
34-
if (!this.controller.config.pointer && this.wasTouch && !this.isTouch) {
32+
if (
33+
!this.controller.config.pointer &&
34+
this.isEventTypeTouch(_lastEventType) &&
35+
!this.isEventTypeTouch(event.type)
36+
) {
3537
const delay = Math.abs(event.timeStamp - this.state.startTime)
3638
if (delay < FILTER_REPEATED_EVENTS_DELAY) return false
3739
}
@@ -50,9 +52,9 @@ export default class DragRecognizer extends CoordinatesRecognizer<'drag'> {
5052
if (currentTarget && pointerId) currentTarget.releasePointerCapture(pointerId)
5153
}
5254

53-
private setListeners = () => {
55+
private setListeners = (isTouch: boolean) => {
5456
this.removeWindowListeners()
55-
const dragListeners: [string, Fn][] = this.isTouch
57+
const dragListeners: [string, Fn][] = isTouch
5658
? [
5759
['touchmove', this.onDragChange],
5860
['touchend', this.onDragEnd],
@@ -68,8 +70,8 @@ export default class DragRecognizer extends CoordinatesRecognizer<'drag'> {
6870
onDragStart = (event: UseGestureEvent): void => {
6971
if (!this.dragShouldStart(event)) return
7072
// if pointers events
71-
else this.setListeners()
7273
if (this.controller.config.pointer) this.setPointers(event as PointerEvent)
74+
else this.setListeners(this.isEventTypeTouch(event.type))
7375

7476
if (this.config.delay > 0) {
7577
this.state._delayedEvent = true
@@ -178,8 +180,6 @@ export default class DragRecognizer extends CoordinatesRecognizer<'drag'> {
178180
clean = (): void => {
179181
super.clean()
180182
this.state._delayedEvent = false
181-
this.wasTouch = this.isTouch
182-
this.isTouch = false
183183

184184
if (this.controller.config.pointer) this.removePointers()
185185
}

src/recognizers/Recognizer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ export default abstract class Recognizer<T extends StateKey> {
113113
* @returns - the generic gesture payload
114114
*/
115115
protected getGenericPayload(event: UseGestureEvent, isStartEvent?: boolean) {
116-
const { timeStamp } = event
116+
const { timeStamp, type } = event
117117
const { values, startTime } = this.state
118118

119119
return {
120+
_lastEventType: type,
120121
event,
121122
timeStamp,
122123
elapsedTime: isStartEvent ? 0 : timeStamp - startTime!,

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ export interface CommonGestureState {
217217
_intentional: [FalseOrNumber, FalseOrNumber]
218218
_movement: Vector2
219219
_initial: Vector2
220+
_lastEventType?: string
220221
event?: UseGestureEvent
221-
currentTarget?: EventTarget | null
222+
currentTarget?: (EventTarget & Element) | null
222223
pointerId?: number | null
223224
values: Vector2
224225
velocities: Vector2

src/utils/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export function getInitialState(): State {
99
_intentional: [false, false],
1010
_movement: [0, 0],
1111
_initial: [0, 0],
12+
_lastEventType: undefined,
1213
event: undefined,
1314
// currentTarget: undefined,
1415
// pointerId: undefined,

0 commit comments

Comments
 (0)