# useDebounce()

Delays updating the parameters by [debouncing](https://css-tricks.com/debouncing-throttling-explained-examples/).

Useful to avoid spamming network requests when parameters might change quickly (like a typeahead field).

> **Tip**
>
> `useDebounce()` returns [refs](https://vuejs.org/api/reactivity-core.html#ref), so the debounced
> value can be passed directly to other composables or components. `isPending` is true from the moment
> the input changes until the debounced value is updated.

## Usage

```ts title="resources/Issue"
import { Entity, RestEndpoint } from '@data-client/rest';

export class Issue extends Entity {
  id = 0;
  number = 0;
  title = '';
  user = { login: '', avatar_url: '' };

  static key = 'Issue';
}

export const searchIssues = new RestEndpoint({
  urlPrefix: 'https://api.github.com',
  path: '/search/issues',
  searchParams: {} as { q: string },
  schema: { items: [Issue] },
});
```

```html title="IssueList.vue"
<script setup lang="ts">
  import { useSuspense } from '@data-client/vue';
  import { searchIssues } from './resources/Issue';

  const props = defineProps<{
    search: string;
    /** still the last search's: the query changed since */
    stale: boolean;
  }>();
  const response = await useSuspense(searchIssues, () => ({
    q: `${props.search} repo:facebook/react is:issue`,
  }));
</script>

<template>
  <p v-if="!response.items.length" class="meta">No issues match</p>
  <div v-else class="list" :aria-busy="stale">
    <div
      v-for="issue in response.items"
      :key="issue.pk()"
      class="listItem"
    >
      <Avatar :src="issue.user.avatar_url" />
      <div>
        <h4>{{ issue.title }}</h4>
        <small>#{{ issue.number }} by {{ issue.user.login }}</small>
      </div>
    </div>
  </div>
</template>
```

```html title="SearchIssues.vue"
<script setup lang="ts">
  import { onErrorCaptured, ref } from 'vue';
  import { useDebounce } from '@data-client/vue';
  import IssueList from './IssueList.vue';

  const query = ref('');
  const [debouncedQuery, isPending] = useDebounce(query, 200);

  const input = ref<HTMLInputElement>();
  function clear() {
    query.value = '';
    input.value?.focus();
  }

  const error = ref<Error | null>(null);
  onErrorCaptured(e => {
    error.value = e;
    return false;
  });
</script>

<template>
  <header class="appHeader">
    <h1 class="appTitle">Issues</h1>
  </header>
  <label class="search" :aria-busy="isPending">
    <input
      ref="input"
      v-model="query"
      type="search"
      aria-label="Search issues"
      placeholder="Search issues"
    />
    <button
      type="button"
      class="clear"
      aria-label="Clear search"
      @click="clear"
    />
  </label>
  <ErrorMessage
    v-if="error"
    :error="error"
    :resetErrorBoundary="() => (error = null)"
  />
  <Suspense v-else>
    <IssueList :search="debouncedQuery" :stale="isPending" />
    <template #fallback><Loading /></template>
  </Suspense>
</template>
```

## Types

```typescript
function useDebounce<T>(
  value: T | Ref<T>,
  delay: number,
  updatable?: boolean | Ref<boolean>,
): [Ref<T>, Ref<boolean>];
```

`value` and `updatable` can be plain values or [refs](https://vuejs.org/api/reactivity-core.html#ref).
Returns a tuple of `[debouncedValue, isPending]` refs. When `updatable` is `false`, the debounced
value stops updating and `isPending` resets to `false`.
