|
1 | 1 | import type { WatchCallback, WatchOptions, WatchSource, WatchStopHandle } from 'vue'
|
2 | 2 | import type { MapOldSources, MapSources } from '../utils'
|
3 |
| -import { nextTick, watch } from 'vue' |
| 3 | +import { watch } from 'vue' |
4 | 4 |
|
5 | 5 | // overloads
|
6 |
| -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 |
| 6 | +export function watchOnce<T extends Readonly<WatchSource<unknown>[]>>( |
| 7 | + source: [...T], |
| 8 | + cb: WatchCallback<MapSources<T>, MapOldSources<T, true>>, |
| 9 | + options?: Omit<WatchOptions<true>, 'once'> |
| 10 | +): WatchStopHandle |
7 | 11 |
|
8 |
| -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 |
| 12 | +export function watchOnce<T>( |
| 13 | + source: WatchSource<T>, |
| 14 | + cb: WatchCallback<T, T | undefined>, |
| 15 | + options?: Omit<WatchOptions<true>, 'once'> |
| 16 | +): WatchStopHandle |
9 | 17 |
|
10 |
| -// implementation |
11 |
| -export function watchOnce<Immediate extends Readonly<boolean> = false>( |
12 |
| - source: any, |
13 |
| - cb: any, |
14 |
| - options?: WatchOptions<Immediate>, |
15 |
| -): WatchStopHandle { |
16 |
| - const stop = watch(source, (...args) => { |
17 |
| - nextTick(() => stop()) |
| 18 | +export function watchOnce<T extends object>( |
| 19 | + source: T, |
| 20 | + cb: WatchCallback<T, T | undefined>, |
| 21 | + options?: Omit<WatchOptions<true>, 'once'> |
| 22 | +): WatchStopHandle |
18 | 23 |
|
19 |
| - return cb(...args) |
20 |
| - }, options) |
21 |
| - |
22 |
| - return stop |
| 24 | +/** |
| 25 | + * Shorthand for watching value with { once: true } |
| 26 | + * |
| 27 | + * @see https://vueuse.org/watchOnce |
| 28 | + */ |
| 29 | +export function watchOnce<T = any>(source: T, cb: any, options?: Omit<WatchOptions, 'once'>) { |
| 30 | + return watch( |
| 31 | + source as any, |
| 32 | + cb, |
| 33 | + { |
| 34 | + ...options, |
| 35 | + once: true, |
| 36 | + }, |
| 37 | + ) |
23 | 38 | }
|
0 commit comments