Skip to content

Commit b96f8be

Browse files
committed
Refactoring and comments
1 parent cce8770 commit b96f8be

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

index.js

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react'
2-
32
const noop = () => {}
43

54
const stateKeys = { onDrag: 'drag', onHover: 'move', onMove: 'move', onPinch: 'pinch', onScroll: 'scroll', onWheel: 'wheel' }
@@ -135,15 +134,16 @@ export function useGesture(props, config) {
135134

136135
const getKinematics = (mov_x, mov_y, event, stateKey, isDelta = false) => {
137136
const lastLocal = state.current[stateKey].lastLocal || initialState[stateKey].lastLocal
138-
const { xy, initial, delta, time, transform } = state.current[stateKey]
137+
const { xy, initial, time, transform } = state.current[stateKey]
139138
const t = transform || event.transform || configRef.current.transform
140139

141140
const delta_t = event.timeStamp - time
142141

143142
const x = isDelta ? mov_x + xy[0] : mov_x
144143
const y = isDelta ? mov_y + xy[1] : mov_y
145-
const delta_x = t.x(isDelta ? mov_x + delta[0] : x - initial[0])
146-
const delta_y = t.y(isDelta ? mov_y + delta[1] : y - initial[1])
144+
145+
const delta_x = t.x(x - initial[0])
146+
const delta_y = t.y(y - initial[1])
147147

148148
const x_dist = t.x(x - xy[0])
149149
const y_dist = t.y(y - xy[1])
@@ -393,6 +393,7 @@ export function useGesture(props, config) {
393393

394394
const onWheel = event => {
395395
if (!configRef.current.enabled || !configRef.current.wheel) return
396+
396397
clearTimeout(timeouts.current.wheel)
397398
timeouts.current.wheel = setTimeout(onWheelEnd, 100)
398399
const { mov_x, mov_y, ...rest } = getWheelEventData(event)
@@ -435,36 +436,36 @@ export function useGesture(props, config) {
435436
const listeners = {}
436437

437438
if (actions.has('onMove')) {
438-
pushEventProp(listeners, pointerEvents ? 'onPointerMove' : 'onMouseMove', onMove)
439+
pushInKeys(listeners, pointerEvents ? 'onPointerMove' : 'onMouseMove', onMove)
439440
}
440441

441442
if (actions.has('onDrag')) {
442443
if (pointerEvents) {
443-
pushEventProp(listeners, 'onPointerDown', onDragStart)
444-
pushEventProp(listeners, 'onPointerMove', onDragMove)
445-
pushEventProp(listeners, ['onPointerUp', 'onPointerCancel'], onDragEnd)
444+
pushInKeys(listeners, 'onPointerDown', onDragStart)
445+
pushInKeys(listeners, 'onPointerMove', onDragMove)
446+
pushInKeys(listeners, ['onPointerUp', 'onPointerCancel'], onDragEnd)
446447
} else {
447-
pushEventProp(listeners, ['onMouseDown', 'onTouchStart'], onDragStart)
448+
pushInKeys(listeners, ['onMouseDown', 'onTouchStart'], onDragStart)
448449
}
449450
}
450451

451452
if (actions.has('onPinch')) {
452-
pushEventProp(listeners, 'onTouchStart', onPinchStart)
453-
pushEventProp(listeners, 'onTouchMove', onPinchMove)
454-
pushEventProp(listeners, ['onTouchEnd', 'onTouchCancel'], onPinchEnd)
453+
pushInKeys(listeners, 'onTouchStart', onPinchStart)
454+
pushInKeys(listeners, 'onTouchMove', onPinchMove)
455+
pushInKeys(listeners, ['onTouchEnd', 'onTouchCancel'], onPinchEnd)
455456
}
456457

457458
if (actions.has('onHover')) {
458-
pushEventProp(listeners, pointerEvents ? 'onPointerEnter' : 'onMouseEnter', onEnter)
459-
pushEventProp(listeners, pointerEvents ? 'onPointerLeave' : 'onMouseLeave', onLeave)
459+
pushInKeys(listeners, pointerEvents ? 'onPointerEnter' : 'onMouseEnter', onEnter)
460+
pushInKeys(listeners, pointerEvents ? 'onPointerLeave' : 'onMouseLeave', onLeave)
460461
}
461462

462463
if (actions.has('onScroll')) {
463-
pushEventProp(listeners, 'onScroll', onScroll)
464+
pushInKeys(listeners, 'onScroll', onScroll)
464465
}
465466

466467
if (actions.has('onWheel')) {
467-
pushEventProp(listeners, 'onWheel', onWheel)
468+
pushInKeys(listeners, 'onWheel', onWheel)
468469
}
469470

470471
if (domTarget) {
@@ -482,13 +483,17 @@ export function useGesture(props, config) {
482483

483484
/*** UTILS ***/
484485

485-
const chain = (...fns) => (...args) => fns.forEach(a => a(...args))
486-
const pushEventProp = (l, keys, fn) => {
486+
// returns a function that chains all functions given as parameters
487+
const chain = (...fns) => (...args) => fns.forEach(fn => fn(...args))
488+
489+
// utility function that pushes values in object keys which are in fact arrays
490+
const pushInKeys = (obj, keys, value) => {
487491
if (!Array.isArray(keys)) keys = [keys]
488-
keys.forEach(key => (l[key] = l[key] ? [...l[key], fn] : [fn]))
492+
keys.forEach(key => (obj[key] = obj[key] ? [...obj[key], value] : [value]))
489493
}
490494

491-
const clearTimeouts = timeouts => Object.values(timeouts).forEach(clearTimeout)
495+
// clears timeouts in keys
496+
const clearTimeouts = timeoutsObj => Object.values(timeoutsObj).forEach(clearTimeout)
492497

493498
const setListeners = add => (el, listeners, options) => {
494499
const action = add ? 'addEventListener' : 'removeEventListener'

0 commit comments

Comments
 (0)