@@ -7,12 +7,6 @@ export default (node, { y: yi = 0, step = scrollStep, maxSteps = Infinity }) =>
7
7
const updateY = ( val ) => {
8
8
y = Math . max ( 0 , Math . min ( maxSteps * step , val ) ) ;
9
9
} ;
10
- const wheel = ( { deltaY } ) => updateY ( y + deltaY ) ;
11
- const touchstart = ( { touches : [ { pageY } ] } ) => ( lastTouch = pageY ) ;
12
- const touchmove = ( { touches : [ { pageY } ] } ) => {
13
- updateY ( y - pageY - lastTouch ) ;
14
- lastTouch = pageY ;
15
- } ;
16
10
17
11
const emitY = ( ) => {
18
12
if ( Math . round ( y / step ) === Math . round ( yi / step ) ) return ;
@@ -27,16 +21,30 @@ export default (node, { y: yi = 0, step = scrollStep, maxSteps = Infinity }) =>
27
21
) ;
28
22
} ;
29
23
30
- node . addEventListener ( 'wheel' , ( evt ) => {
31
- wheel ( evt ) ;
24
+ const wheelListener = ( { deltaY } ) => {
25
+ updateY ( y + deltaY ) ;
32
26
emitY ( ) ;
33
- } ) ;
34
- node . addEventListener ( 'touchstart' , ( evt ) => {
35
- touchstart ( evt ) ;
27
+ } ;
28
+ const touchstartListener = ( { touches : [ { pageY } ] } ) => {
29
+ lastTouch = pageY ;
36
30
emitY ( ) ;
37
- } ) ;
38
- node . addEventListener ( 'touchmove' , ( evt ) => {
39
- touchmove ( evt ) ;
31
+ } ;
32
+ const touchmoveListener = ( { touches : [ { pageY } ] } ) => {
33
+ updateY ( y - ( pageY - lastTouch ) ) ;
34
+ lastTouch = pageY ;
40
35
emitY ( ) ;
41
- } ) ;
36
+ } ;
37
+
38
+ node . addEventListener ( 'wheel' , wheelListener ) ;
39
+ node . addEventListener ( 'touchstart' , touchstartListener ) ;
40
+ node . addEventListener ( 'touchmove' , touchmoveListener ) ;
41
+
42
+ return {
43
+ destroy ( ) {
44
+ console . log ( 'cleaning up' ) ;
45
+ node . removeEventListener ( 'wheel' , wheelListener ) ;
46
+ node . removeEventListener ( 'touchstart' , touchstartListener ) ;
47
+ node . removeEventListener ( 'touchmove' , touchmoveListener ) ;
48
+ }
49
+ } ;
42
50
} ;
0 commit comments