Skip to content

fix(replay): Fix debounce when maxWait == wait #7208

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

Merged
merged 3 commits into from
Feb 17, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/replay/src/util/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function debounce(func: CallbackFunction, wait: number, options?: Debounc
}
timerId = setTimeout(invokeFunc, wait);

if (maxWait && maxTimerId === undefined && maxWait !== wait) {
if (maxWait && maxTimerId === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, invokeFunc already calls clearTimeout, so we can streamlime this slightly - IMHO we only need to remove the maxWait !== wait part, which was an incorrect "streamlining" that we added.

I'll adjust this and merge this and will actually release this fix today as well!

maxTimerId = setTimeout(invokeFunc, maxWait);
}

Expand Down
17 changes: 15 additions & 2 deletions packages/replay/test/unit/util/debounce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,27 @@ describe('Unit | util | debounce', () => {
const debouncedCallback = debounce(callback, 100, { maxWait: 100 });

debouncedCallback();
jest.advanceTimersByTime(100);
jest.advanceTimersByTime(25);
debouncedCallback();
jest.advanceTimersByTime(25);
debouncedCallback();
jest.advanceTimersByTime(25);
debouncedCallback();
jest.advanceTimersByTime(25);

expect(callback).toHaveBeenCalledTimes(1);

const retval = debouncedCallback();
expect(retval).toBe('foo');

jest.advanceTimersByTime(100);
jest.advanceTimersByTime(25);
debouncedCallback();
jest.advanceTimersByTime(25);
debouncedCallback();
jest.advanceTimersByTime(25);
debouncedCallback();
jest.advanceTimersByTime(25);

expect(callback).toHaveBeenCalledTimes(2);
});
});