Skip to content

Commit

Permalink
fix(replay): Fix debounce when maxWait == wait (#7208)
Browse files Browse the repository at this point in the history
Co-authored-by: Francesco Novy <francesco.novy@sentry.io>
  • Loading branch information
billyvg and mydea committed Feb 17, 2023
1 parent 8305b94 commit aacdf2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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) {
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);
});
});

0 comments on commit aacdf2b

Please sign in to comment.