Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/hooks/src/useRafInterval/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,16 @@ describe('useRafInterval', () => {
// not to be called
expect(callback).toHaveBeenCalledTimes(0);
});

test('execute clear in the callback and interval should be clear', () => {
const callback = vi.fn().mockImplementation(() => hook.result.current());
const hook = setUp({ fn: callback, delay: FRAME_TIME });

expect(callback).not.toBeCalled();

vi.advanceTimersByTime(FRAME_TIME * 1.5);
expect(callback).toHaveBeenCalledTimes(1);
vi.advanceTimersByTime(FRAME_TIME * 1.5);
expect(callback).toHaveBeenCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion packages/hooks/src/useRafInterval/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const setRafInterval = (callback: () => void, delay: number = 0): Handle => {
};
const loop = () => {
const current = Date.now();
handle.id = requestAnimationFrame(loop);
if (current - start >= delay) {
callback();
start = Date.now();
}
handle.id = requestAnimationFrame(loop);
};
handle.id = requestAnimationFrame(loop);
return handle;
Expand Down
Loading