forked from vitest-dev/vitest
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(watchOnce): new function (vitest-dev#765)
* feat(watchOnce): new Function * chore(watchOnce): improve code * chore: simplify Co-authored-by: webfansplz <> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
9f19c15
commit 098639b
Showing
7 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
category: Watch | ||
--- | ||
|
||
# watchOnce | ||
|
||
`watch` that only triggers once. | ||
|
||
## Usage | ||
|
||
After the callback function has been triggered once, the watch will be stopped automatically. | ||
|
||
```ts | ||
import { watchOnce } from '@vueuse/core' | ||
|
||
watchOnce(source, () => { | ||
// triggers only once | ||
console.log('source changed!') | ||
}) | ||
``` |
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ref, nextTick } from 'vue-demi' | ||
import { watchOnce } from '.' | ||
|
||
describe('watchOnce', () => { | ||
it('should work', async() => { | ||
const num = ref(0) | ||
const spy = jest.fn() | ||
|
||
watchOnce(num, spy) | ||
num.value = 1 | ||
await nextTick() | ||
num.value = 2 | ||
await nextTick() | ||
expect(spy).toBeCalledTimes(1) | ||
}) | ||
}) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { WatchSource, WatchCallback, WatchOptions, watch } from 'vue-demi' | ||
import { MapOldSources, MapSources } from '../utils' | ||
|
||
// overlads | ||
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>): void | ||
|
||
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>): void | ||
|
||
export function watchOnce<T, Immediate extends Readonly<boolean> = false>(sources: WatchSource<T>, cb: WatchCallback<T, Immediate extends true ? T | undefined : T>, options?: WatchOptions<Immediate>): void | ||
|
||
// implementation | ||
export function watchOnce<Immediate extends Readonly<boolean> = false>( | ||
source: any, | ||
cb: any, | ||
options?: WatchOptions<Immediate>, | ||
): void { | ||
const stop = watch(source, (...args) => { | ||
stop() | ||
return cb(...args) | ||
}, options) | ||
} |