Skip to content

Commit edd8387

Browse files
committed
clean ups and attempt to simplify
1 parent bb9b6df commit edd8387

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

src/index.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export const DOWN = 'Down'
2222
const touchStart = 'touchstart'
2323
const touchMove = 'touchmove'
2424
const touchEnd = 'touchend'
25-
const touchEvents = [touchStart, touchMove, touchEnd]
2625
const mouseMove = 'mousemove'
2726
const mouseUp = 'mouseup'
2827

@@ -126,15 +125,11 @@ function getHandlers(set, props) {
126125

127126
const attachTouch = el => {
128127
if (el && el.addEventListener) {
129-
const attach = { [touchStart]: onStart, [touchMove]: onMove, [touchEnd]: onUp }
130-
touchEvents.forEach(e => el.addEventListener(e, attach[e]))
131-
return getCleanUpTouch(el, attach)
132-
}
133-
}
134-
135-
const getCleanUpTouch = (el, attached) => () => {
136-
if (el && el.removeEventListener) {
137-
touchEvents.forEach(e => el.removeEventListener(e, attached[e]))
128+
// attach touch event listeners and handlers
129+
const tls = [[touchStart, onStart], [touchMove, onMove], [touchEnd, onEnd]]
130+
tls.forEach(([e, h]) => el.addEventListener(e, h))
131+
// return properly scoped cleanup method for removing listeners
132+
return () => tls.forEach(([e, h]) => el.removeEventListener(e, h))
138133
}
139134
}
140135

@@ -146,44 +141,46 @@ function getHandlers(set, props) {
146141
// if the same DOM el as previous just return state
147142
if (state.el === el) return state
148143

149-
// store event attached DOM el for comparison, clean up, and re-attachment
150-
const newState = { ...state, el }
151-
144+
let addState = {}
152145
// if new DOM el clean up old DOM and reset cleanUpTouch
153146
if (state.el && state.el !== el && state.cleanUpTouch) {
154-
newState.cleanUpTouch = state.cleanUpTouch()
147+
state.cleanUpTouch()
148+
addState.cleanUpTouch = null
155149
}
156150
// only attach if we want to track touch
157-
if (state.props.trackTouch && newState.el) {
158-
newState.cleanUpTouch = attachTouch(newState.el)
151+
if (state.props.trackTouch && el) {
152+
addState.cleanUpTouch = attachTouch(el)
159153
}
160-
return newState
161-
})
162-
}
163-
164-
// set ref callback to attach touch event listeners
165-
const output = { ref: onRef }
166154

167-
// if track mouse attach mouse down listener
168-
if (props.trackMouse) {
169-
output.onMouseDown = onStart
155+
// store event attached DOM el for comparison, clean up, and re-attachment
156+
return { ...state, el, ...addState }
157+
})
170158
}
171159

172160
// update state, props, and handlers
173161
set(state => {
174-
const newState = { ...state, props }
162+
let addState = {}
175163
// clean up touch handlers if no longer tracking touches
176164
if (!props.trackTouch && state.cleanUpTouch) {
177-
newState.cleanUpTouch = state.cleanUpTouch()
165+
state.cleanUpTouch()
166+
addState.cleanUpTouch = null
178167
} else if (props.trackTouch && !state.cleanUpTouch) {
179168
// attach/re-attach touch handlers
180-
if (newState.el) {
181-
newState.cleanUpTouch = attachTouch(newState.el)
169+
if (state.el) {
170+
addState.cleanUpTouch = attachTouch(state.el)
182171
}
183172
}
184-
return newState
173+
return { ...state, props, ...addState }
185174
})
186175

176+
// set ref callback to attach touch event listeners
177+
const output = { ref: onRef }
178+
179+
// if track mouse attach mouse down listener
180+
if (props.trackMouse) {
181+
output.onMouseDown = onStart
182+
}
183+
187184
return output
188185
}
189186

0 commit comments

Comments
 (0)