11import type { ReactiveController , ReactiveControllerHost } from 'lit' ;
22import type { Ref } from 'lit/directives/ref.js' ;
3+ import { createAbortHandle } from '../abort-handler.js' ;
34
45const Events = [
56 'pointerdown' ,
@@ -72,7 +73,9 @@ export class SwipeEvent extends Event {
7273
7374class GesturesController extends EventTarget implements ReactiveController {
7475 private readonly _host : ReactiveControllerHost & HTMLElement ;
75- private _ref ?: Ref < HTMLElement > ;
76+ private readonly _ref ?: Ref < HTMLElement > ;
77+ private readonly _abortHandle = createAbortHandle ( ) ;
78+
7679 private _options : GesturesOptions = {
7780 thresholdDistance : 100 ,
7881 thresholdTime : 500 ,
@@ -90,7 +93,7 @@ class GesturesController extends EventTarget implements ReactiveController {
9093 }
9194
9295 /** Get the current configuration object */
93- public get options ( ) {
96+ public get options ( ) : GesturesOptions {
9497 return this . _options ;
9598 }
9699
@@ -114,44 +117,50 @@ class GesturesController extends EventTarget implements ReactiveController {
114117 type : SwipeEvents ,
115118 callback : ( event : SwipeEvent ) => void ,
116119 options ?: AddEventListenerOptions
117- ) {
120+ ) : this {
118121 const bound = callback . bind ( this . _host ) as EventListener ;
119122
120123 this . addEventListener ( type , bound , options ) ;
121124 return this ;
122125 }
123126
124- public handleEvent ( event : PointerEvent ) {
127+ /** @internal */
128+ public handleEvent ( event : PointerEvent ) : void {
125129 if ( this . _options . touchOnly && event . pointerType === 'mouse' ) {
126130 return ;
127131 }
132+
128133 switch ( event . type ) {
129134 case 'pointerdown' :
130- return this . _handlePointerDown ( event ) ;
135+ this . _handlePointerDown ( event ) ;
136+ break ;
131137 case 'pointermove' :
132- return this . _handlePointerMove ( event ) ;
138+ this . _handlePointerMove ( event ) ;
139+ break ;
133140 case 'lostpointercapture' :
134141 case 'pointercancel' :
135- return this . _handleLostPointerCapture ( event ) ;
142+ this . _handleLostPointerCapture ( event ) ;
136143 }
137144 }
138145
139- public async hostConnected ( ) {
140- await this . _host . updateComplete ;
146+ /** @internal */
147+ public hostConnected ( ) : void {
148+ const { signal } = this . _abortHandle ;
141149
142- for ( const event of Events ) {
143- this . _element . addEventListener ( event , this , { passive : true } ) ;
144- }
150+ this . _host . updateComplete . then ( ( ) => {
151+ for ( const event of Events ) {
152+ this . _element . addEventListener ( event , this , { passive : true , signal } ) ;
153+ }
154+ } ) ;
145155 }
146156
147- public hostDisconnected ( ) {
148- for ( const event of Events ) {
149- this . _element . removeEventListener ( event , this ) ;
150- }
157+ /** @internal */
158+ public hostDisconnected ( ) : void {
159+ this . _abortHandle . abort ( ) ;
151160 }
152161
153162 /** Updates the configuration of the controller */
154- public updateOptions ( options : Omit < GesturesOptions , 'ref' > ) {
163+ public updateOptions ( options : Omit < GesturesOptions , 'ref' > ) : void {
155164 Object . assign ( this . _options , options ) ;
156165 }
157166
0 commit comments