Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 3 additions & 8 deletions packages/shared/watchOnce/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ category: Watch

# watchOnce

`watch` that only triggers once.

::: tip

[Once Watcher](https://vuejs.org/guide/essentials/watchers.html#once-watchers) has been added to Vue [since 3.4](https://github.com/vuejs/core/pull/9034), use `watch(watchSource, callback, { once: true })` instead on supported versions.

:::
Shorthand for watching value with `{ once: true }`. Once the callback fires once, the watcher will be stopped.
See [Vue's docs](https://vuejs.org/guide/essentials/watchers.html#once-watchers) for full details.

## Usage

After the callback function has been triggered once, the watch will be stopped automatically.
Similar to `watch`, but with `{ once: true }`

```ts
import { watchOnce } from '@vueuse/core'
Expand Down
45 changes: 30 additions & 15 deletions packages/shared/watchOnce/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
import type { WatchCallback, WatchOptions, WatchSource, WatchStopHandle } from 'vue'
import type { MapOldSources, MapSources } from '../utils'
import { nextTick, watch } from 'vue'
import { watch } from 'vue'

// overloads
export function watchOnce<T extends Readonly<WatchSource<unknown>[]>, Immediate extends Readonly<boolean> = false>(source: [...T], cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>, options?: WatchOptions<Immediate>): WatchStopHandle
export function watchOnce<T extends Readonly<WatchSource<unknown>[]>>(
source: [...T],
cb: WatchCallback<MapSources<T>, MapOldSources<T, true>>,
options?: Omit<WatchOptions<true>, 'once'>
): WatchStopHandle

export function watchOnce<T, Immediate extends Readonly<boolean> = false>(sources: WatchSource<T>, cb: WatchCallback<T, Immediate extends true ? T | undefined : T>, options?: WatchOptions<Immediate>): WatchStopHandle
export function watchOnce<T>(
source: WatchSource<T>,
cb: WatchCallback<T, T | undefined>,
options?: Omit<WatchOptions<true>, 'once'>
): WatchStopHandle

// implementation
export function watchOnce<Immediate extends Readonly<boolean> = false>(
source: any,
cb: any,
options?: WatchOptions<Immediate>,
): WatchStopHandle {
const stop = watch(source, (...args) => {
nextTick(() => stop())
export function watchOnce<T extends object>(
source: T,
cb: WatchCallback<T, T | undefined>,
options?: Omit<WatchOptions<true>, 'once'>
): WatchStopHandle

return cb(...args)
}, options)

return stop
/**
* Shorthand for watching value with { once: true }
*
* @see https://vueuse.org/watchOnce
*/
export function watchOnce<T = any>(source: T, cb: any, options?: Omit<WatchOptions, 'once'>) {
return watch(
source as any,
cb,
{
...options,
once: true,
},
)
}
Comment on lines +29 to 38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function watchOnce<T = any>(source: T, cb: any, options?: Omit<WatchOptions, 'once'>) {
return watch(
source as any,
cb,
{
...options,
once: true,
},
)
}
export function watchOnce<T = any, Immediate extends Readonly<boolean> = false>(source: T | WatchSource<T>, cb: any, options?: Omit<WatchOptions<Immediate>, 'once'>) {
return watch(
source,
cb,
{
...options,
once: true,
},
)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OrbisK Why? To keep compat?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, and to be as close as possible to native watch

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OrbisK Shall we do the same with the other watch shortcuts I guess.

I wonder as well why we don't do rest parameters and typeof watch (with a tail-like helper like the ones in type-fest to overwrite just the last parameter) so we don't have to keep this up to date ever...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can do, but does this work with the generics and overloads?