You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_guide/patterns.md
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,6 +29,29 @@ class FuzzySearchElement extends HTMLElement {
29
29
}
30
30
```
31
31
32
+
Alternatively, if you'd like more precise control over the exact way debouncing happens (for example you'd like to make the debounce timeout dynamic, or sometimes call _without_ debouncing), you can have two methods following the pattern of `foo`/`fooNow` or `foo`/`fooSync`, where the non-suffixed method dispatches asynchronously to the `Now`/`Sync` suffixed method, a little like this:
// This function is very computationally intensive, so we should run it as little as possible
49
+
this.filterAllItemsWithValue(value)
50
+
}
51
+
52
+
}
53
+
```
54
+
32
55
### Aborting Network Requests
33
56
34
57
When making network requests using `fetch`, based on user input, you can cancel old requests as new ones come in. This is useful for performance as well as UI responsiveness, as old requests that aren't cancelled might complete later than newer ones, and causing the UI to jump around. Aborting network requests requires you to use [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) (a web platform feature).
@@ -70,3 +93,37 @@ class RemoveSearchElement extends HTMLElement {
70
93
}
71
94
}
72
95
```
96
+
97
+
### Registering global or many event listeners
98
+
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.
100
+
101
+
102
+
```typescript
103
+
@controller
104
+
classUnsavedChangesElementextendsHTMLElement {
105
+
106
+
#eventAbortController:AbortController|null=null
107
+
108
+
connectedCallback(event:Event) {
109
+
// Create the new AbortController and get the new signal
0 commit comments