-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
feat(watchPausable): add options.initialState
to control the initial active value
#4533
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
Conversation
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
…l active value fixes vueuse#4515
OrbisK
commented
Jan 24, 2025
Comment on lines
-80
to
287
throttleFilter(1000, true), | ||
sumSpy, | ||
) | ||
|
||
let result = throttledSum(2, 3) | ||
setTimeout(() => { | ||
result = throttledSum(4, 5) | ||
}, 600) | ||
setTimeout(() => { | ||
result = throttledSum(6, 7) | ||
}, 900) | ||
|
||
vi.runAllTimers() | ||
|
||
expect(sumSpy).toHaveBeenCalledTimes(2) | ||
await expect(result).resolves.toBe(6 + 7) | ||
|
||
setTimeout(() => { | ||
result = throttledSum(8, 9) | ||
}, 1200) | ||
setTimeout(() => { | ||
result = throttledSum(10, 11) | ||
}, 1800) | ||
|
||
vi.runAllTimers() | ||
|
||
expect(sumSpy).toHaveBeenCalledTimes(4) | ||
await expect(result).resolves.toBe(10 + 11) | ||
}) | ||
|
||
expect(sumSpy).toHaveBeenCalledTimes(4) | ||
await expect(result).resolves.toBe(10 + 11) | ||
it('should get leading value', async () => { | ||
const sumSpy = vi.fn((a: number, b: number) => a + b) | ||
const throttledSum = createFilterWrapper( | ||
throttleFilter(1000, false), | ||
sumSpy, | ||
) | ||
|
||
let result = throttledSum(2, 3) | ||
setTimeout(() => { | ||
result = throttledSum(4, 5) | ||
}, 600) | ||
setTimeout(() => { | ||
result = throttledSum(6, 7) | ||
}, 900) | ||
|
||
vi.runAllTimers() | ||
|
||
expect(sumSpy).toHaveBeenCalledTimes(1) | ||
await expect(result).resolves.toBe(2 + 3) | ||
|
||
setTimeout(() => { | ||
result = throttledSum(8, 9) | ||
}, 1200) | ||
setTimeout(() => { | ||
result = throttledSum(10, 11) | ||
}, 1800) | ||
|
||
vi.runAllTimers() | ||
|
||
expect(sumSpy).toHaveBeenCalledTimes(2) | ||
await expect(result).resolves.toBe(8 + 9) | ||
}) | ||
}) | ||
|
||
it('should get leading value', async () => { | ||
const sumSpy = vi.fn((a: number, b: number) => a + b) | ||
const throttledSum = createFilterWrapper( | ||
throttleFilter(1000, false), | ||
sumSpy, | ||
) | ||
|
||
let result = throttledSum(2, 3) | ||
setTimeout(() => { | ||
result = throttledSum(4, 5) | ||
}, 600) | ||
setTimeout(() => { | ||
result = throttledSum(6, 7) | ||
}, 900) | ||
|
||
vi.runAllTimers() | ||
|
||
expect(sumSpy).toHaveBeenCalledTimes(1) | ||
await expect(result).resolves.toBe(2 + 3) | ||
|
||
setTimeout(() => { | ||
result = throttledSum(8, 9) | ||
}, 1200) | ||
setTimeout(() => { | ||
result = throttledSum(10, 11) | ||
}, 1800) | ||
|
||
vi.runAllTimers() | ||
|
||
expect(sumSpy).toHaveBeenCalledTimes(2) | ||
await expect(result).resolves.toBe(8 + 9) | ||
describe('pausableFilter', () => { | ||
it.todo('should pause') | ||
}) |
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.
Did not change these tests, just wrapped them in describe(filterXXX)
OrbisK
commented
Jan 24, 2025
export function watchPausable<T extends Readonly<WatchSource<unknown>[]>, Immediate extends Readonly<boolean> = false>(sources: [...T], cb: WatchCallback<MapSources<T>, MapOldSources<T, Immediate>>, options?: WatchWithFilterOptions<Immediate>): WatchPausableReturn | ||
export function watchPausable<T, Immediate extends Readonly<boolean> = false>(source: WatchSource<T>, cb: WatchCallback<T, Immediate extends true ? T | undefined : T>, options?: WatchWithFilterOptions<Immediate>): WatchPausableReturn | ||
export function watchPausable<T extends object, Immediate extends Readonly<boolean> = false>(source: T, cb: WatchCallback<T, Immediate extends true ? T | undefined : T>, options?: WatchWithFilterOptions<Immediate>): WatchPausableReturn | ||
export type WatchPausableOptions<Immediate> = WatchWithFilterOptions<Immediate> & PausableFilterOptions |
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.
like this?
antfu
approved these changes
Feb 12, 2025
perfect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
enhancement
New feature or request
lgtm
This PR has been approved by a maintainer
size:L
This PR changes 100-499 lines, ignoring generated files.
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.
fixes #4515
Before submitting the PR, please make sure you do the following
fixes #123
).Description
This PR adds
options.initalState
asactive
orpaused
topausableWatcher
. This does allow starting the watcher as paused.Additional context