Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(watchOnce): allow watching until certain condition is met #3946

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 28 additions & 0 deletions packages/shared/watchOnce/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,31 @@ watchOnce(source, () => {
console.log('source changed!')
})
```

### Options: `until`

The option allows keeping the watcher until certain condition is met, then run the callback once and stop the watcher automatically.

```ts
import { getCurrentInstance } from 'vue'
import { useElementVisibility, watchOnce } from '@vueuse/core'

const instance = getCurrentInstance()

watchOnce(
[
// Check whether the element is within the viewport.
useElementVisibility(instance.proxy),
// Check whether the element is not visually hidden.
() => !!instance.vnode.el?.offsetParent,
],
() => {
// Triggers only once.
console.log('The component is in the viewport and visible')
},
{
immediate: true,
until: ([isInViewport, isVisible]) => isInViewport && isVisible,
},
)
```
14 changes: 14 additions & 0 deletions packages/shared/watchOnce/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@ describe('watchOnce', () => {
await nextTick()
expect(spy).toBeCalledTimes(1)
})

it('should watch until', async () => {
const num = ref(0)
const spy = vi.fn()

watchOnce(num, spy, { until: (value) => value === 2 })
num.value = 1
await nextTick()
num.value = 2
await nextTick()
num.value = 3
await nextTick()
expect(spy).toBeCalledTimes(2)
})
})
24 changes: 19 additions & 5 deletions packages/shared/watchOnce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,35 @@ import type { WatchCallback, WatchOptions, WatchSource, WatchStopHandle } from '
import { nextTick, watch } from 'vue-demi'
import type { MapOldSources, MapSources } from '../utils'

interface WatchOnceOptions<Callback extends WatchCallback, Immediate extends boolean> extends WatchOptions<Immediate> {
readonly until?: (...args: Parameters<Callback>) => boolean
}

// 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>[]>, Immediate extends Readonly<boolean> = false>(
source: [...T],
cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>,
options?: WatchOnceOptions<typeof cb, Immediate>,
): 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, Immediate extends Readonly<boolean> = false>(
sources: WatchSource<T>,
cb: WatchCallback<T, Immediate extends true ? T | undefined : T>,
options?: WatchOnceOptions<typeof cb, Immediate>,
): WatchStopHandle

// implementation
export function watchOnce<Immediate extends Readonly<boolean> = false>(
source: any,
cb: any,
options?: WatchOptions<Immediate>,
options?: WatchOnceOptions<typeof cb, Immediate>,
): WatchStopHandle {
const stop = watch(source, (...args) => {
nextTick(() => stop())
if (!options?.until || options.until(...args)) {
nextTick(() => stop())

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

return stop
Expand Down