Skip to content

Commit 8ce0dae

Browse files
fix(useRafFn): resolve reactive null fpsLimit not being handled (#5284)
1 parent 39afd85 commit 8ce0dae

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

packages/core/useRafFn/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ describe('useRafFn', () => {
7070
expect(fn1.mock.calls.length).toBeLessThan(fn2.mock.calls.length)
7171
})
7272

73+
it('should handle a reactive null fpsLimit as no limit', async () => {
74+
const fn = vi.fn()
75+
const limit = shallowRef<number | null>(null)
76+
useRafFn(fn, { fpsLimit: limit })
77+
await vi.waitFor(() => {
78+
expect(fn).toHaveBeenCalled()
79+
})
80+
})
81+
7382
it('should handle a framerate change', { retry: 3 }, async () => {
7483
const initialFramerate = 60
7584
const fr = shallowRef(initialFramerate)

packages/core/useRafFn/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export interface UseRafFnOptions extends ConfigurableWindow {
2626
immediate?: boolean
2727
/**
2828
* The maximum frame per second to execute the function.
29-
* Set to `undefined` to disable the limit.
29+
* Set to `null` to disable the limit.
3030
*
31-
* @default undefined
31+
* @default null
3232
*/
33-
fpsLimit?: MaybeRefOrGetter<number>
33+
fpsLimit?: MaybeRefOrGetter<number | null>
3434
/**
3535
* After the requestAnimationFrame loop executed once, it will be automatically stopped.
3636
*
@@ -49,14 +49,15 @@ export interface UseRafFnOptions extends ConfigurableWindow {
4949
export function useRafFn(fn: (args: UseRafFnCallbackArguments) => void, options: UseRafFnOptions = {}): Pausable {
5050
const {
5151
immediate = true,
52-
fpsLimit = undefined,
52+
fpsLimit = null,
5353
window = defaultWindow,
5454
once = false,
5555
} = options
5656

5757
const isActive = shallowRef(false)
5858
const intervalLimit = computed(() => {
59-
return fpsLimit ? 1000 / toValue(fpsLimit) : null
59+
const limit = toValue(fpsLimit)
60+
return limit ? 1000 / limit : null
6061
})
6162
let previousFrameTimestamp = 0
6263
let rafId: null | number = null

0 commit comments

Comments
 (0)