github / github/auto-complete-element
Use abortcontroller to simplify connectedCallback
- Lenguaje dominante
- JavaScript
- Estrellas
- 407
- Forks
- 57
- Merge medio
- 1 d 7 h
- PR fusionados (30 d)
- 5
Descripción
Per https://github.com/github/auto-complete-element/pull/53#discussion_r761014561, we register many event handlers like `addEventlistener('foo', this.bar = this.bar.bind(bar))`, but rather than calling function bind we can use `handleEvent` and rather than calling `removeEventListener` a bunch, we can store an abort controller per-instance (e.g. with a private field) and abort during `disconnectedCallback`.
The pattern would look as follows:
```ts
class MyElement extends HTMElement {
#ctl = new AbortController()
connectedCallback() {
this.input.addEventListener('foo', this, { signal: this.#ctl.signal })
// many more events here...
}
handleEvent(event) {
if (event.type === 'foo' && event.currentTarget === this.input) {
// do things
}
}
disconnectedCallback() {
this.#ctl.abort()
}
}
```
Guía de contribución
Línea de trabajo
Locate the custom element's connectedCallback and existing event listener setup, then inspect its disconnectedCallback and the handlers currently bound or removed individually. Replace that lifecycle wiring with handleEvent and an instance AbortController as described, and verify that listeners are registered and cleaned up correctly when the element disconnects.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript
- Área
- frontend
- Tipo de issue
- Refactorización
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100