forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(watchAtMost): new function (vitest-dev#764)
Co-authored-by: webfansplz <> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
bc6c342
commit 7c4fefc
Showing
7 changed files
with
100 additions
and
0 deletions.
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,23 @@ | ||
--- | ||
category: Watch | ||
--- | ||
|
||
# watchAtMost | ||
|
||
`watch` with the number of times triggered. | ||
|
||
## Usage | ||
|
||
Similar to `watch` with an extra option `count` which set up the number of times the callback function is triggered. After the count is reached, the watch will be stopped automatically. | ||
|
||
```ts | ||
import { watchAtMost } from '@vueuse/core' | ||
|
||
watchAtMost( | ||
source, | ||
() => { console.log('trigger!') }, // triggered it at most 3 times | ||
{ | ||
count: 3, // the number of times triggered | ||
} | ||
) | ||
``` |
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 { ref, nextTick } from 'vue-demi' | ||
import { watchAtMost } from '.' | ||
|
||
describe('watchAtMost', () => { | ||
it('should work', async() => { | ||
const num = ref(0) | ||
const spy = jest.fn() | ||
|
||
const { count } = watchAtMost(num, spy, { | ||
count: 2, | ||
}) | ||
num.value = 1 | ||
await nextTick() | ||
num.value = 2 | ||
await nextTick() | ||
num.value = 3 | ||
await nextTick() | ||
expect(spy).toBeCalledTimes(2) | ||
expect(count.value).toBe(2) | ||
}) | ||
}) |
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,46 @@ | ||
import { Ref, WatchSource, ref, unref, WatchStopHandle, WatchCallback } from 'vue-demi' | ||
import { MapOldSources, MapSources, MaybeRef } from '../utils' | ||
import { WatchWithFilterOptions, watchWithFilter } from '../watchWithFilter' | ||
|
||
export interface WatchAtMostOptions<Immediate> extends WatchWithFilterOptions<Immediate> { | ||
count: MaybeRef<number> | ||
} | ||
|
||
export interface WatchAtMostReturn { | ||
stop: WatchStopHandle | ||
count: Ref<number> | ||
} | ||
|
||
// overlads | ||
export function watchAtMost<T extends Readonly<WatchSource<unknown>[]>, Immediate extends Readonly<boolean> = false>(source: T, cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>, options: WatchAtMostOptions<Immediate>): WatchAtMostReturn | ||
|
||
export function watchAtMost<T extends Readonly<WatchSource<unknown>[]>, Immediate extends Readonly<boolean> = false>(source: T, cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>, options: WatchAtMostOptions<Immediate>): WatchAtMostReturn | ||
|
||
export function watchAtMost<T, Immediate extends Readonly<boolean> = false>(sources: WatchSource<T>, cb: WatchCallback<T, Immediate extends true ? T | undefined : T>, options: WatchAtMostOptions<Immediate>): WatchAtMostReturn | ||
|
||
// implementation | ||
export function watchAtMost<Immediate extends Readonly<boolean> = false>( | ||
source: any, | ||
cb: any, | ||
options: WatchAtMostOptions<Immediate>, | ||
): WatchAtMostReturn { | ||
const { | ||
count, | ||
...watchOptions | ||
} = options | ||
|
||
const current = ref(0) | ||
|
||
const stop = watchWithFilter( | ||
source, | ||
(...args) => { | ||
current.value += 1 | ||
if (current.value >= unref(count)) | ||
stop() | ||
cb(...args) | ||
}, | ||
watchOptions, | ||
) | ||
|
||
return { count: current, stop } | ||
} |