Skip to content

Commit 3f43b58

Browse files
authored
document debouncing without a library
1 parent 51f3530 commit 3f43b58

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

docs/_guide/patterns.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ class FuzzySearchElement extends HTMLElement {
2929
}
3030
```
3131

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:
33+
34+
```typescript
35+
import {controller} from '@github/catalyst'
36+
37+
@controller
38+
class FuzzySearchElement extends HTMLElement {
39+
40+
#searchAnimationFrame = 0
41+
search(event: Event) {
42+
clearAnimationFrame(this.#searchAnimationFrame)
43+
this.#searchAnimationFrame = requestAnimationFrame(() => this.searchNow(event: Event))
44+
}
45+
46+
searchNow(event: Event) {
47+
const value = event.currentTarget.value
48+
// This function is very computationally intensive, so we should run it as little as possible
49+
this.filterAllItemsWithValue(value)
50+
}
51+
52+
}
53+
```
54+
3255
### Aborting Network Requests
3356

3457
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).

0 commit comments

Comments
 (0)