Skip to content
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

fix(watchThrottled): duplicate callback invokation when leading and trailing edges coincide #1593

Merged
merged 2 commits into from May 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/useThrottledRefHistory/index.test.ts
Expand Up @@ -16,7 +16,7 @@ describe('useThrottledRefHistory - sync', () => {

await promiseTimeout(ms * 3)

expect(history.value.length).toBe(3)
expect(history.value.length).toBe(2)
expect(history.value[0].snapshot).toBe(100)

v.value = 200
Expand All @@ -25,7 +25,7 @@ describe('useThrottledRefHistory - sync', () => {

await promiseTimeout(ms * 3)

expect(history.value.length).toBe(5)
expect(history.value.length).toBe(3)
expect(history.value[0].snapshot).toBe(400)
})
})
17 changes: 8 additions & 9 deletions packages/shared/utils/filters.ts
Expand Up @@ -103,7 +103,7 @@ export function debounceFilter(ms: MaybeRef<number>, options: DebounceFilterOpti
export function throttleFilter(ms: MaybeRef<number>, trailing = true, leading = true) {
let lastExec = 0
let timer: ReturnType<typeof setTimeout> | undefined
let preventLeading = !leading
let isLeading = true

const clear = () => {
if (timer) {
Expand All @@ -123,24 +123,23 @@ export function throttleFilter(ms: MaybeRef<number>, trailing = true, leading =
return invoke()
}

if (elapsed > duration) {
if (elapsed > duration && (leading || !isLeading)) {
lastExec = Date.now()
if (preventLeading)
preventLeading = false
else invoke()
invoke()
}
if (trailing) {
else if (trailing) {
timer = setTimeout(() => {
lastExec = Date.now()
if (!leading)
preventLeading = true
isLeading = true
clear()
invoke()
}, duration)
}

if (!leading && !timer)
timer = setTimeout(() => preventLeading = true, duration)
timer = setTimeout(() => isLeading = true, duration)

isLeading = false
}

return filter
Expand Down
25 changes: 11 additions & 14 deletions packages/shared/utils/index.test.ts
Expand Up @@ -72,20 +72,6 @@ describe('filters', () => {
expect(debouncedFilterSpy).toHaveBeenCalledTimes(2)
})

it('should throttle', () => {
const debouncedFilterSpy = vitest.fn()
const filter = createFilterWrapper(throttleFilter(1000), debouncedFilterSpy)

setTimeout(filter, 500)
setTimeout(filter, 500)
setTimeout(filter, 500)
setTimeout(filter, 500)

vitest.runAllTimers()

expect(debouncedFilterSpy).toHaveBeenCalledTimes(2)
})

it('should throttle with ref', () => {
const debouncedFilterSpy = vitest.fn()
const throttle = ref(0)
Expand All @@ -102,4 +88,15 @@ describe('filters', () => {

expect(debouncedFilterSpy).toHaveBeenCalledTimes(2)
})

it('should not duplicate single event', () => {
const debouncedFilterSpy = vitest.fn()
const filter = createFilterWrapper(throttleFilter(1000), debouncedFilterSpy)

setTimeout(filter, 500)

vitest.runAllTimers()

expect(debouncedFilterSpy).toHaveBeenCalledTimes(1)
})
})