Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(debounceFilter): invoke on maxWait with the latest invoker #4497

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 5 commits into from
Jan 22, 2025
Merged
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
5 changes: 4 additions & 1 deletion 5 packages/shared/utils/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export function debounceFilter(ms: MaybeRefOrGetter<number>, options: DebounceFi
lastRejector = noop
}

let lastInvoker: () => void

const filter: EventFilter = (invoke) => {
const duration = toValue(ms)
const maxDuration = toValue(options.maxWait)
Expand All @@ -90,13 +92,14 @@ export function debounceFilter(ms: MaybeRefOrGetter<number>, options: DebounceFi

return new Promise((resolve, reject) => {
lastRejector = options.rejectOnCancel ? reject : resolve
lastInvoker = invoke
// Create the maxTimer. Clears the regular timer on invoke
if (maxDuration && !maxTimer) {
maxTimer = setTimeout(() => {
if (timer)
_clearTimeout(timer)
maxTimer = null
resolve(invoke())
resolve(lastInvoker())
}, maxDuration)
}

Expand Down
29 changes: 28 additions & 1 deletion 29 packages/shared/watchDebounced/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@ describe('watchDebounced', () => {
num.value = 5
await vi.advanceTimersByTimeAsync(75)
expect(cb).toHaveBeenCalledTimes(2)
expect(cb).toHaveBeenCalledWith(4, 2, expect.anything())
expect(cb).toHaveBeenCalledWith(5, 4, expect.anything())
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as i can tell, this test was already showing the bug but we just rolled with it

second pair of eyes on this test would be helpful

})

it('should work with constant changes over multiple maxWaits', async () => {
vi.useFakeTimers()
const num = ref(0)
const cb = vi.fn()

const constantUpdateOverTime = async (ms: number) => {
for (let i = 0; i < ms; i++) {
num.value += 1
await vi.advanceTimersByTimeAsync(1)
}
}
watchDebounced(num, cb, { debounce: 10, maxWait: 50 })
expect(cb).toHaveBeenCalledTimes(0)
await constantUpdateOverTime(49)
expect(cb).toHaveBeenCalledTimes(0)
await constantUpdateOverTime(1)
expect(cb).toHaveBeenCalledTimes(1)
await constantUpdateOverTime(50)
expect(cb).toHaveBeenCalledTimes(2)
await constantUpdateOverTime(50)

expect(cb).toHaveBeenCalledTimes(3)
expect(cb.mock.calls[0][0]).toBe(50)
expect(cb.mock.calls[1][0]).toBe(100)
expect(cb.mock.calls[2][0]).toBe(150)
})
})
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.