-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(watchOnce): use vue's native once behaviour #4750
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
) | ||
} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we need to, but maybe we want to overload like vue does:
https://github.com/vuejs/core/blob/1f98a9c493d01c21befa90107f0593bc92a58932/packages/runtime-core/src/apiWatch.ts#L87-L140
There was a problem hiding this comment.
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 intype-fest
to overwrite just the last parameter) so we don't have to keep this up to date ever...There was a problem hiding this comment.
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?