github / github/auto-complete-element

Use abortcontroller to simplify connectedCallback

未关闭
#55 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
407
派生
57
平均合并
1 天 7 小时
30 天内合并 PR
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 摘要。