Skip to content

Commit

Permalink
fix(watchThrottled): duplicate callback invokation when leading and t…
Browse files Browse the repository at this point in the history
…railing edges coincide (#1593)
  • Loading branch information
bodograumann committed May 16, 2022
1 parent cd6f856 commit 8ba3b9d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
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 @@ -77,20 +77,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 @@ -107,4 +93,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)
})
})

0 comments on commit 8ba3b9d

Please sign in to comment.