Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document debouncing without a library #186

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/_guide/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ class FuzzySearchElement extends HTMLElement {
}
```

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:

```typescript
import {controller} from '@github/catalyst'

@controller
class FuzzySearchElement extends HTMLElement {

#searchAnimationFrame = 0
search(event: Event) {
clearAnimationFrame(this.#searchAnimationFrame)
this.#searchAnimationFrame = requestAnimationFrame(() => this.searchNow(event: Event))
}

searchNow(event: Event) {
const value = event.currentTarget.value
// This function is very computationally intensive, so we should run it as little as possible
this.filterAllItemsWithValue(value)
}

}
```

### Aborting Network Requests

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).
Expand Down