github / github/auto-complete-element

Use abortcontroller to simplify connectedCallback

Open
#55 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
407
Forks
57
Avg merge
1d 7h
Merged PRs (30d)
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()
}

}
```

Contributor guide

Open the contributing guide

Research direction

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.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.