github / github/auto-complete-element

Use abortcontroller to simplify connectedCallback

Ouverte
#55 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
JavaScript
Étoiles
407
Forks
57
Merge moyen
1 j 7 h
PR mergées (30 j)
5

Description

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()
}

}
```

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript
Domaine
frontend
Type d'issue
Refactorisation
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.