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

Commit 48e0a2e

Browse filesBrowse files
43081jOrbisK
andauthored
fix(debounceFilter): invoke on maxWait with the latest invoker (#4497)
Co-authored-by: Robin Kehl <robin.kehl@singular-it.de> Co-authored-by: OrbisK <37191683+OrbisK@users.noreply.github.com>
1 parent b6947f7 commit 48e0a2e
Copy full SHA for 48e0a2e

File tree

Expand file treeCollapse file tree

2 files changed

+32
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-2
lines changed

‎packages/shared/utils/filters.ts

Copy file name to clipboardExpand all lines: packages/shared/utils/filters.ts
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export function debounceFilter(ms: MaybeRefOrGetter<number>, options: DebounceFi
7373
lastRejector = noop
7474
}
7575

76+
let lastInvoker: () => void
77+
7678
const filter: EventFilter = (invoke) => {
7779
const duration = toValue(ms)
7880
const maxDuration = toValue(options.maxWait)
@@ -90,13 +92,14 @@ export function debounceFilter(ms: MaybeRefOrGetter<number>, options: DebounceFi
9092

9193
return new Promise((resolve, reject) => {
9294
lastRejector = options.rejectOnCancel ? reject : resolve
95+
lastInvoker = invoke
9396
// Create the maxTimer. Clears the regular timer on invoke
9497
if (maxDuration && !maxTimer) {
9598
maxTimer = setTimeout(() => {
9699
if (timer)
97100
_clearTimeout(timer)
98101
maxTimer = null
99-
resolve(invoke())
102+
resolve(lastInvoker())
100103
}, maxDuration)
101104
}
102105

‎packages/shared/watchDebounced/index.test.ts

Copy file name to clipboardExpand all lines: packages/shared/watchDebounced/index.test.ts
+28-1Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,33 @@ describe('watchDebounced', () => {
4242
num.value = 5
4343
await vi.advanceTimersByTimeAsync(75)
4444
expect(cb).toHaveBeenCalledTimes(2)
45-
expect(cb).toHaveBeenCalledWith(4, 2, expect.anything())
45+
expect(cb).toHaveBeenCalledWith(5, 4, expect.anything())
46+
})
47+
48+
it('should work with constant changes over multiple maxWaits', async () => {
49+
vi.useFakeTimers()
50+
const num = ref(0)
51+
const cb = vi.fn()
52+
53+
const constantUpdateOverTime = async (ms: number) => {
54+
for (let i = 0; i < ms; i++) {
55+
num.value += 1
56+
await vi.advanceTimersByTimeAsync(1)
57+
}
58+
}
59+
watchDebounced(num, cb, { debounce: 10, maxWait: 50 })
60+
expect(cb).toHaveBeenCalledTimes(0)
61+
await constantUpdateOverTime(49)
62+
expect(cb).toHaveBeenCalledTimes(0)
63+
await constantUpdateOverTime(1)
64+
expect(cb).toHaveBeenCalledTimes(1)
65+
await constantUpdateOverTime(50)
66+
expect(cb).toHaveBeenCalledTimes(2)
67+
await constantUpdateOverTime(50)
68+
69+
expect(cb).toHaveBeenCalledTimes(3)
70+
expect(cb.mock.calls[0][0]).toBe(50)
71+
expect(cb.mock.calls[1][0]).toBe(100)
72+
expect(cb.mock.calls[2][0]).toBe(150)
4673
})
4774
})

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.