@@ -22,7 +22,6 @@ export const DOWN = 'Down'
2222const touchStart = 'touchstart'
2323const touchMove = 'touchmove'
2424const touchEnd = 'touchend'
25- const touchEvents = [ touchStart , touchMove , touchEnd ]
2625const mouseMove = 'mousemove'
2726const 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