Skip to content

Commit

Permalink
fix(useIntervalFn): prevent timer being set after pause is called in …
Browse files Browse the repository at this point in the history
…cb (#4258)

Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
laporchen and antfu authored Oct 24, 2024
1 parent f71e513 commit 59f8c94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/shared/useIntervalFn/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ describe('useIntervalFn', () => {
expect(callback).toHaveBeenCalledTimes(0)
})

it('pause in callback', async () => {
const pausable = useIntervalFn(() => {
callback()
pausable.pause()
}, 50, { immediateCallback: true, immediate: false })

pausable.resume()
expect(pausable.isActive.value).toBeFalsy()
expect(callback).toHaveBeenCalledTimes(1)

await promiseTimeout(60)
expect(callback).toHaveBeenCalledTimes(1)

pausable.resume()
expect(pausable.isActive.value).toBeFalsy()
expect(callback).toHaveBeenCalledTimes(2)

await promiseTimeout(60)
expect(callback).toHaveBeenCalledTimes(2)
})

it('cant work when interval is negative', async () => {
const { isActive } = useIntervalFn(callback, -1)

Expand Down
3 changes: 2 additions & 1 deletion packages/shared/useIntervalFn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export function useIntervalFn(cb: Fn, interval: MaybeRefOrGetter<number> = 1000,
if (immediateCallback)
cb()
clean()
timer = setInterval(cb, intervalValue)
if (isActive.value)
timer = setInterval(cb, intervalValue)
}

if (immediate && isClient)
Expand Down

0 comments on commit 59f8c94

Please sign in to comment.