Skip to content

Commit 9de0557

Browse files
authored
Merge pull request #189 from github/add-reuse-note-to-abortcontroller
Add note on AbortController reuse
2 parents ca46c29 + c92fad5 commit 9de0557

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

docs/_guide/patterns.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ class RemoveSearchElement extends HTMLElement {
9696

9797
### Registering global or many event listeners
9898

99-
Generally speaking, you'll want to use ["Actions"]({{ site.baseurl }}/guide/actions) to register event listeners with your Controller, but Actions only work for components nested within your Controller. It may also be necessary to listen for events on the Document, Window, or across well-known adjacent elements. We can manually call `addEventListener` for these types, including during the `connectedCallback` phase. Cleanup for `addEventListener` can be a bit error prone, but [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) can be useful here to pass a signal that the element is cleaning up:
99+
Generally speaking, you'll want to use ["Actions"]({{ site.baseurl }}/guide/actions) to register event listeners with your Controller, but Actions only work for components nested within your Controller. It may also be necessary to listen for events on the Document, Window, or across well-known adjacent elements. We can manually call `addEventListener` for these types, including during the `connectedCallback` phase. Cleanup for `addEventListener` can be a bit error prone, but [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) can be useful here to pass a signal that the element is cleaning up. AbortControllers should be created once per `connectedCallback`, as they are not re-usable, while Controllers _can_ be reused.
100100

101101

102102
```typescript
103103
@controller
104104
class UnsavedChangesElement extends HTMLElement {
105105

106-
#eventAbortController: AbortController|null
106+
#eventAbortController: AbortController|null = null
107107

108108
connectedCallback(event: Event) {
109109
// Create the new AbortController and get the new signal

0 commit comments

Comments
 (0)