Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 979c65f

Browse filesBrowse files
authored
fix(whenever): improve old value types (#5096)
1 parent ac2ef95 commit 979c65f
Copy full SHA for 979c65f

2 files changed

+47-4Lines changed: 47 additions & 4 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/shared/whenever/index.test.ts‎

Copy file name to clipboardExpand all lines: packages/shared/whenever/index.test.ts
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,43 @@ describe('whenever', () => {
102102

103103
vm.unmount()
104104
})
105+
106+
it('immediate', async () => {
107+
const vm = useSetup(() => {
108+
const number = shallowRef<number | null>(null)
109+
const lazyWatchCount = shallowRef(0)
110+
const eagerWatchCount = shallowRef(0)
111+
112+
whenever(number, (value, prevValue) => {
113+
lazyWatchCount.value++
114+
expectType<number>(value)
115+
expectType<number | null>(prevValue)
116+
}, { immediate: false })
117+
118+
whenever(number, (value, prevValue) => {
119+
eagerWatchCount.value++
120+
expectType<number>(value)
121+
expectType<number | null | undefined>(prevValue)
122+
}, { immediate: true })
123+
124+
const changeNumber = (v: number) => number.value = v
125+
126+
return { number, lazyWatchCount, eagerWatchCount, changeNumber }
127+
})
128+
129+
expect(toValue(vm.lazyWatchCount)).toBe(0)
130+
expect(toValue(vm.eagerWatchCount)).toBe(0)
131+
132+
vm.changeNumber(1)
133+
await nextTick()
134+
expect(toValue(vm.lazyWatchCount)).toBe(1)
135+
expect(toValue(vm.eagerWatchCount)).toBe(1)
136+
137+
vm.changeNumber(2)
138+
await nextTick()
139+
expect(toValue(vm.lazyWatchCount)).toBe(2)
140+
expect(toValue(vm.eagerWatchCount)).toBe(2)
141+
142+
vm.unmount()
143+
})
105144
})
Collapse file

‎packages/shared/whenever/index.ts‎

Copy file name to clipboardExpand all lines: packages/shared/whenever/index.ts
+8-4Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import type { WatchCallback, WatchOptions, WatchSource } from 'vue'
1+
import type { WatchCallback, WatchHandle, WatchOptions, WatchSource } from 'vue'
22
import { nextTick, watch } from 'vue'
33

4-
export interface WheneverOptions extends WatchOptions {
4+
type Truthy<T> = T extends false | null | undefined ? never : T
5+
6+
export interface WheneverOptions<Immediate = boolean> extends WatchOptions<Immediate> {
57
/**
68
* Only trigger once when the condition is met
79
*
@@ -17,14 +19,16 @@ export interface WheneverOptions extends WatchOptions {
1719
*
1820
* @see https://vueuse.org/whenever
1921
*/
20-
export function whenever<T>(source: WatchSource<T | false | null | undefined>, cb: WatchCallback<T>, options?: WheneverOptions) {
22+
export function whenever<T>(source: WatchSource<T>, cb: WatchCallback<Truthy<T>, T | undefined>, options?: WheneverOptions<true>): WatchHandle
23+
export function whenever<T>(source: WatchSource<T>, cb: WatchCallback<Truthy<T>, T>, options?: WheneverOptions<false>): WatchHandle
24+
export function whenever<T, Immediate extends Readonly<boolean> = false>(source: WatchSource<T>, cb: WatchCallback<Truthy<T>, T | undefined>, options?: WheneverOptions<Immediate>) {
2125
const stop = watch(
2226
source,
2327
(v, ov, onInvalidate) => {
2428
if (v) {
2529
if (options?.once)
2630
nextTick(() => stop())
27-
cb(v, ov, onInvalidate)
31+
cb(v as Truthy<T>, ov, onInvalidate)
2832
}
2933
},
3034
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.