github / github/auto-complete-element

Use abortcontroller to simplify connectedCallback

オープン
#55 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
JavaScript
スター
407
フォーク
57
平均マージ
1日 7時間
マージ済み PR(30日)
5

説明

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

}
```

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript
領域
frontend
issue の種類
リファクタリング
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。