-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
36 lines (34 loc) · 829 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { WatchCallback, WatchOptions, WatchSource } from 'vue-demi'
import { nextTick, watch } from 'vue-demi'
export interface WheneverOptions extends WatchOptions {
/**
* Only trigger once when the condition is met
*
* Override the `once` option in `WatchOptions`
*
* @default false
*/
once?: boolean
}
/**
* Shorthand for watching value to be truthy
*
* @see https://vueuse.org/whenever
*/
export function whenever<T>(source: WatchSource<T | false | null | undefined>, cb: WatchCallback<T>, options?: WheneverOptions) {
const stop = watch(
source,
(v, ov, onInvalidate) => {
if (v) {
if (options?.once)
nextTick(() => stop())
cb(v, ov, onInvalidate)
}
},
{
...options,
once: false,
} as WatchOptions,
)
return stop
}