@@ -152,6 +152,8 @@ if (typeof HTMLElement === 'function') {
152
152
private $$data = { } ;
153
153
private $$reflecting = false ;
154
154
private $$props_definition : Record < string , CustomElementPropDefinition > = { } ;
155
+ private $$listeners : Record < string , Function [ ] > = { } ;
156
+ private $$listener_unsubscribe_fns = new Map < Function , Function > ( ) ;
155
157
156
158
constructor (
157
159
private $$componentCtor : ComponentType ,
@@ -165,10 +167,26 @@ if (typeof HTMLElement === 'function') {
165
167
// We can't determine upfront if the event is a custom event or not, so we have to
166
168
// listen to both. If someone uses a custom event with the same name as a regular
167
169
// browser event, this fires twice - we can't avoid that.
168
- this . $$component ! . $on ( type , listener ) ;
170
+ this . $$listeners [ type ] = this . $$listeners [ type ] || [ ] ;
171
+ this . $$listeners [ type ] . push ( listener ) ;
172
+ if ( this . $$component ) {
173
+ const unsub = this . $$component ! . $on ( type , listener ) ;
174
+ this . $$listener_unsubscribe_fns . set ( listener , unsub ) ;
175
+ }
169
176
super . addEventListener ( type , listener , options ) ;
170
177
}
171
178
179
+ removeEventListener ( type : string , listener : any , options ?: any ) : void {
180
+ super . removeEventListener ( type , listener , options ) ;
181
+ if ( this . $$component ) {
182
+ const unsub = this . $$listener_unsubscribe_fns . get ( listener ) ;
183
+ if ( unsub ) {
184
+ unsub ( ) ;
185
+ this . $$listener_unsubscribe_fns . delete ( listener ) ;
186
+ }
187
+ }
188
+ }
189
+
172
190
async connectedCallback ( ) {
173
191
this . $$connected = true ;
174
192
if ( ! this . $$component ) {
@@ -228,6 +246,14 @@ if (typeof HTMLElement === 'function') {
228
246
}
229
247
}
230
248
} ) ;
249
+
250
+ for ( const type in this . $$listeners ) {
251
+ for ( const listener of this . $$listeners [ type ] ) {
252
+ const unsub = this . $$component ! . $on ( type , listener ) ;
253
+ this . $$listener_unsubscribe_fns . set ( listener , unsub ) ;
254
+ }
255
+ }
256
+ this . $$listeners = { } ;
231
257
}
232
258
}
233
259
0 commit comments