Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/watchIgnorable.md
1---2category: Watch3alias: ignorableWatch4---56# watchIgnorable78Ignorable watch910## Usage1112Extended `watch` that returns extra `ignoreUpdates(updater)` and `ignorePrevAsyncUpdates()` to ignore particular updates to the source.1314```ts15import { watchIgnorable } from '@vueuse/core'16import { nextTick, shallowRef } from 'vue'1718const source = shallowRef('foo')1920const { stop, ignoreUpdates } = watchIgnorable(21source,22v => console.log(`Changed to ${v}!`),23)2425source.value = 'bar'26await nextTick() // logs: Changed to bar!2728ignoreUpdates(() => {29source.value = 'foobar'30})31await nextTick() // (nothing happened)3233source.value = 'hello'34await nextTick() // logs: Changed to hello!3536ignoreUpdates(() => {37source.value = 'ignored'38})39source.value = 'logged'4041await nextTick() // logs: Changed to logged!42```4344## WatchOptionFlush timing4546`watchIgnorable` accepts the same options as `watch` and uses the same defaults.47So, by default the composable works using `flush: 'pre'`.4849## `ignorePrevAsyncUpdates`5051This feature is only for async flush `'pre'` and `'post'`. If `flush: 'sync'` is used, `ignorePrevAsyncUpdates()` is a no-op as the watch will trigger immediately after each update to the source. It is still provided for sync flush so the code can be more generic.5253```ts54import { watchIgnorable } from '@vueuse/core'55import { nextTick, shallowRef } from 'vue'5657const source = shallowRef('foo')5859const { ignorePrevAsyncUpdates } = watchIgnorable(60source,61v => console.log(`Changed to ${v}!`),62)6364source.value = 'bar'65await nextTick() // logs: Changed to bar!6667source.value = 'good'68source.value = 'by'69ignorePrevAsyncUpdates()7071await nextTick() // (nothing happened)7273source.value = 'prev'74ignorePrevAsyncUpdates()75source.value = 'after'7677await nextTick() // logs: Changed to after!78```7980## Recommended Readings8182- [Ignorable Watch](https://patak.dev/vue/ignorable-watch.html) - by [@patak-dev](https://github.com/patak-dev)8384## Type Declarations8586```ts87export type IgnoredUpdater = (updater: () => void) => void88export type IgnoredPrevAsyncUpdates = () => void89export interface WatchIgnorableReturn {90ignoreUpdates: IgnoredUpdater91ignorePrevAsyncUpdates: IgnoredPrevAsyncUpdates92stop: WatchStopHandle93}94export declare function watchIgnorable<95T extends Readonly<MultiWatchSources>,96Immediate extends Readonly<boolean> = false,97>(98sources: [...T],99cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,100options?: WatchWithFilterOptions<Immediate>,101): WatchIgnorableReturn102export declare function watchIgnorable<103T,104Immediate extends Readonly<boolean> = false,105>(106source: WatchSource<T>,107cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,108options?: WatchWithFilterOptions<Immediate>,109): WatchIgnorableReturn110export declare function watchIgnorable<111T extends object,112Immediate extends Readonly<boolean> = false,113>(114source: T,115cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,116options?: WatchWithFilterOptions<Immediate>,117): WatchIgnorableReturn118/** @deprecated use `watchIgnorable` instead */119export declare const ignorableWatch: typeof watchIgnorable120```121